// JavaScript Document
var browserName=navigator.appName; 
function isIE()
{
    if(navigator.userAgent.match(/MSIE \d\.\d+/))
        return true;
    return false;
}

$(function() {
	if(isIE()) {
		var zIndexNumber = 100000;
		$('.header').children().each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	}
});
$(document).ready(function(){
	$(".header_wrap .get_a_quote_wrap").css({"z-index": "100000"});
	$(".btn-get_a_quote").click(function() {
		$(".get_a_quote_wrap").stop(true, true).slideToggle();								 
	});
	
	$(".btn-submit_quote").click(function() {
		validated=true;
		$(".submit_quote_form").find(".required_text").each(function() {
			$(this).removeClass("missing");
			if($(this).val()=="") {
				$(this).addClass("missing");
				validated=false;
			}
		});
		
		$(".submit_quote_form").find(".required_email").each(function() {
			$(this).removeClass("missing");
			if(!validate_email($(this).val())) {
				$(this).addClass("missing");
				validated=false;
			}
		});
		
		if(validated)
			$(".submit_quote_form").submit();
	});
	
	$("#slider .feature_box img").each(function() {
		$(this).css({opacity: 0.5});
	});
	
	$("#slider .feature_box a").hover(
		function () {
			$(this).find("img").stop(true, true).animate({
				opacity: 1
			}, 250);
		}, 
		function () {
			$(this).find("img").animate({
				opacity: 0.5
			}, 250);
		}
	);
						   
	if($('#footer').attr("class") == "ie_hidden")
		$('#footer').removeClass("ie_hidden")
	if (browserName!="Microsoft Internet Explorer")
	{			   
		$(".topnav").hide();
		$(".page_wrap").hide();
		$("#slides").hide();
		$("#footer").hide();
		$('.topnav').fadeIn(1000);
		$('.page_wrap').fadeIn(1000);
		$("#slides").fadeIn(1000);
		$("#footer").fadeIn(1000);
	}
	else
	{
		$(".page_wrap").show();
	}
				   
	$("a").click(function() {
		var the_src = $(this).attr("href");
		var the_class = $(this).attr("class");
		if(the_src=="#")
			return false;
			
		if(the_class == "new_win") {
			window.open(the_src)
			return false;
		}
		if(the_class == "read-more going_on_cbox") {
			return false;
		}
		
		if(the_class == "nolink") {
			return false;
		}
			
		if(the_src.substr(0,6) == "mailto")
				return true;
		
		if(the_class == "pop_case_study cboxElement")
			return true;
		
		if(the_class == "btn-submit_quote")
			return true;
		
			
		if (browserName=="Microsoft Internet Explorer")
		{
			window.location = the_src;
			return false
		}
		
		$(".topnav").fadeOut(500);
		$("#slides").fadeOut(500);
		$("#footer").fadeOut(500);
		$('html, body').animate({ scrollTop: 0 }, 400);
		$(".page_wrap").fadeOut(500, function () {
  			window.location = the_src;
  		});
		return false;
	});			   
						   
	$("#myController").jFlow({
		slides: "#slides",
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		auto: true,		//auto change slide, default true
		width: "967px",
		height: "250px",
		duration: 1000,
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext" // must be class, use . sign
	});
	
	$('a.slick-slidetoggle-gallery').click(function() {

		$(this).parent().next().find(".hover_block").children().each(function() {
			$(this).hide();
		});
		
		var isVisible = $(this).parent().next().find(".hover_block").is(':visible');
		
		if(isVisible)
		{
			var fade_time = 400;
			var start_time = 0;
			var split_time = 50;
			
			$(this).parent().next().find(".hover_block").children().each(function() {
				$(this).hide();
			});

		}
		else
		{
			var fade_time = 600;
			var start_time = 800;
			var split_time = 100;
			$(this).parent().next().find(".hover_block").show();
			$(this).parent().next().find(".hover_block").children().each(function() {
				var class_name = "."+$(this).attr("class");
				setTimeout( function(){$(class_name).fadeIn(fade_time);}, start_time);	
				start_time += split_time;
			});
		}
		
		var the_pos = $(this).offset().top;
		$(this).parent().next().slideToggle(400, function() {											  
			if($(document).height()<$(window).height()+the_pos)
				the_pos = $(document).height()-$(window).height();
				$('html, body').animate({ scrollTop: the_pos }, 400);
		
		});
		return false;
	});
		
	$('a.slick-slidetoggle').click(function() {
		if($('.btn_rotate').is(':visible')) {
			$('.iphone_h').children().each(function(){
				$(this).hide();				
			});
		}
		else {
			setTimeout( function(){
				$('.iphone_h').children().each(function(){
					$(this).show();				
				});
			}, 400);	
		}
		var the_pos = $(this).offset().top;
			
		$(this).parent().next().slideToggle(400, function() {										  
			if($(document).height()<$(window).height()+the_pos)
				the_pos = $(document).height()-$(window).height();
				$('html, body').animate({ scrollTop: the_pos }, 400);											  
		});
		return false;
	});
	
	$('.slickbox_head').click(function() {
		if($('.btn_rotate').is(':visible')) {
			$('.iphone_h').children().each(function(){
				$(this).hide();				
			});
		}
		else {
			setTimeout( function(){
				$('.iphone_h').children().each(function(){
					$(this).show();				
				});
			}, 400);	
		}
		
		if($(this).find('.slick-slidetoggle-gallery').attr("class") == "slick-slidetoggle-gallery")
		{
			$(this).next().find(".hover_block").children().each(function() {
				$(this).hide();
			});
			
			var isVisible = $(this).next().find(".hover_block").is(':visible');
			
			if(isVisible)
			{
				var fade_time = 400;
				var start_time = 0;
				var split_time = 50;
				
				$(this).next().find(".hover_block").children().each(function() {
					$(this).hide();
				});
	
			}
			else
			{
				var fade_time = 600;
				var start_time = 800;
				var split_time = 100;
				$(this).next().find(".hover_block").show();
				$(this).next().find(".hover_block").children().each(function() {
					var class_name = "."+$(this).attr("class");
					setTimeout( function(){$(class_name).fadeIn(fade_time);}, start_time);	
					start_time += split_time;
				});
			}
			
			var the_pos = $(this).offset().top;
			
			if($(this).children().find('div').attr("class") == "btn-close")
			{
				$(this).children().find('div').html("click to open")
				$(this).children().find('div').attr("class", "btn-open")
			}
			else
			{
				$(this).children().find('div').html("click to close")
				$(this).children().find('div').attr("class", "btn-close")
			}
			
			$(this).next().slideToggle(400, function() {	
				if($(document).height()<$(window).height()+the_pos)
					the_pos = $(document).height()-$(window).height();
				
				$('html, body').animate({ scrollTop: the_pos }, 400);
			});
			return false;
		}
		
		var the_pos = $(this).offset().top;

		if($(this).children().find('div').attr("class") == "btn-close")
		{
			$(this).children().find('div').html("click to open")
			$(this).children().find('div').attr("class", "btn-open")
		}
		else
		{
			$(this).children().find('div').html("click to close")
			$(this).children().find('div').attr("class", "btn-close")
		}
		$(this).next().slideToggle(400, function() {										  
			if($(document).height()<$(window).height()+the_pos)
				the_pos = $(document).height()-$(window).height();
			
			$('html, body').animate({ scrollTop: the_pos }, 400);	
		});
		return false;
	});
	
	jQuery(function($) {
		var slide = false;
		var height = $('#footer_content').height();
		$('#footer_button').click(function() {
			var docHeight = $(document).height();
			var windowHeight = $(window).height();
			var scrollPos = docHeight - windowHeight + height;
			
			if($('#footer_content').is(':visible'))
				$('#footer_content').slideUp({duration: 1000, easing: 'easeOutBounce'});
			else
				$('#footer_content').slideDown({duration: 1300, easing: 'easeOutElastic'});
			return false;
		});
	});
	
	$(".pop_case_study").colorbox({innerWidth:"800", innerHeight:"550", iframe:true, opacity: 0.75});
	
	$(function() {
		$('ul.hover_block li').hover(function(){
			$(this).find('img').stop(true, true).animate({top:'112px'},{queue:false,duration:500});
		}, function(){
			$(this).find('img').stop(true, true).animate({top:'0px'},{queue:false,duration:1000, easing: 'easeOutElastic'});
			
		});
	});
	
	$("ul.subnav").parent().append("<span></span>"); 

	$("ul.topnav li").hover(function() {
		$(this).find(".subnav").stop(true, true).slideDown({duration: "fast"}).show();
		},
		function () {$(this).find(".subnav").slideUp({duration: 1000, easing: 'easeOutBounce'});
		}
	);
	
	$(".subnav li").mouseover(function(){ 
		$(this).stop().addClass($(this).attr("id")+"-subnav").animate({paddingLeft:'10px',width:"210px"},{queue:false, duration:100})
	}),
	$(".subnav li").mouseout(function(){
		$(this).stop().removeClass($(this).attr("class")).addClass($(this).attr("id")).animate({paddingLeft:'0px',width:"200px"},{queue:false, duration:600, easing: 'easeOutBounce'})
	});
	
	$('.show_join_mailing_list').click(function() {
		if($('.mailing_list_div').is(':visible'))
			$('.mailing_list_div').slideUp("slow");
		else
			$('.mailing_list_div').slideDown("slow", function() {
				setTimeout( function() { 
				  $("#mailing_email").select(); 
				} , 100); 	
				var the_pos = $('.contact_forms').offset().top;
				if($(document).height()<$(window).height()+the_pos)
					the_pos = $(document).height()-$(window).height();
					$('html, body').animate({ scrollTop: the_pos }, 400);
			});
		$('.enquiry_div').slideUp("slow");									
	});
	
	$('.show_send_enquiry').click(function() {
		if($('.enquiry_div').is(':visible'))
			$('.enquiry_div').slideUp("slow");
		else
			$('.enquiry_div').slideDown("slow", function() {
				setTimeout( function() { 
				  $("#enquiry_name").select(); 
				} , 100); 
				var the_pos = $('.contact_forms').offset().top;
				if($(document).height()<$(window).height()+the_pos)
					the_pos = $(document).height()-$(window).height();
					$('html, body').animate({ scrollTop: the_pos }, 400);
			});
		$('.mailing_list_div').slideUp("slow");	
	});
	
	$(window).scroll(function(){ 
		if($("#colorbox").css("display") != "none") {
			$.colorbox.resize({innerWidth: $("#colorbox").width()-10, innerHeight: $("#colorbox").height()-10})
		}
	});
	
	$('.iphone_h, .btn_rotate').click(function() {
		if (browserName=="Microsoft Internet Explorer") {
			if($(".iphone_h").css("margin-right") == "10px") {
				$('.iphone_h').stop().animate({marginRight: "140px"}, 500, function(){
					$('.iphone_wrap').attr("src",  "images/iphone-h.png");	
					$('.iphone_h').find('.iphone_content').animate({width: '182px', height: '121px', top: '13px', left: '47px'}, 0, function(){
						$('.iphone_h').find('.btn_rotate').attr("src",  "images/btn-rotate-h.png");
						$('.iphone_h').find('.iphone_content').attr("src",  "images/iphone_content_h.jpg");
						
					});	
				});
			}
			else if($(".iphone_h").css("margin-right") == "140px") {
				$('.iphone_wrap').attr("src",  "images/iphone.png");
				$('.iphone_h').find('.iphone_content').animate({width: '121px', height: '182px', top: '47px', left: '13px'}, 0, function(){
					$('.iphone_h').find('.btn_rotate').attr("src",  "images/btn-rotate.png");
					$('.iphone_h').find('.iphone_content').attr("src",  "images/iphone_content_v.jpg");
					
				});	
				$('.iphone_h').stop().animate({marginRight: "10px"}, 500);
			}
			return
		}								   
		if($(".iphone_h").css("margin-right") == "10px") {
			$('.iphone_h').stop().animate({rotate: '90deg',marginRight: "80px",marginLeft: "100px",marginTop: "-40px"}, 500, function(){
				$('.iphone_h').find('.iphone_content').animate({rotate: '-45deg', width: '75px', height: '50px', top: '97px', left: '40px'}, 250);	
				$('.iphone_h').find('.iphone_content').attr("src",  "images/iphone_content_h.jpg");
				$('.iphone_h').find('.iphone_content').animate({rotate: '-90deg', width: '182px', height: '121px', top: '80px', left: '-18px'}, 250, function(){
					$('.iphone_h').find('.btn_rotate').attr("src",  "images/btn-rotate-h.png");
				});	
			});	
		}
		else if($(".iphone_h").css("margin-right") == "80px") {
			$('.iphone_h').stop().animate({rotate: '0deg',marginLeft: "20px",marginRight: "10px",marginTop: "0px"}, 500, function(){
				$('.iphone_h').find('.iphone_content').animate({rotate: '-45deg', width: '50px', height: '65px', top: '97px', left: '50px'}, 250);
				$('.iphone_h').find('.iphone_content').attr("src",  "images/iphone_content_v.jpg");
				$('.iphone_h').find('.iphone_content').animate({rotate: '0deg', width: '121px', height: '182px', top: '47px', left: '13px'}, 250, function(){
					$('.iphone_h').find('.btn_rotate').attr("src",  "images/btn-rotate.png");
				});
			});	
		}
	});
});
function validate_email(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
		return false;
	}
	return true;
}
