$(document).keypress(function(e) {  
//code to close #popdiv of escape keypress
    if (e.which == 27) { 
		$("#pagefade").stop().fadeTo(300, 0).hide();
		$("#popdiv-cont").stop().fadeTo(100, 0).hide();
    } 
}); 




$(document).ready(function(){

//code to close #popdiv
	$("#popdiv .close").click(
		function () {
			$("#pagefade").stop().fadeTo(300, 0).hide();
			$("#popdiv-cont").stop().fadeTo(100, 0).hide();
		}
	);	
						   
//code to expand collapsed client testimonies into #popdiv and subsequently display
	$(".collapsed .client-testimony").click(
		function () {
			$("#popdiv-content").html( $(this).children(".quote").html() );
			$("#pagefade").stop().fadeTo(300, 0.25);
			$("#popdiv").css({'width' : '500px','top' : '300px'});
			$("#popdiv-cont").stop().fadeTo(100, 1);
		}
	);	

//code to populate #popdiv with .hyperpop content
	//focus does not work on webkit browsers - use mouseup() alternative
	$(".hyperpop").mouseup(
		function () {
			$("#popdiv").css({'width' : $(this).children(".hyperpop-content").css('width'),'top' : $(this).children(".hyperpop-content").css('top')});
			$("#popdiv-content").html( $(this).children(".hyperpop-content").html() );
			$("#pagefade").stop().fadeTo(300, 0.25);
			$("#popdiv-cont").stop().fadeTo(100, 1);
		}
	);
	
	$(".hyperpop").focusin(
		function () {
			$("#popdiv").css({'width' : $(this).children(".hyperpop-content").css('width'),'top' : $(this).children(".hyperpop-content").css('top')});
			$("#popdiv-content").html( $(this).children(".hyperpop-content").html() );
			$("#pagefade").stop().fadeTo(300, 0.25);
			$("#popdiv-cont").stop().fadeTo(100, 1);
		}
	);

						   
//code for timeline content expand on hover
	$(".sliderGallery ul li").hover(
		function () {
			$(this).children(".hov-slide").stop(true, true).fadeIn();
		},
		function () {
			$(this).children(".hov-slide").stop(true, true).fadeOut();
		}
	);	

						   
//code for process vertical sliding panels
	$("#submit").mouseup(
		function() {
			var counter=0;
			$("#track-cont, #receive-cont").stop(true, true).slideUp(
				function(){
					counter++;
					if(counter==2)
					{
						$("#submit-cont").stop(true, true).slideToggle();
					}
				})
		}
	);
	$("#track").mouseup(
		function() {
			var counter=0;
			$("#submit-cont, #receive-cont").stop(true, true).slideUp(
				function(){
					counter++;
					if(counter==2)
					{
						$("#track-cont").stop(true, true).slideToggle();
					}
			})
		}
	);
	$("#receive").mouseup(
		function() {
			var counter=0;
			$("#submit-cont, #track-cont").stop(true, true).slideUp(
				function(){
					counter++;
					if(counter==2)
					{
						$("#receive-cont").stop(true, true).slideToggle();
					}
			})
		}
	);


//code for slider controlled panel
	$('div.sliderGallery').each(function () {
		var ul = $('ul', this);
		var productWidth = ul.innerWidth() - $(this).outerWidth();
		
	
		var slider = $('#slider', this).slider({ 
		  min: 0, 
		  max: productWidth, 
		  slide: function (event, ui) {
		  //alternate - no scrolling		ul.css('left', '-' + ui.value + 'px');
			ul.stop(true, false).animate({ 'left' : '-' + ui.value + 'px' }, 500);
		  }, 
		  change: function (event, ui) {
		  ul.stop(true, false).animate({ 'left' : '-' + ui.value + 'px' }, 500);
		  }
		});
	});	
		
	
//code for jquery ui accordian
	$(function() {
			   
		var icons = {
			header: "custom-icon",
			headerSelected: "custom-icon-selected"
		};

		$( "#accordion" ).accordion({
			collapsible: true,
			active: false,
			autoHeight: false,
			navigation: true,
			icons: icons
		});
	});

//code for client items scroller
	$("#client img.control-arrow").hover(
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-std.png/, "-hov.png"));
		},
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-hov.png/, "-std.png"));
		}
	);	
	$("#client #left-control").click(
		function () {
			$('#client-items').animate({left: Math.min( -15, -15 + (885+30) + (885+30) * Math.ceil(($('#client-items').position().left + 15) / (885+30)) ) + 'px'}, 800);
		}
	);	
	$("#client #right-control").click(
		function () {
			$('#client-items').animate({left: Math.max( -(885+30+15), -15 -(885+30) + (885+30) * Math.floor(($('#client-items').position().left + 15) / (885+30)) ) + 'px'}, 800);
		}
	);
	
	
//code for featured items scroller
	$("#featured img.control-arrow").hover(
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-std.png/, "-hov.png"));
		},
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-hov.png/, "-std.png"));
		}
	);	
	$("#featured #left-control").click(
		function () {
			$('#featured-items').animate({left: Math.min( -7, -7 + 249 + 249 * Math.ceil(($('#featured-items').position().left + 7) / 249) ) + 'px'}, 500);
		}
	);	
	$("#featured #right-control").click(
		function () {
			$('#featured-items').animate({left: Math.max( -505, -7 - 249  + 249 * Math.floor(($('#featured-items').position().left + 7) / 249) ) + 'px'}, 500);
		}
	);	

						   
