jQuery(document).ready(function($) {

	isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	if(isIE6)
	{
		//$(document).pngFix();
		$('.homeLink').pngFix(); 
		$('.LifestyleName').pngFix(); 
		$('.pngWrapper').pngFix();  
	}
	/*
  $(".backgroundWidgetInner").quickPager( {
		pageSize: 6,
		currentPage: 1,
		pagerLocation: "after"
	});
	*/
	
 $(".backgroundWidgetInner").PageIt();
  checkBackgroundCookie();
  var lifestyleBasements = {
    okToRun: true
 };
  $(".summary_text").each(function(){
  	$(this).hide();
  });
	
 
  $('.showContent').click(function(){
  	showContentArea();
  	return false;
  });
 
  $('#topNav li a').click(function(){
  	showContentArea();
  });

  
  $('.backgroundWidgetInner a').click(function(){
	if (isIE6)
	{
		return true;
	}
 	hideContentArea();  

	setBackground($(this).attr('href'));
  	return false;
  });
  
  function showContentArea()
  {
  	if(!$('.contentWrapper .content').is(':visible'))
  	{
  		$('.contentWrapper .content').slideDown("slow");
  		$('.showContent').fadeOut('slow');
  		/*$('.headerInner').animate({width: "810px"}, 1500, function() {
   		 $('#topNav').show();
  		} );*/
  		//$('.callNow').animate({right: "20px"}, 1500);  		
  	}
  }
  
  
  function hideContentArea()
  {
  	if($('.contentWrapper .content').is(':visible'))
  	{
  		$('.showContent').fadeIn('slow');
  		$('.contentWrapper .content').slideUp("slow");
  		//$('#topNav').hide();
  		//$('header').css('width','310px');
  		//$('.headerInner').animate({width: "350px"}, 1500);
  		//$('.callNow').animate({right: "510px"}, 1500);
  		
  		//$('.headerInner').css('width','350px');
  	}
  }
  
  
  function setBackground(background)
  {
  	//if we need to figure out how to wait for something to finish, do it here

	  	if(getCookie('background')!=background)
	  	{
	  		$.backstretch(background,{speed:400});
	  		setCookie('background',background,1);
	  	}
	
  }
  
  
  function checkBackgroundCookie()
{

	var background=getCookie('background');
	if (background!=null && background!="")
	  {
	  	//alert('from memory: '+background)
	  	$.backstretch(background);
	  	setCookie('background',background,1);
	  }
	else
	  {
	  	background = $('#defaultBackground').html();
	  	setBackground(background);
	  }
}

function deleteCookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}


function setCookie(c_name,value,expiredays)
{
	deleteCookie('background');
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString()+"; path=/");
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

$("#next").click(function() {
	
	setPagerVal(parseInt($(this).attr('class')));
	return false;
});
$("#prev").click(function() {
	
	setPagerVal(parseInt($(this).attr('class')));
	return false;
});


function setPagerVal(value) 
{
	if(value>0 && value<=parseInt($('.totalPages').html()))
	{
		$(".backgroundWidgetInner").PageIt( {
		currentPage: value
		});
		$("#next").attr('class', value+1);
		$("#prev").attr('class', value-1);
	}
}



});



(function($) {
	    
	$.fn.PageIt = function(options) {
	
		var defaults = {
				pageSize: 6,
				currentPage: 1,
				holder: null,
				pagerLocation: "after"
			};
		
		var options = $.extend(defaults, options);
		var selector = $(this);	
		var pageCounter = 1;
		
		selector.children().each(function(i){ 
						
			if(i < pageCounter*options.pageSize && i >= (pageCounter-1)*options.pageSize) {
				
				$(this).addClass("page_"+pageCounter);
			}
			else {
				$(this).addClass("page_"+(pageCounter+1));
				pageCounter ++;
				$('.pager').css('visibility','visible');
			}
			//alert($(this).attr('class'));
			if($(this).hasClass('page_'+options.currentPage))
			{
				$(this).removeClass('notCurrent');
				$(this).addClass('current');
			}
			else
			{
				$(this).removeClass('current');
				$(this).addClass('notCurrent');
			}
					
		});
		
		$('.notCurrent').each(function(){
			$(this).hide();
		});

		$('.current').each(function(){
			$(this).fadeIn('slow');
		});
		
		
				
		$('.totalPages').html(pageCounter);
				
	
	}

})(jQuery);
