$(document).ready(function(){
	
	var currentPromoPosition = 0;
	var currentPromo = $('.rotating-promo').get(currentPromoPosition);
	var promosLength = $('.rotating-promo').length;
	var promoSwitchDelay = 8000;
	var paused = false;
	
	if(promosLength > 1)
	{
		$('.rotate-controls').css('visibility','visible');
		var promoInterval = window.setInterval(rotate,promoSwitchDelay);
	}
	
	function rotate() {
		if(currentPromoPosition < promosLength-1)
		{
			currentPromoPosition++;
		}
		else
		{
			currentPromoPosition = 0;
		}
		$(currentPromo).css('z-index',2);
		$.preload([$($('.rotating-promo').get(currentPromoPosition)).children(".HeadlineCont").children("a").children("img").attr("src")], {onFinish:function(data){
			$($('.rotating-promo').get(currentPromoPosition)).css('z-index',0).css('display','block').addClass('visible');
			$(currentPromo).fadeOut("slow",function(){
				$(this).removeClass('visible');
				currentPromo = $('.rotating-promo').get(currentPromoPosition);
			});
		}});
	

	}
	function rotateBack() {
		if(currentPromoPosition > 0)
		{
			currentPromoPosition--;
		}
		else
		{
			currentPromoPosition = promosLength-1;
		}
		
		$(currentPromo).css('z-index',2);
		$.preload([$($('.rotating-promo').get(currentPromoPosition)).children(".HeadlineCont").children("a").children("img").attr("src")], {onFinish:function(data){
			$($('.rotating-promo').get(currentPromoPosition)).css('z-index',0).css('display','block').addClass('visible');
			$(currentPromo).fadeOut("slow",function(){
				$(this).removeClass('visible');
				currentPromo = $('.rotating-promo').get(currentPromoPosition);
			});
		}});
	}
	
	$('.rotate-pause img').mouseover(function(){
		if(!paused)
		{
			$('.rotate-pause img').attr('src', 'http://asset2.homesandproperty.co.uk/handp/media/pause_on_3688.gif');
		}
	});
	$('.rotate-pause img').mouseout(function(){
		if(!paused)
		{$('.rotate-pause img').attr('src', 'http://asset1.homesandproperty.co.uk/handp/media/pause_off_3687.gif');}
	});
	$('.rotate-pause img').click(function(){
		if(!paused){
		paused = true;
		$('.rotate-play img').attr('src', 'http://asset1.homesandproperty.co.uk/handp/media/play_off_3689.gif');
		$('.rotate-pause img').attr('src', 'http://asset2.homesandproperty.co.uk/handp/media/pause_on_3688.gif');
		
		window.clearInterval(promoInterval);
		}
	});
	
	$('.rotate-play img').mouseover(function(){
		if(paused)
		{$('.rotate-play img').attr('src', 'http://asset2.homesandproperty.co.uk/handp/media/play_on_3690.gif');}
	})
	$('.rotate-play img').mouseout(function(){
		if(paused)
		{$('.rotate-play img').attr('src', 'http://asset1.homesandproperty.co.uk/handp/media/play_off_3689.gif');}
	});
	$('.rotate-play img').click(function(){
		if(paused){
		paused = false;
		$('.rotate-play img').attr('src', 'http://asset2.homesandproperty.co.uk/handp/media/play_on_3690.gif');
		$('.rotate-pause img').attr('src', 'http://asset1.homesandproperty.co.uk/handp/media/pause_off_3687.gif');
		
		rotate();
		promoInterval = window.setInterval(rotate,promoSwitchDelay);
		}
	});
	
	$('.rotate-back img').mouseover(function(){
		$('.rotate-back img').attr('src', 'http://asset1.homesandproperty.co.uk/handp/media/back_on_3683.gif');
	});
	$('.rotate-back img').mouseout(function(){
		$('.rotate-back img').attr('src', 'http://asset2.homesandproperty.co.uk/handp/media/back_off_3682.gif');
	});
	$('.rotate-back img').click(function(){
		window.clearInterval(promoInterval);
		rotateBack();
		if(!paused)
		{promoInterval = window.setInterval(rotate,promoSwitchDelay);}
	});
	
	$('.rotate-next img').mouseover(function(){
		$('.rotate-next img').attr('src', 'http://asset1.homesandproperty.co.uk/handp/media/next_on_3685.gif');
	});
	$('.rotate-next img').mouseout(function(){
		$('.rotate-next img').attr('src', 'http://asset2.homesandproperty.co.uk/handp/media/next_off_3684.gif');
	});
	$('.rotate-next img').click(function(){
		window.clearInterval(promoInterval);
		
		rotate();
		if(!paused)
		{promoInterval = window.setInterval(rotate,promoSwitchDelay);}
	});
	
});


