// New js effects for Process page
function initProcessGallery() {

	$(function() {

		$('#process_gallery').cycle({
			fx:      'fade',
			timeout:  8000,
			prev:    '#prev',
			next:    '#next',
			pagerAnchorBuilder: pagerFactory
		});

		function pagerFactory(idx, slide) {
			var s = idx > 2 ? ' style="display:none"' : '';
			return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
		};
		
		$('#stop').click(function() { 
			$('#process_gallery').cycle('pause'); 
		});
		
		$('#play').click(function() { 
			$('#process_gallery').cycle('resume'); 
		});
		
	});
}
initProcessGallery();



// Language drop-down menu
$(function() {
	$("ul#flags").click(function() {
		$("ul#language_menu").slideToggle("fast");
	});
  
	$("ul#language_menu li a").click(function() {
		$("ul#language_menu").fadeOut("fast");
	});
});



$(function() {
    $('ul#flags').cycle({
        fx:      'fade',
        timeout:  5000
    });

});



function initHomePage() {

	// Sliding story effects on Homepage
	$(function() {

		// Story panel - Set 1
		$("a.find_out_how, .large_thumb a").click(function() {
			//$(".large_thumb a").addClass("disabled");
			$(".large_thumb").addClass("active");
			$(".small_thumb_left").removeClass("active");
			$(".small_thumb_right").removeClass("active");


			$("a.find_out_how").fadeOut();
			$(".main_story_panel.set1").effect("slide", { direction: "right" }, 400);
			$(".main_story_panel.set2:visible, .main_story_panel.set3:visible").fadeOut("fast");
			$(".panel_overlay:hidden, h3.panel_tagline").fadeIn("1");
			
			setTimeout(function() {
				$(".panel_overlay").fadeOut("700");
				$("h3.panel_tagline").fadeOut("700");
			}, 4000); 
		});

		// Story panel - Set 2
		$(".small_thumb_left a").click(function() {

			//$(this).addClass("disabled");
			$(".large_thumb").removeClass("active");
			$(".small_thumb_left").addClass("active");
			$(".small_thumb_right").removeClass("active");

			$("a.find_out_how").fadeOut();
			$(".main_story_panel.set2").effect("slide", { direction: "right" }, 400);
			$(".main_story_panel.set1:visible, .main_story_panel.set3:visible").fadeOut("fast");
			$(".panel_overlay:hidden, h3.panel_tagline").fadeIn("1");
			
			setTimeout(function() {
				$(".panel_overlay").fadeOut("700");
				$("h3.panel_tagline").fadeOut("700");
			}, 4000); 
		});
		
		// Story panel - Set 3
		$(".small_thumb_right a").click(function() {
			$(".large_thumb").removeClass("active");
			$(".small_thumb_left").removeClass("active");
			$(".small_thumb_right").addClass("active");

			//$(this).addClass("disabled");
			$("a.find_out_how").fadeOut();
			$(".main_story_panel.set3").effect("slide", { direction: "right" }, 400);
			$(".main_story_panel.set1:visible, .main_story_panel.set2:visible").fadeOut("fast");
			$(".panel_overlay:hidden, h3.panel_tagline").fadeIn("1");
			
			setTimeout(function() {
				$(".panel_overlay").fadeOut("700");
				$("h3.panel_tagline").fadeOut("700");
			}, 4000); 
		});
		
		
		// Resets all the hidden and faded out elements
		$("ul.innovative_nav li.nav a").click(function() {
			
			$(".large_thumb").removeClass("active");
			$(".small_thumb_left").removeClass("active");
			$(".small_thumb_right").removeClass("active");

			$(".main_story_panel:visible").fadeOut("fast");
			$("#story_wrapper").find(".disabled").removeClass("disabled");
			$("a.find_out_how:hidden").show();
			$(".panel_overlay:hidden, h3.panel_tagline").fadeIn("1");
			$(".main_story").fadeIn("slow");
			$("ul.innovative_nav").find(".selected").removeClass("selected");
			this.className="selected";
		});
	});



	// Scrolling function for projects on the homepage		
	$(function(){
		$("#story_wrapper").serialScroll({
			items:'.main_story',
			next:'a.unavailable',
			axis:'xy',
			navigation:'.innovative_nav li.nav a',
			duration:1,
			force:true
		});
	});

	$(function(){
		$("ul.innovative_nav li.nav a").click(function() {
			clearTimeout(hpTimer);
		});
	});

	$(function(){
		$("ul.innovative_nav li.next a.continue").click(function() { 
			clearTimeout(hpTimer);
			updateHPStories();
			curStory=1;
		});
	});

	$(function(){
		$(".main_story a").click(function() {
			clearTimeout(hpTimer);
		});
	});
}
initHomePage();



// Photo gallery right sidebar
$(function(){
	$("#process #gallery li a").click(function(){
		var largePath = $(this).attr("href");
		var largeAlt = $(this).attr("title");
		
		$("#selected_image img").attr({ src: largePath, alt: largeAlt, title: largeAlt });
		return false;
	});
});



