// init vars for bg
var theBg, pW, pH, winRatio, imgRatio, imgLeft, contHeight;
var theBgW = 1920; // set as backup for slow file loading b/c script needs this bg width
var imgBgW = 1920;

/*
$('document').ready(function() {
	// gather initial state
	theBg = $('body').css('background-image');
	theBg = theBg.match(/\/deals\/.*\.jpg/);
	pW = $(window).width();
	pH = $(window).height();
	contHeight = $('.mainContainer').outerHeight();
	
	$('body').prepend('<div id="stretch"><img src="/" alt="" /></div>');
	$('#stretch img').attr('src',theBg);
	
	winRatio = pW / pH;
	imgRatio = $('#stretch img').width() / $('#stretch img').height();
	
	resizeBG(); // init bg resize
	
	// set up resize change
	$(window).resize(function() {
		if ($('#DvLayer').is(':visible')) {
			pW = $(window).width();
			pH = $('#DvLayer').height();
			winRatio = pW / pH;
			
			resizeBGdvLayer();

		} else {
			pW = $(window).width();
			pH = $(window).height();
			winRatio = pW / pH;
			resizeBG();
		}
	});
});
*/


$(document).ready(function(){
    //  Initialize Backgound Stretcher
    var theBg = $('body').css('background-image');
    theBg = theBg.match(/\/deals\/.*\.jpg/);
    var img = new Image();
    img.src = theBg;    
    $(document).bgStretcher({        
        images: theBg, imageWidth: img.naturalWidth, imageHeight: img.naturalHeight
    });
});


function resizeBG() {
	if ((pH < contHeight)) { // we have to stop the sizing down if too small
		$('#stretch').css('height',contHeight + 'px');
		return false;
	}
	$('#stretch').css('height','100%');
	if (imgRatio < winRatio) {
		$('#stretch img').css({'width': pW, 'height': pW / imgRatio});
	} else {
		$('#stretch img').css({'width': pH * imgRatio, 'height': pH});
	}
	imgBgWidth = $('#stretch img').width();
	if (imgBgWidth == 0) imgBgWidth = imgBgW;
	imgLeft = (imgBgWidth - pW) / -2;
	$('#stretch img').css('left', imgLeft);
}

function resizeBGdvLayer() {
	if (imgRatio < winRatio) {
		$('#stretch').css('height',pW / imgRatio);
		$('#stretch img').css({'width': pW, 'height': pW / imgRatio});
	} else {
		$('#stretch').css('height',pH);
		$('#stretch img').css({'width': pH * imgRatio, 'height': pH});
	}
	imgBgWidth = $('#stretch img').width();
	if (imgBgWidth == 0) imgBgWidth = imgBgW;
	imgLeft = (imgBgWidth - pW) / -2;
	$('#stretch img').css('left', imgLeft);
}

var selectLists = document.getElementsByTagName('select');
function DvLayerOpen(popCon, TPos){
	var DvLayerId = document.getElementById('DvLayer'); 	   // Var for Main full Opaque Layer
	var DvMessageId = document.getElementById(popCon);         // Var for Main content div popup Layer
	var ScrollHeight = document.documentElement.scrollHeight;  // Value for scroll height of the page
	var ScrollWidth = document.body.scrollWidth; 			   // Value for scroll width of the page
	var ClientHeight = document.documentElement.clientHeight;  // Value for browser height of the page
	var ClientWidth = document.documentElement.clientWidth;    // Value for Browser width of the page
	var OffsetHeight = document.documentElement.offsetHeight;  // Value for scroll height for Safari of the page
	var OffsetWidth = document.documentElement.offsetWidth;    // Value for height height for Safari of the page
	//For Select DropBox Hide//
	for (var counter=0; counter<selectLists.length; counter++){
		version=parseInt(navigator.appVersion);
		if (navigator.appName=='Microsoft Internet Explorer' && !$('select').hasClass('required'))
			selectLists[counter].style.visibility = 'hidden'
		else
			selectLists[counter].style.MozOpacity = .90;
			selectLists[counter].style.opacity = .90;
			selectLists[counter].style.filter = 'alpha(opacity=' + 90 + ')';
	}
	//For Select DropBox Hide//
	if (DvLayerId != null) {
	    DvLayerId.style.display = "block";
	    DvLayerId.style.width = ClientWidth + "px";
	    if (ScrollHeight > ClientHeight) {
	        DvLayerId.style.height = ScrollHeight + "px";
	    } else
	        DvLayerId.style.height = ClientHeight + "px";
	}

	if (DvMessageId != null) {
	    DvMessageId.style.display = "block";

	    var aaa = document.documentElement.scrollTop;

	    var popW = DvMessageId.clientWidth;
	    var movePos = popW / 2;
	    var centerPos = ClientWidth / 2;
	    var Lalign = centerPos - movePos;

	    DvMessageId.style.left = Lalign + 'px';

	    var popH = DvMessageId.clientHeight;
	    var middlePos = ClientHeight / 2;
	    var moveMidPos = popH / 2;
	    var Talign = middlePos - moveMidPos;

	    DvMessageId.style.top = '36px';
	    if (DvMessageId.scrollHeight > ScrollHeight) {
	        DvLayerId.style.height = DvMessageId.clientHeight + 300 + 'px';
	    }

	    pW = $(window).width();
	    pH = $('#DvLayer').height();
	    winRatio = pW / pH;

	    // resizeBGdvLayer();
	}
	
	//add GA tracking
	_gaq.push(['_trackEvent',popCon,window.location.pathname]);
}
function DvLayerHide(popCon){ 
	$('#nsHidden').val('');
    document.getElementById('DvLayer').style.display='none';
    document.getElementById(popCon).style.display='none';
	//For Select DropBox Show//
	for (var counter=0; counter<selectLists.length; counter++){
		version=parseInt(navigator.appVersion);
		if (navigator.appName=='Microsoft Internet Explorer')
			selectLists[counter].style.visibility = '';
		else
			selectLists[counter].style.MozOpacity = 1.0;
			selectLists[counter].style.opacity = 1.0;
			selectLists[counter].style.filter = 'alpha(opacity=' + 100 + ')';
	}
	
	//For Select DropBox Show//
	
	pW = $(window).width();
	pH = $(window).height();
	winRatio = pW / pH;

	// resizeBG();
}