//code for banner image sliding panel
	$("#banner-slider-controlarrow").hover(
		function() {
			$("#banner-slider-content").stop(true, true).slideDown(1000);		
		},
		function() {	
		}
	);
	$("#banner-slider").hover(
		function() {	
		},
		function() {	
			$("#banner-slider-content").slideUp(750);	
		}
	);

//code for navigation image swaps
	$("#nav ul li a").each(function() {
	if(this.href == window.location || this.href == document.location.protocol + "//" + window.location.hostname + window.location.pathname)
		$(this).parent("li").addClass("active");
	});
	
	$("#nav li:not(.active) img").hover(
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-std.jpg/, "-hov.jpg"));
		},
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-hov.jpg/, "-std.jpg"));
		}
	);	
	
	$("#nav li.active img").each(function() {
		$(this).attr("src", $(this).attr("src").replace(/-std.jpg/, "-act.jpg"));
	});
		



//code for fading images	
    var counter = 1, 
	
    divs = $('.fader1, .fader2, .fader3'); 
 
    function showDiv () {  // function to loop through divs and show correct div 

        divs.filter(function (index) { return index == counter % 3; }) // figure out correct div to show 
			.hide()
			.css({'z-index': (counter + 100)})
			.fadeTo(1000, 1)
			.show(); // and show it	 
			
			
        counter++
    }; 


    setInterval(function () { 
		showDiv() // show next div
    }, 7 * 1000) // do this every x seconds    
	
	
	
//code for submenu (horizonal region slide)
	$('#subnav ul li').focusin(		
		function () {
			var listindex = $('#subnav ul li').index(this);
			$('#subnav-target').stop().animate({left: -800 * listindex + 'px'}, 1000);
			$('#subnav ul li').removeClass("active");
			$(this).addClass("active");
			
			
			$("#subnav li.active img").each(function() {
				$(this).attr("src", $(this).attr("src").replace(/-std.jpg/, "-act.jpg"));
				$(this).attr("src", $(this).attr("src").replace(/-hov.jpg/, "-act.jpg"));
			});
			
			$("#subnav li:not(.active) img").each(function () {
					$(this).attr("src", $(this).attr("src").replace(/-act.jpg/, "-std.jpg"));
			});	
			
			//collapse accordian in visible area (and all other areas)
			$( "#accordion" ).accordion( "activate" , false );
		}
	);
	
	//focus does not work on webkit browsers - use mouseup() alternative
	$('#subnav ul li').mouseup(		
		function () {
			var listindex = $('#subnav ul li').index(this);
			$('#subnav-target').stop().animate({left: -800 * listindex + 'px'}, 1000);
			$('#subnav ul li').removeClass("active");
			$(this).addClass("active");
			
			
			$("#subnav li.active img").each(function() {
				$(this).attr("src", $(this).attr("src").replace(/-std.jpg/, "-act.jpg"));
				$(this).attr("src", $(this).attr("src").replace(/-hov.jpg/, "-act.jpg"));
			});
			
			$("#subnav li:not(.active) img").each(function () {
					$(this).attr("src", $(this).attr("src").replace(/-act.jpg/, "-std.jpg"));
			});	
			
			//collapse accordian in visible area (and all other areas)
			$( "#accordion" ).accordion( "activate" , false );
		}
	);
	

//code for sub-navigation image swaps	
	$("#subnav li.active img").each(function() {
		$(this).attr("src", $(this).attr("src").replace(/-std.jpg/, "-act.jpg"));
		$(this).attr("src", $(this).attr("src").replace(/-hov.jpg/, "-act.jpg"));
	});			
	
	$("#subnav li:not(.active) img").hover(
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-std.jpg/, "-hov.jpg"));
		},
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-hov.jpg/, "-std.jpg"));
		}
	);	

	$("#subnav li.active img").hover(
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-act.jpg/, "-hov.jpg"));
		},
		function () {
			$(this).attr("src", $(this).attr("src").replace(/-hov.jpg/, "-act.jpg"));
		}
	);	
	

//code for lightbox effect when hovering over class .hovpagefade - requires non-static element
	var thiszindex;
	$('.hovpagefade').hover(		
		function () {
			thiszindex = $(this).css("z-index");
			$(this).css({'z-index': 5000}); 
			$('#pagefade').stop().fadeTo(300, 0.2);
		},
		function () {
			$('#pagefade').stop().fadeTo(300, 0).hide();
			$(this).css({'z-index': thiszindex});
		}
	); 

//code for b&w -> colour image transition on hover
	$(".imghvr").show(function() { 

		var imgOver = $(this).find("img").attr("src"); //Get image url and assign it to 'imgOver'

		//Set a background image (imgOver)
		$(this).css({'background' : 'url(' + imgOver + ') no-repeat'});
		$(this).attr("style", $(this).attr("style").replace(/-std.jpg/, "-hov.jpg"));
	});

	$(".imghvr").hover(function() { //On hover...

		//Animate the image to 0 opacity (fade it out)
		$(this).css('opacity', '1'); // fix IE bug
		$(this).find("img").stop().fadeTo('normal', 0 , function() {
			  $(this).hide() //Hide the image after fade
		});
	} , function() { //on hover out...
		//Fade the image to full opacity 
		$(this).find("img").stop().fadeTo('normal', 1).show()
	});
	

});