// Project details accordion
$(function() {
	$("#services_detail_categories").accordion({ header: 'h3.accordion_header' });
});



// Expand/collapse button
$(function() {
	$("div.projects_detail_title a.expand_collapse").click(function() {
		$("div.projects_detail_title div.collapsable_content").toggle("slide", { direction: "right" }, 400);
	});
	
	$("div.projects_detail_caption a.expand_collapse").click(function() {
		$("div.projects_detail_caption div.collapsable_content").toggle("slide", { direction: "right" }, 500);
	});
	
	$("div.services_detail_title a.expand_collapse").click(function() {
		$("div.services_detail_title div.collapsable_content").toggle("slide", { direction: "right" }, 400);
	});
	
	$("div.services_detail_caption a.expand_collapse").click(function() {
		$("div.services_detail_caption div.collapsable_content").toggle("slide", { direction: "right" }, 500);
	});
});



// Emergency view
function doEmergency() {
	$("#emergency_window").show();
	$(".emergency_overlay").show();
	$("a.window_close").show();
	 $("#projects embed#rwdi_swf").hide();
}

$(function(){
	$("a#emergency").click(function(){
		doEmergency();
	});
	
	$(".emergency_overlay, a.window_close").click(function(){
		$(".emergency_overlay").hide();
		$("#emergency_window").fadeOut("fast");
		$("a.window_close").hide();
		$("#projects embed#rwdi_swf").show();
	});
});



// Language view
function doLanguage() {
    $("#language_box").show();
    $(".language_overlay").show();
    $("a.window_close").show();
    $("#projects embed#rwdi_swf").hide();
}

$(function(){
    $("ul#language_menu li a").click(function(){
    	doLanguage();
    });
    
    $(".language_overlay, a.window_close").click(function(){
    	$(".language_overlay").hide();
		$("#language_box").fadeOut("fast");
		$("a.window_close").hide();
		$("#projects embed#rwdi_swf").show();
    });
});


// New project thumbnail scroller 
function initGalleryScroller() {

	$(function() {
		
		$('#slideshow').cycle({ 
			fx:     'fade', 
	   		speed:  '300', 
	   		timeout: 0,
	   		after:   onAfter,
	   		pager:  '#gallery', 
	   		prev:  '#prev_button', 
	   		next:  '#next_button',
	   		nowrap: 1, 
		    pagerAnchorBuilder: function(idx, slide) { 
		        // return selector string for existing anchor 
		        return '#gallery li:eq(' + idx + ') a'; 
		    } 
		});
		
		
	     function onAfter(curr, next, opts) {
		     var index = opts.currSlide;
		     var pid = $('#li_'+index).attr("pid");
		     var ptype = $('#gallery').attr("ptype");
		     $('#prev_button')[index == 0 ? 'hide' : 'show']();
		     $('#next_button')[index == opts.slideCount - 1 ? 'hide' : 'show']();
		     
			//if($.browser.msie && !($.browser.version == 6) || $.browser.mozilla || $.browser.safari){
			 	updatePhoto(ptype, pid);
			//}
		 }	

		$('#next_button').click(function(){
			 $("#gallery_wrapper").stop().scrollTo( '+=65', 500, {axis:'x'} );
		});
		
		$('#prev_button').click(function(){
			 $("#gallery_wrapper").stop().scrollTo( '-=65', 500, {axis:'x'} );
		});

	// Calculate the width of the ul#gallery
	    var galleryChildren = $('#gallery').children().size();
	    var itemWidth =  65;
	    var galleryWidth = galleryChildren * itemWidth;	
	    			
	    $('ul#gallery').css('width', galleryWidth);
	});

}
initGalleryScroller();


// Experts page
$(function(){
	$("ul#experts_list li a").click(function(){
		$("#experts #content_left").animate( { width:"585px"}, "fast");
		$("#experts #content_left #experts_list_wrapper").animate( { width:"585px"}, "fast");
		$("#experts #content_left ul#experts_list").animate( { width:"567px"}, "fast");
		$("#experts #content_right").fadeIn().animate( { width:"269px"}, "fast");
	});
});


function initFancyBox() {
	// Fancybox for left sidebar
	$(function(){
		$("#selected a, #thumbnails a, ul#otb_projects li a").fancybox({
			'overlayShow': true,
			'hideOnContentClick': false,
			'overlayOpacity': 0.6
		}); 
	});
}
initFancyBox();

// Photo gallery in left sidebar
$(function(){
	$("ul#thumbnails li a").hover(function(){
		var largePath = $(this).attr("href");
		var largeThumb = $(this).children().attr("src");
		var largeAlt = $(this).attr("title");
		var largeCaption = $(this).attr("title");
		
		$("#selected img").attr({ src: largeThumb, alt: largeAlt, title: largeAlt });
		$("#selected a").attr({ href: largePath });
		
		$("#selected p.caption").html(" " + largeCaption + " "); 
		return false;
	});
});