// JavaScript Document

$(document).ready(function(){
	// Gallery switcher
	
	if($('.galleryContainer').length > 0){
		//$('.galleryContainer').hide();
		//$('.galleryContainer:first').show();
	}
	
	// Banner image rotation
	if($('#bannerArea').length > 0){
		
		var images = $('ul.banners li');
		$.each(images, function(index, value){
			if($(this).attr('title')){
				$(this).css('background', 'url('+$(this).attr('title')+') no-repeat left top');
			}
		});
		imageCount = images.length;
		$('ul.banners li').css('opacity', 0).css('display', 'block');
		$('ul.banners li:first').css('opacity', 1);
		if(imageCount > 1){
			imageIndex = 0;
			setTimeout("rotateBannerImages()",3000);
		}
	}
	
	
	//Nav icon rollover
	$('#nav li a').hover(function(){
		var id = $(this).parent().attr('id').substr(4);
		$('#icon_'+id+' img').show();
	}, function(){
		var id = $(this).parent().attr('id').substr(4);
		$('#icon_'+id+' img').fadeOut('fast');
		
	});

	if($('#case_study_images').length > 0){
		repeat = true;
		$('#case_study_images li').animate({opacity: 0},5);
		$('#case_study_images li:first').animate({opacity: 1},5);
		// animateHomepage
		setTimeout("animateHomepage()",3000);
		$('#case_study_select li, #case_studies li div.thumb span').click(function(){
			repeat = false;	
			if (typeof foo !== 'undefined') {
				clearTimeout(foo);
			}
			homepageCaseStudy($(this));																					
		});
	}
	
	// OUR SERVICES TAB
	if($('#tabs li ').length > 0){
		var completeRefurb = $('ul#tab_content li#tab_content_refurb').height();
		var internal = $('ul#tab_content li#tab_content_partitioning').height();
		var mezzanine = $('ul#tab_content li#tab_content_floors').height();
		
		$('#tabs li').click(function(){
			var currentLi = $(this);
				if($('.gallery_'+currentLi.attr('id')).length > 0){
					$('.galleryContainer').hide(1, function(){
						$('.gallery_'+currentLi.attr('id')).show();
					});
				}
			$('#tabs li').removeClass('on');								 
			currentLi.addClass('on');
			if(currentLi.attr('id') == 'refurb'){
				$('#box_our_services').animate({height: completeRefurb},500);
				$('ul#tab_content li#tab_content_refurb').fadeIn();
				$('ul#tab_content li#tab_content_partitioning').fadeOut();
				$('ul#tab_content li#tab_content_floors').fadeOut();
			}
			if(currentLi.attr('id') == 'partitioning'){
				$('#box_our_services').animate({height: internal},500);
				$('ul#tab_content li#tab_content_refurb').fadeOut();
				$('ul#tab_content li#tab_content_partitioning').fadeIn();
				$('ul#tab_content li#tab_content_floors').fadeOut();
			}
			if(currentLi.attr('id') == 'floors'){
				$('#box_our_services').animate({height: mezzanine},500);
				$('ul#tab_content li#tab_content_refurb').fadeOut();
				$('ul#tab_content li#tab_content_partitioning').fadeOut();
				$('ul#tab_content li#tab_content_floors').fadeIn();
			}			
			return false;
		});
	}
	
	// OUR SERVICES TAB
	if($('#tabsWhy li').length > 0){
		var whyKube = $('ul#tab_content li#tab_content_why_kube').height();
		var finance = $('ul#tab_content li#tab_content_finance').height();
		var meetTheTeam = $('ul#tab_content li#tab_content_meet_the_team').height();
		
		$('#tabsWhy li').click(function(){
			var currentLi = $(this);
			if($('.gallery_'+currentLi.attr('id')).length > 0){
				$('.galleryContainer').hide(1, function(){
					$('.gallery_'+currentLi.attr('id')).show();
				});
			}
			$('#tabsWhy li').removeClass('on');								 
			currentLi.addClass('on');
			if(currentLi.attr('id') == 'whyKube'){
				$('#box_our_services').animate({height: whyKube},500);
				$('ul#tab_content li#tab_content_why_kube').fadeIn();
				$('ul#tab_content li#tab_content_finance').fadeOut();
				$('ul#tab_content li#tab_content_meet_the_team').fadeOut();
			}
			if(currentLi.attr('id') == 'finance'){
				$('#box_our_services').animate({height: finance},500);
				$('ul#tab_content li#tab_content_why_kube').fadeOut();
				$('ul#tab_content li#tab_content_finance').fadeIn();
				$('ul#tab_content li#tab_content_meet_the_team').fadeOut();
			}
			if(currentLi.attr('id') == 'meetTheTeam'){
				$('#box_our_services').animate({height: meetTheTeam},500);
				$('ul#tab_content li#tab_content_why_kube').fadeOut();
				$('ul#tab_content li#tab_content_finance').fadeOut();
				$('ul#tab_content li#tab_content_meet_the_team').fadeIn();
			}			
			return false;
		});
	}
	
	// Map
	
	if($('#map').length > 0){
    var latlng = new google.maps.LatLng(52.010612,-0.2693377);
		
    var myOptions = {
      zoom: 9,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.TERRAIN
    };
		
		
		
		
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
		var image = '/img/icon.png';
    var map = new google.maps.Map(document.getElementById("map"), myOptions); 
    var latlng = new google.maps.LatLng(52.037295,-0.330997); 
		var marker = new google.maps.Marker({
        position: latlng, 
        map: map,
				icon: image,
        title:"Kube Office Interiors Shefford"
    });   
		var latlng = new google.maps.LatLng(51.98162,-0.209234);
		var marker = new google.maps.Marker({
        position: latlng, 
        map: map,
				icon: image,
        title:"Kube Office Interiors Letchworth"
    });   
		
		
		
		//contact form validation
		$('#contactForm').submit(function(){
			var send = true;
			var fields = new Array('ContactCompany','ContactEmail','ContactName' );
			$.each(fields, function(){
				if($('#'+this).val() == ''){
					$('#'+this).focus();
					send = false;
				}			
				if(this == 'ContactEmail'){
					if(!validateEmail($('#'+this).val())){
						$('#'+this).focus();
						send = false;
					}
				}
			});
			if(send == false){
				return false;
			}
		});
	}
	
	// Case studies
	
	if($('#caseStudies').length > 0){
		$('#caseStudies div.content').not(':first').hide();
		$('#caseStudies li:first h2').addClass('on');
		$('#caseStudies h2').click(function(){
			if($(this).hasClass('on')){
				//$(this).removeClass('on').next('div.content').slideUp();	
			} else {
				$('#caseStudies li h2.on').removeClass('on');
				$('#caseStudies div.content').slideUp();
				$(this).addClass('on').next('div.content').slideDown();								
			}
		});
	}

});

