function initHomePage(){
	menuInitHome();
	
	if(location.href.indexOf("#") != -1){
		doChainedWindow();
	}
}


function doChainedWindow(){
	$("#menu-main-menu li a.win").each(function(index) {
		if($(this).attr('href').indexOf(location.href.split("#")[1]) !=-1){
			var winId = $(this).parent().attr('id');							 
			doLPWin($(this), winId);
		}		
	  });	
}

function menuInitHome(){
	$("#menu-main-menu li a.win").click(function(){
			var winId = $(this).parent().attr('id');									 
			doLPWin($(this), winId); return false;
	});
}

function doLPWin(menuItem, winId){
	var destUrl = menuItem.attr('href');
	var winID = winId.replace('menu' , 'win');
	if($('#'+winID).length != 0){
	 	setCurrentWin(winID);
	}else{
		createWin(winID, destUrl);
	}
}

function setCurrentWin(winID){
	$('.floatingWin').removeClass('currentWin');
	$('#'+winID).addClass('currentWin');
}

function createWin(winID, destUrl){
	this.$NewWin = $('<div></div>')
	$NewWin.addClass("floatingWin");
	$NewWin.attr("id", winID);
	$NewWin.mousedown(function(){setCurrentWin(winID)});
	$NewWin.hide();

	var cssObj = {
      'top' : 100 + (30 * $("#content > div.floatingWin").size()),
      'left' : 100 + (50 * $("#content > div.floatingWin").size())
    }
	
    $NewWin.css(cssObj);
	
	$("#content").append($NewWin);

	setCurrentWin(winID);
	$.ajax({
	  url: destUrl,
	  data: "ajaxed=true",
	  success: function(data) {
		$NewWin.html(data);
		$NewWin.find("a.close").click(function(){closeWin($(this)); return false;});
		$NewWin.draggable({handle:"div.winHeader"});
		
		$NewWin.find(".winScroller").slider({
			orientation: "vertical",
			range: "min",
			min: 0,
			max: 100,
			value: 100,
			animate: true,
			change: handleSliderChange,
			slide: handleSliderSlide
		});
		
		$NewWin.fadeIn('slow');
	  }
	});
	
}

function closeWin($alink){
	$toClose = $alink.parent().parent();
	$toClose.fadeOut('slow', function(){$toClose.remove();});
}

function handleSliderChange(e, ui)
{
  var maxScroll = $(".currentWin").find(".winContent").attr("scrollHeight") -
                  $(".currentWin").find(".winContent").height();
  $(".currentWin").find(".winContent").animate({scrollTop: (100-ui.value) *
     (maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui)
{
  var maxScroll = $(".currentWin").find(".winContent").attr("scrollHeight") -
                  $(".currentWin").find(".winContent").height();
  $(".currentWin").find(".winContent").attr({scrollTop: (100 -ui.value) * (maxScroll / 100) });
}

function nextPromo(){
	var next = $('li.active').next();
	if(!next.length){
	  	next = 	$("#homePromos ul li:first-child");
	 }
	 $('li.active').animate({
		color: '#404040'
	  }, 500, function() {
		$(this).removeClass('active');
	  });
	 
	next.animate({
		color: '#fff'
	  }, 500, function() {
		$(this).addClass('active');
		doNext();
	  });
	
	
}

function doNext(){
	setTimeout("nextPromo()",3000);
}


