var allPicsArray=new Array(
'#pic1', '#pic2', '#pic3', '#pic4', '#pic5', '#pic6', '#pic7', '#pic8', '#pic9', '#pic10',
'#pic11', '#pic12', '#pic13', '#pic14', '#pic15', '#pic16', '#pic17', '#pic18', '#pic19', '#pic20',
'#pic21', '#pic22', '#pic23', '#pic24', '#pic25', '#pic26', '#pic27', '#pic28', '#pic29', '#pic30',
'#pic31', '#pic32', '#pic33', '#pic34', '#pic35', '#pic36', '#pic37', '#pic38', '#pic39', '#pic40',
'#pic41', '#pic42', '#pic43', '#pic44', '#pic45', '#pic46', '#pic47', '#pic48', '#pic49', '#pic50',
'#pic51', '#pic52', '#pic53', '#pic54', '#pic55', '#pic56', '#pic57', '#pic58', '#pic59', '#pic60',
'#pic61', '#pic62', '#pic63', '#pic64', '#pic65', '#pic66', '#pic67', '#pic68', '#pic69', '#pic70',
'#pic71', '#pic72', '#pic73', '#pic74', '#pic75', '#pic76', '#pic77');

var photoarray=new Array();

var windowWidth = $(window).width();
var windowHeight = $(window).height();
var productPopupWidth = windowWidth*0.8;
var productPopupHeight = windowHeight*0.75;

var slidePopupWidth = 400;
var slidePopupHeight = 300;
var picPopupWidth = 800;
var picPopupHeight = 600;

var photoindex=0;
var buttonlock=false;
var div_id;
var d=300;
var currentProductPosition = 0;
var currentPicPosition = 0;


function movetoimg(id)
	{
		$(photoarray[id]).fadeIn(500,function()
		{
			buttonlock=false;
		});
	}

function changedetails(productid)
	{
		document.images.plan.src="images/products/"+productid+"Plan.png";
		document.images.writeup.src="images/products/"+productid+"Writeup.png";
		document.images.prodbg.src="images/products/"+productid+"bg.png";

	if(productid=="Ribble")
	{
	loadPicArray(53,55);
	}

	else if(productid=="Hodder")
	{
	loadPicArray(50,52);
	}

	else if(productid=="Langden")
	{
	loadPicArray(56,58);
	}

	else if(productid=="Mearley")
	{
	loadPicArray(59,61);
	}
	
	}

function loadPicArray(firstPic, lastPic)
	{
	var count=(lastPic-firstPic+1);
	var j=0;

for (i=firstPic;i<=lastPic;i++)
{
photoarray[j]=allPicsArray[i];
j++;
}

	}

function changepic(imagesrc)
	{
		document.images.slide.src=imagesrc;
	}

function launchProductSlideshow()
	{
		movetoimg(photoindex);
		$("#slideshowPopup").css
		({
		"position": "absolute", 
		"top": windowHeight/2-slidePopupHeight/2,
		"left": windowWidth/2-slidePopupWidth/2
		});

		$("#slideshowPopupBackground").css
		({
		"opacity": "0.8"
		});
		$("#slideshowPopupBackground").fadeIn(1500,function()
			{
			$("#slideshowPopup").fadeIn(1000);
			});

		$("#slideshowPopupClose").click(function()
		{

			$(photoarray[photoindex]).fadeOut(500);
			$("#slideshowPopup").fadeOut(500,function(){
			$("#slideshowPopupBackground").fadeOut(500);
			});
		photoarray.length=0;
		photoindex=0;	
			
		});
	}

function launchTrimSlideshow()
	{
	loadPicArray(0,49);
	launchProductSlideshow();
	}

function launchShelterSlideshow()
	{
	loadPicArray(62,69);
	launchProductSlideshow();
	}
function launchSensorySlideshow()
	{
	loadPicArray(70,76);
	launchProductSlideshow();
	}


function manageProductControls(productPosition)
	{
		if(productPosition==0){ $('#productLeftControl').hide() } else{ $('#productLeftControl').show() }
		if(productPosition==$('.productSlide').length-1){ $('#productRightControl').hide() } else{ $('#productRightControl').show() }
	}

function toggle_visibility(divid)
	{

	for (var i=1; i<6; i++)
	{
		div_id = "div" + i;
		if (div_id == divid)
$("#"+divid).fadeIn(2000);

		else
		document.getElementById (div_id).style.display = "none";
	}
	}


$(document).ready(function(){

$('#prev').click(function(){
	if(buttonlock) return;
	else buttonlock=true;
	$(photoarray[photoindex]).fadeOut(500,function(){
		photoindex--;
		if(photoindex<0) photoindex=photoarray.length-1;
		movetoimg(photoindex);
	});
});

$('#next').click(function(){
	if(buttonlock) return;
	else buttonlock=true;
	$(photoarray[photoindex]).fadeOut(500,function(){
		photoindex++;
		if(photoindex>photoarray.length-1) photoindex=0;
		movetoimg(photoindex);
	});
});
	
	

$('#navigation a').each(
	function()
		{
		$(this).stop().animate({'marginTop':'-80px'},d+=1000);
		});

$('#navigation > li').hover(
	function()
		{
		$('a',$(this)).stop().animate({'marginTop':'-2px'},350);
		},
	function()
		{
		$('a',$(this)).stop().animate({'marginTop':'-80px'},200);
		}
		);
	


$('.productSlide')
.wrapAll('<div id="productSlideInner"></div>')
.css({'float' : 'left','width' : 900});
$('#productSlideInner')
.css('width', 900 * $('.productSlide').length);

manageProductControls(currentProductPosition);						
$('.productControl').bind('click', function(){currentProductPosition = ($(this).attr('id')=='productRightControl') ? currentProductPosition+1 : currentProductPosition-1;
manageProductControls(currentProductPosition);
$('#productSlideInner').animate({'marginLeft' : 900*(-currentProductPosition)});});


























	$(".image").click(function()
	{
		$("#picPopup").css
		({
			"position": "absolute", 
			"top": windowHeight/2-picPopupHeight/2,
			"left": windowWidth/2-picPopupWidth/2
		});

		$("#picPopupBackground").css
		({
			"opacity": "0.8"
		});

		$("#picPopupBackground").fadeIn(1500,function()
		{
			$("#picPopup").fadeIn(1000);
		});
		
	});
				
	$("#picPopupClose").click(function()
	{
		$("#picPopup").fadeOut(500,function()
		{
			$("#picPopupBackground").fadeOut(500);
		});
	});

	$(".product").click(function()
	{
		$("#productPopup").css
		({
			"position" : "absolute",
			"width" : productPopupWidth,
			"height" : productPopupHeight,
			"top" : windowHeight/2-productPopupHeight/2,
			"left" : windowWidth/2-productPopupWidth/2
		});

		$("#productWriteup").css
		({
			"position" : "absolute",
			"left" : "0px"
		});

		$("#productPlan").css
		({
			"position" : "absolute",
			"bottom" : "0px",
			"right" : "0px"
		});

		$("#productPopupBackground").css
		({
			"opacity" : "0.8"
		});

		$("#productPopupBackground").fadeIn(1500,function()
		{
			$("#productPopup").fadeIn(1000);
		});
	});
				
	$("#productPopupClose").click(function()
	{
		$("#productPopup").fadeOut(500,function()
		{
			$("#productPopupBackground").fadeOut(500);
			photoarray.length=0;
			photoindex=0;
		});
	});
});