function animateHomepage(){
	homepageCaseStudy();
	if(repeat == true){
		foo = setTimeout("animateHomepage()",3000);
	}
}


function homepageCaseStudy(selectMe){
	selectMe = typeof(selectMe) != 'undefined' ? selectMe : $('#case_study_select li.on').next();
	if(!selectMe.length > 0){
		selectMe = $('#case_study_select li:first');
	}
	var id = selectMe.attr('id').split('_');
	id = id[1];
	$('#case_study_select li').removeClass('on');
	$('#case_study_select li#select_'+id).addClass('on');
	$('#case_studies li').removeClass('on');
	$('#case_studies li#cs_'+id).addClass('on');
	if(!$('#case_study_images li#image_'+id).hasClass('visible')){
		var visibleId = $('#case_study_images li.visible').attr('id').split('_');
		visibleId = visibleId[1];
		$('#case_study_images li#image_'+visibleId).removeClass('visible').animate({opacity: 0},1500);
		$('#case_study_images li#image_'+id).addClass('visible').animate({opacity: 1},1500);
	}
}


function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   } else {
			return true;
	 }
}


function rotateBannerImages(){
	var images = $('ul.banners').children();
	$('ul.banners li').eq(imageIndex).animate({'opacity': 0},1000);
	imageIndex++;
	if($('ul.banners li').eq(imageIndex).length > 0){
		$('ul.banners li').eq(imageIndex).animate({'opacity': 1},1000);
	} else {
		$('ul.banners li:first').animate({'opacity': 1},1000);
		imageIndex = 0;
	}
	setTimeout("rotateBannerImages()",3000);
}