var autoSlideWidgetInterval=7000;
var slideWidgetDelayBetweenStarts=1000;
var slideWidgets= new Array();

$(function() {

    $("div.slideWidgetContainer").each(function(i,e){
        $(this).attr("id","slideWidget_"+i);
        slideAssignControls($(this),(i*slideWidgetDelayBetweenStarts));
    });
});


function slideAssignControls(widget,delay) {

	$("a.slideRightBtn.scontrol").hover(function(){
if($(this).parent('.topnav')){}else{
		$(this).children("img").attr("src","http://asset1.homesandproperty.co.uk/handp/media/next_on_3685.gif");
}
	},function(){
if($(this).parent('.topnav')){}else{
		$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/next_off_3684.gif");
}
	});

	$("a.slideLeftBtn.scontrol").hover(function(){
if($(this).parent('.topnav')){}else{
		$(this).children("img").attr("src","http://asset1.homesandproperty.co.uk/handp/media/back_on_3683.gif");
}
	},function(){
if($(this).parent('.topnav')){}else{
		$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/back_off_3682.gif");
}
	});

	$("a.slidePauseBtn.scontrol").hover(function(){
		if(!slideWidgets[$(this).parents("div.slideWidgetContainer").attr("id")].slideWidgetPause){
			$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/pause_on_3688.gif");
		}
	},function(){
		if(! slideWidgets[$(this).parents("div.slideWidgetContainer").attr("id")].slideWidgetPause){
			$(this).children("img").attr("src","http://asset1.homesandproperty.co.uk/handp/media/pause_off_3687.gif");
		}
	});

	$("a.slidePlayBtn.scontrol").hover(function(){
		if(slideWidgets[$(this).parents("div.slideWidgetContainer").attr("id")].slideWidgetPause){
			$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/play_on_3690.gif");
		}
	},function(){
		if(slideWidgets[$(this).parents("div.slideWidgetContainer").attr("id")].slideWidgetPause){
			$(this).children("img").attr("src","http://asset1.homesandproperty.co.uk/handp/media/play_off_3689.gif");
		}
	});

    var slideWidgetSettings= {     
        containerNode:widget,
        containerNodeID: widget.attr("id"),
        autoSlideWidget:true, 
        autoStartSlideWidget:true,
        autoSlideWidgetInterval:autoSlideWidgetInterval,
        slideLeftButton:"a.slideLeftBtn",
        slideRightButton:"a.slideRightBtn",
        slidePauseButton:"a.slidePauseBtn",
        slidePlayButton:"a.slidePlayBtn",
        autoSlideWidgetTO:0,
        slideWidgetPause:false
    
    };
  
    slideWidgets[widget.attr("id")]=slideWidgetSettings;
//    alert(slideWidgets[widget.attr("id")].containerNodeID);
    

    //assign buttons
    $(slideWidgetSettings.slideRightButton).click(function(){
        pauseSlideWidget($(this).parents("div.slideWidgetContainer").attr("id"));
	slidePanels($(this).parents("div.slideWidgetContainer").attr("id"),+1);
return false;
	});
	
	$(slideWidgetSettings.slideLeftButton).click(function(){
	    pauseSlideWidget($(this).parents("div.slideWidgetContainer").attr("id"));
		slidePanels($(this).parents("div.slideWidgetContainer").attr("id"),-1);
return false;
	});
	
	$(slideWidgetSettings.slidePauseButton).click(function(){
	    slideWidgets[$(this).parents("div.slideWidgetContainer").attr("id")].slideWidgetPause=true;
		pauseSlideWidget($(this).parents("div.slideWidgetContainer").attr("id"));
		
		$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/pause_on_3688.gif");
		$(this).siblings("a.slidePlayBtn.scontrol").children("img").attr("src","http://asset1.homesandproperty.co.uk/handp/media/play_off_3689.gif");
	});
	
	$(slideWidgetSettings.slidePlayButton).click(function(){
	    slideWidgets[$(this).parents("div.slideWidgetContainer").attr("id")].slideWidgetPause=false;
		playSlideWidget($(this).parents("div.slideWidgetContainer").attr("id"),10);
		
		$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/play_on_3690.gif");
		$(this).siblings("a.slidePauseBtn.scontrol").children("img").attr("src","http://asset1.homesandproperty.co.uk/handp/media/pause_off_3687.gif");
	});

	
	//assign hover controls
	$("ul.slideWidget li").hover(function(){
		if(!slideWidgets[$(this).parents("div.slideWidgetContainer").attr("id")].slideWidgetPause)
		{
			window.hoveredwidgetpaused = true;
		}
		pauseSlideWidget($(this).parents("div.slideWidgetContainer").attr("id"));
	},
	    function(){
			if(window.window.hoveredwidgetpaused)
			{
	        	playSlideWidget($(this).parents("div.slideWidgetContainer").attr("id"),0);
			}
	    }
	);
	
	
	//kick off the auto scroll (if necessary)
	if(slideWidgetSettings.autoStartSlideWidget) {setTimeout(function(){startAutoSlideWidget(widget.attr("id"));},delay); }


}

