// On DOM ready
$(document).ready( function() {
	
	
	$(window).bind("resize", adjustLayout);
	
	adjustLayout();
	
	
});

function adjustLayout() {
	 //alert($(window).height());
	 var winHeight = $(window).height();
	 var winWidth = $(window).width();
	 
	 // normal is be 1600 x 1065
	 
	 if(winWidth > 1600){
		  var hOffSet = Math.abs(Math.floor((1600 - winWidth) / 2));
	 } else {
		 var hOffSet = -(Math.floor((1600 - winWidth) / 2));
	 }
	 
	 if(winHeight > 1065) {
		 var vOffSet = Math.abs(Math.floor((1065 - winHeight) / 2) + 45); 
	 } else  {
		 var vOffSet = -(Math.floor((1065 - winHeight) / 2) - 45);
	 }
	
	 
	 /*if(winHeight < 750){
		 $("div#fakeBackground").removeClass('attachTop').addClass('attachBottom');
	 } else {
		 $("div#fakeBackground").removeClass('attachBottom').addClass('attachTop');
	 }*/
	 
	 $("div#fakeBackground, div#fakeFade").css({'left':hOffSet, 'top' : vOffSet});
			
}

// ON window READY
$(window).ready( function() {
	
	adjustLayout();
	
	$(function() {
		// retrieve list of slides from server
		$.getJSON('/includes/backgrounds/ajax_backgrounds.cfm?rand=' + (Math.random() * 999999), startSlideShow);
		//slides = ['fence_winter.jpg','kidsNcocoa_winter.jpg','ski-into-town_winter.jpg','street-sign_winter.jpg','snow-ridges_winter.jpg'];
		
		//startSlideShow(slides);
		
		function startSlideShow(slides){
			
			var totalSlideCount = 1 + slides.length;	
			
			var $slideshow = $('#fakeBackground.fakeHome');
			
			// markup contains only a single slide; before starting the slideshow we  
         // append one slide and prepend one slide (to account for prev/next behavior)
			$slideshow.prepend('<img src="/includes/images/backgrounds/' + slides.pop() + '" />');
			$slideshow.append('<img src="/includes/images/backgrounds/' + slides.shift() + '" />');
			
			//start show
			$('#fakeBackground.fakeHome').cycle({
				fx:				'fade',
				startingSlide: 1, //must start with slide from markup
				timeout: 		8000,
				speed: 			2000,
				before:			onBefore
			});
			
			function onBefore(curr, next, opts, fwd){
				//	curr == cur element
				//	next == next DOM element
				// opts == slideshow options
				// fwd == true if cycling forward, false if backward
				
				// on first pass, addSlide is unDefined cause plugin hasn't created the function yet
				if(!opts.addSlide)
					return;
				
				if(opts.slideCount == totalSlideCount)
					return;
				
				// shif or pop from slide arr	
				var nextSlideSrc = fwd ? slides.shift() : slides.pop();
				
				// add next slide
				opts.addSlide('<img src="/includes/images/backgrounds/' + nextSlideSrc + '" />', fwd == false);	
			};
		};
	});
	
});

function welcome_hotdeals_reset(me,a) {
	var wwt = jQuery('#welcomeWidgetTop');
	var cww = jQuery('#couponWidgetWelcome');	
	cww.slideUp();
	wwt.slideDown();
	jQuery(me).addClass('ui-state-active');
	jQuery(me).attr('aria-expanded','true');
	jQuery(a).show();
}

function welcome_hotdeals_toggle(me,toggleme) {
	var wwt = jQuery('#welcomeWidgetTop');
	var cww = jQuery('#couponWidgetWelcome');
	var cur = jQuery('#sideColumnContent h3').first();
	
	if (toggleme != 0)
	{
		jQuery(me).hide();
	}
	
	if (wwt.is(":visible"))
	{ 
		wwt.slideUp();
		cww.slideDown();
		cur.bind('click',function(){welcome_hotdeals_reset(cur,me);});
		cur.removeClass('ui-state-active');
		cur.addClass('ui-state-default');
		cur.attr('aria-expanded','false');
	} 
	else
	{ 
		welcome_hotdeals_reset(cur,me);
	}
}