function startAutoSlideWidget(widget) {
   slideWidgets[widget].autoSlideWidgetTO= setInterval(function(){slideWidgetAutoScroll(widget);},slideWidgets[widget].autoSlideWidgetInterval); 
}

function slideWidgetAutoScroll(widget) {
    if(!slideWidgets[widget].slideWidgetPause && slideWidgets[widget].autoSlideWidget) {
        slidePanels(widget,+1);
        playSlideWidget(widget,0);
    }
}

function pauseSlideWidget(widget) {
    slideWidgets[widget].slideWidgetPause=true;
}
 
function playSlideWidget(widget,delay) {
      slideWidgets[widget].slideWidgetPause=false;
} 
 
//slide left one panel
function slidePanels(widget,direction) {
    //check if one is animating
	    var isOneAnimated = false;
	    slideWidgets[widget].containerNode.find("ul.slideWidget li").each(function(){
	        if($(this).is(":animated")){
	            isOneAnimated = true;
	          }
	    })

   	if(!isOneAnimated){
   	
          slideWidgets[widget].containerNode.find("ul.slideWidget li").each(function(){ 
    
            //slide left (direction=1)
            if(direction > 0) {
            
                var l=parseInt($(this).css("left"));
          	    var w=parseInt($(this).width())+parseInt($(this).css("margin-right"))+parseInt($(this).css("margin-left"));
                var newLeft=l-w * direction ;
            
                if($(this).is("ul.slideWidget li:first-child")) {
                   $(this).animate({left: newLeft},1000,function(){reOrderLis(widget,direction);});
                } else {
                    $(this).animate({left: newLeft},1000,function(){ $(this).css("left",0);});
                }
              
            }
            
            
             //slide right (direction-1
            if(direction < 0) {
    
                 if($(this).is("ul.slideWidget li:last-child")) {
                        reOrderLis(widget,direction);
                  }
    
                var l=parseInt($(this).css("left"));
          	    var w=parseInt($(this).width())+parseInt($(this).css("margin-right"))+parseInt($(this).css("margin-left"));
              	
              	//re-shuffle positions
              	$(this).css("left",l-w);
              	var newLeft=parseInt($(this).css("left"))+w;
              	
              	//scroll
              	$(this).animate({left: newLeft},1000);
    
    
            } 
       });
 }
}


function reOrderLis(widget,direction) {

      var liToMove;

    //sliding left, remove first li and append to end
      if(direction > 0) {
        liToMove= slideWidgets[widget].containerNode.find("ul.slideWidget li").eq(0);
        slideWidgets[widget].containerNode.find("ul.slideWidget").append(liToMove);
       liToMove.css("left",0); 
      }
     
    //sliding right, remove last li and append to start
      if(direction < 0) {
        liToMove= slideWidgets[widget].containerNode.find("ul.slideWidget li:last-child");
        slideWidgets[widget].containerNode.find("ul.slideWidget").prepend(liToMove);
      } 
      
}


$(function(){
	latchToGalleryEvents();
	//$("div.article-picture.current").fadeOut("normal").fadeIn("normal");
	$("span.total-images").text($("div.article-picture").length);
	
	for(i = 0; i < $("div.article-picture").length; i++) {
		$("div.article-picture").eq(i).data("position",i+1);
	}
	
	resetGalleryZ();
})

//set decreasing z-index per slide
function resetGalleryZ() {
	$("span.current-image").text($("div.article-picture.current").data("position"));
	
	for(i = 0; i < $("div.article-gallery div.article-picture").length; i++) {
		$("div.article-gallery div.article-picture").eq(i).css("z-index",$("div.article-gallery div.article-picture").length - i);
		
		if(i!=0) {
			$("div.article-gallery div.article-picture").eq(i).hide();
		}
	}
}

function latchToGalleryEvents() {
	$("div.article-gallery div.controls a.toggle-ss").click(toggleSlideshow);
	$("div.article-gallery div.controls a.gallery-next,a.image-control.inext").click(function(e){
		e.preventDefault();
		e.stopPropagation();
		
		moveNext();
	});
	
	$("div.article-gallery div.controls a.gallery-back,a.image-control.back").click(function(e){
		e.preventDefault();
		e.stopPropagation();
		
		moveBack();
	});
	
	$("a.gallery-back").hover(function(){
		$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/play_active_03_10612.gif");
	},
	function(){
		$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/play_off_03_10608.gif");
	});
	
	$("a.gallery-next").hover(function(){
		$(this).children("img").attr("src","http://asset1.homesandproperty.co.uk/handp/media/play_active_05_10605.gif");
	},
	function(){
		$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/play_off_05_10604.gif");
	});
	
	$("a.toggle-ss").hover(function(){
		if(!$(this).is(".paws")){
			$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/HP_Galleryon_07_10610.gif");
		}
		else {
			$(this).children("img").attr("src","http://asset1.homesandproperty.co.uk/handp/media/play_active_07_10611.gif");
		}
	},
	function(){
		if(!$(this).is(".paws")){
			$(this).children("img").attr("src","http://asset2.homesandproperty.co.uk/handp/media/pause_off_07_10606.gif");
		}
		else {
			$(this).children("img").attr("src","http://asset1.homesandproperty.co.uk/handp/media/play_off_07_10607.gif");
		}
	});
	
	
}

function moveNext() {
	$("div.article-picture.current").fadeOut("normal",function(){
		$(this).appendTo($("div.article-gallery"));
		
		$("div.article-picture.current").removeClass("current");
		
		$("div.article-picture:first").addClass("current");
		
		resetGalleryZ();
		
		$("div.article-picture.current").fadeIn("normal");
	})
}

function moveBack() {
	$("div.article-gallery div.article-picture:last").css("z-index",$("div.article-gallery div.article-picture").length).insertAfter("div.article-gallery div.article-picture:first");
	$("div.article-picture.current").fadeOut("normal",function(el){
		$("div.article-picture.current").insertAfter($("div.article-picture").eq(1)).removeClass("current");
		
		
		$("div.article-picture:first").addClass("current");
		resetGalleryZ();
		
		$("div.article-picture.current").fadeIn("normal");
	})
}

function toggleSlideshow() {
	if(window.slideshowPlaying) {
		window.clearTimeout(window.slideshowPlaying);
		window.slideshowPlaying = false;
		$("div.controls a.toggle-ss").removeClass("play");
		$("div.controls a.toggle-ss").addClass("paws");
		$("div.controls span.toggle-text").text("PLAY");
		$("div.controls a.toggle-ss img").attr("src","/media/images/play_off_07_10607.gif")
	}
	else {
		moveNext();
		window.slideshowPlaying = window.setInterval(moveNext,5000);
		$("div.controls a.toggle-ss").removeClass("paws");
		$("div.controls a.toggle-ss").addClass("play");
		$("div.controls span.toggle-text").text("PAUSE");
		$("div.controls a.toggle-ss img").attr("src","/media/images/pause_off_07_10606.gif")
	}
}

