/* javascript.js
* Javascript functions
* Author: David Anderson 2010
* dave@veodesign.co.uk
*/

function clearTemplateFields(){
	$('#footer-q').click(function(){
		if($(this).val()=="enter keywords") $(this).val('');
	});
	
	$('.contact-email').click(function(){
		if($(this).val()=="email") $(this).val('');
	});
	$('.contact-message').click(function(){
		if($(this).val()=="Please Enter Your Enquiry") $(this).val('');
	});	
}

/*Anchor Scrolling
 - Allows smooth scrolling
 -
 -http://www.learningjquery.com/2007/08/animated-scrolling-for-same-page-links
*/
function InitScrolling(){
	$('a[href*=#]').click(function() {
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	  && location.hostname == this.hostname) {
	   var $target = $(this.hash);
	   $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
	   if ($target.length) {
		   $.scrollTo($target, 900, {});
		   return false;
	   }
	};
	});
}

/*MAP FUNCTIONS*/
/*******************************************************************************/
function initFooterMap(){
var latlng = new google.maps.LatLng(53.4782228, -1.5652656);
		var myOptions = {
		  zoom: 14,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP,
		  mapTypeControl: false,
		  streetViewControl: false
		};
		var map = new google.maps.Map(document.getElementById("footer-map"),
			myOptions);
			
		var mark = new google.maps.Marker({
    	position: new google.maps.LatLng(53.4782228, -1.5652656),
    	map: map,
    	title: "Veo Design"
    });
}

/*NEWSFLASH FUNCTIONS*/
/*******************************************************************************/
function initNewsFlash(){
	$(document).ready(function() { 
		//start the timer
		$(this).everyTime(7000,"newsflash",function(){
			var str = $('#newsflash > div.active').attr('id');
			str= str.substr(str.lastIndexOf('-')+1,str.length);
			
			var count=$('#newsflash > div').size();
			
			if(str>=count)
				str=1;
			else
				str++;
			
			$('#newsflash  div.active').hide('drop',{},600,function(){
					$('#news-'+str).show('drop',{},1200);
					$('#news-'+str).addClass('active');
				});
			$('#newsflash  div.active').removeClass('active');
				
			});
	});
}


/*QUICK MESSAGE FUNCTIONS*/
/*******************************************************************************/
function initQuickMsg(){
	$(document).ready(function() { 
		
		//init ajax
		var options = {  
			 <!-- A function to be used if submit is successful --> 
			 success:  processXml,
			 dataType: 'xml',
			 beforeSerialize: formVal,
			 beforeSubmit:  hideForm
		};
		
		$('form#new-quick-message').ajaxForm(options);
		
		//init scrolling
		$("a[href='#msg']").click(function(ev){
			$("#footer-contact").parent().effect("highlight",{},2200);
			$("#footer-contact").effect("pulsate",{times:3},500);
		});
		
		//init search
		$("a[href='#search']").click(function(ev){
			$("#header-search input#query").effect("highlight",{},2200);
			$("#header-search input#query").focus();
		});
		
	
	});
}
function formVal(){
	if($('.contact-email').val()=="email / phone") $('.contact-email').val('');
	if($('.contact-message').val()=="Please Enter Your Enquiry") $('.contact-message').val('');

}

function hideForm(){								
	//check not default values								
	 $('form#new-quick-message').hide();
	 $('#quick-message-load').show();
};

function processXml(responseXML) { 
	 <!-- get the text from our Message node in the response XML -->
	 var message = $('result', responseXML).text();
	//$("#new-quick-message-response").html($('message div', responseXML).clone());
	$('#quick-message-load').hide();
	
	 if(message=="success"){
		$("#new-quick-message-response").html('<div class="form-success"><h3>' + $('message', responseXML).text() + "</h3></div>");
		
		$('form#new-quick-message').hide()
	 }
	 else{
		$('form#new-quick-message').show();
		var out = "#new-quick-message-response";
		
		$(out).html('<div class="form-error"><h3>' + $('message', responseXML).text() + "</h3></div>" );									
		
		out = "#new-quick-message-response .form-error";
		
		$(out).append("<ul>");
		if($('error contact', responseXML).text()!=""){
			$(out).append('<li>' + $('error contact', responseXML).text() + '</li>');
		}
		if($('error msg', responseXML).text()!=""){
			$(out).append('<li>' + $('error msg', responseXML).text() + '</li>');
		}
		$(out).append('</ul>');
	 }
}

/*HOMEPAGE ITEMS FUNCTIONS*/
/*******************************************************************************/
function homeToggleItems(){
	
	//trigger hover behaviour
	$("#home-items > div").mouseover(function(){
		
		$("#home-items .hover").removeClass("hover");
		$(this).addClass("hover");
		
		var id= $(this).attr('id');
		id=id.replace("item-",'-');
		
		$("#home-desc .active").hide();
		$("#home-desc .active").removeClass("active");
		$("#desc"+id).addClass('active');
		$("#desc"+id).show();
	});
	
	//trigger click behaviour for items
	$(".desc-item").click(function(){
		$(window.location).attr('href', $("a",this).attr('href'));
	});
	
	//trigger click behavior for descriptions
	$("#home-items > div").click(function(){
		$(window.location).attr('href', $("a",this).attr('href'));
	});
	
	//reset default message on leaving the area
	$("#home-middle").mouseleave(function(){
		$("#home-desc .active").hide();
		$("#home-desc .active").removeClass("active");	
		
		$("#desc-default").show();
		$("#desc-default").addClass('active');
		
		$("#home-items .hover").removeClass("hover");
	});
	
}

/*IDEAS ITEMS FUNCTIONS*/
/*******************************************************************************/
function ideasToggleItems(){
	
	//trigger hover behaviour
	$("#ideas-navigation > div").mouseover(function(){
		
		$("#ideas-navigation .hover").removeClass("hover");
		$(this).addClass("hover");
	});
	
	//trigger click behaviour for items
	$("#ideas-navigation .item").click(function(){
		$(window.location).attr('href', $("a",this).attr('href'));
	});
	
	//trigger click behavior for descriptions
	$("#ideas-navigation .item").click(function(){
		$(window.location).attr('href', $("a",this).attr('href'));
	});
	
	//reset default message on leaving the area
	$("#ideas-navigation").mouseleave(function(){
		$("#ideas-navigation .hover").removeClass("hover");
	});	
}

/*QUOTE ITEMS FUNCTIONS*/
/*******************************************************************************/
function quotesToggleItems(){
	
	//trigger hover behaviour
	$("#quotes-navigation > div").mouseover(function(){
		
		$("#quotes-navigation .hover").removeClass("hover");
		$(this).addClass("hover");
	});
	
	//trigger click behaviour for items
	$("#quotes-navigation .item").click(function(){
		$("#quotes-navigation .active").removeClass('active');
		$(this).addClass('active');
		var val=$(this).attr('id')
		$('input#fields-type').val(val.substring(1));		
	});
	
	//reset default message on leaving the area
	$("#quotes-navigation").mouseleave(function(){
		$("#quotes-navigation .hover").removeClass("hover");
	});	
	
	//init at the start
	var v=$('input#fields-type').val();	
	if(v!=""){
		$("#o"+v).addClass('active');	
	}
}




/*ABOUT ITEMS FUNCTIONS*/
/*******************************************************************************/
function aboutPageInit(){
	
	$("#about-sidebar > div").click(function(){			
		$("a",this).trigger('click');
		$(window.location).attr('href', $("a",this).attr('href'));
		return false;
	});
	
	$("#about-people > div").click(function(){	
		
		var h=$("a",this).attr('href');
		$.colorbox({
				href:function(){
				return h.replace('about/people','ajax/people');	
		}})
		
		//$("a",this).triggerHandler('click');
		return true;

		
	});
	
	
	
	$("div#about-sidebar").scrollFollow({
			speed: 400,
			offset: 70,
			container: 'about-wrapper'
	});

}



/*############################# initWebGuide ################################ */

var wgSection="#web-guide-sections";
var wgImage="#web-guide-image";
var wgOverlay="#web-guide-image-overlay"
var wgNav="#web-guide-tips-nav";
var wgContent="#web-guide-tips-content";
var wgIcons="#web-guide-tips-icons";
var wgSlideDur=1800;
var wgPopDur=1200;
var wgTransferDur=600;
var wgWidth=760;

var wgA=" .active";
var wgI=" > .inner";

function initWebGuide(){
	
	$(wgSection+" .item").click(function(){
		var id=getWgId($(this).attr('id'));		
		processSectionClick(wgImage+id);		
	});
	
	$(wgNav+" > div").click(function(){
		$("div a",this).trigger('click');
		//$("div a",this).click();
	});
	
	$(wgNav+" > div a").click(function(ev){
		ev.preventDefault();
		processTipClick(wgContent+getWgId($(this).parent().attr('id')));
		
		var h=$(this).attr('href');
		var hash=h.substring(h.lastIndexOf('/')+1);
		window.location.hash=hash;
		return false;
	});
	
	$(".web-guide-tips-icons > div a").click(function(ev){
		ev.preventDefault();
		processTipClick(wgContent+getWgId($(this).parent().attr('id')));
		
		var h=$(this).attr('href');
		var hash=h.substring(h.lastIndexOf('/')+1);
		window.location.hash=hash;
		return false;
	});
	
	$(wgContent+" .tooltip-close").click(function(){
		processTipClick(isTipActive());
	});
	
	//put in right place
	if(window.location.hash) {
		var hash=window.location.hash;
		hash=hash.substring(1);
		var h=$('.'+hash).attr('id');
		processTipClick(wgContent+getWgId(h));
		$.scrollTo('#web-guide-header', 900, {});		
	}
	
	$(".text-format a[href*='#']").click(function(ev){
		
		ev.preventDefault();
		
		var hash=$(this).attr('href');
		hash=hash.substring(1);
		var h=$('.'+hash).attr('id');
		processTipClick(wgContent+getWgId(h));
		$.scrollTo('#web-guide-header', 900, {});	
	});
	
}

function changeActiveSectionNav(sId){
	
	//not already active
	if("#"+$(wgSection+wgA).attr('id')!= sId){
			
		$(wgSection+wgA).removeClass("active");
		$(wgNav+" .s-active").removeClass('s-active');
		
		$(wgSection+getWgId(sId)).addClass("active");
		$(wgNav+" .rs"+getWgId(sId)).addClass("s-active");	
		
		return true;
	}
	return false;
}

function changeActiveTipNav(cId){
	
	//if a different tip
	if("#"+$(wgNav+wgA).attr('id')!= wgNav+getWgId(cId)){
		$(wgNav+wgA).removeClass('active');
		$(wgNav+ getWgId(cId)).addClass("active");
		return true;
	}
	//if the same tip, just toggle
	else{
		$(wgNav+wgA).removeClass('active');
		return false;	
	}
}


function getL(sId){
	return "-"+($(wgImage+getWgId(sId)).index()*wgWidth)+"px";
}

function processSectionClick(sId){	
	//change nav and make sure not the active one
	if(changeActiveSectionNav(sId)){	
		var oldTip=isTipActive();
		
		//if none
		if(oldTip==null){
			//slide
			$(wgImage+wgI).animate({"margin-left": getL(sId)},wgSlideDur);
			
		}
		else{
			changeActiveTipNav(oldTip);
			//hide tip	
			$(wgContent+getWgId(oldTip)).parent().effect("transfer",{to:$(wgIcons+getWgId(oldTip)), className:"wg-transfer"},wgTransferDur,function(){
				
				//transition slider
				$(wgImage+wgI).animate({"margin-left": getL(sId)},wgPopDur);				
			});
			$(wgContent+getWgId(oldTip)).hide("fade",{},wgPopDur);
			
			//$(wgImage+wgI).animate({"margin-left": getL(sId)},wgSlideDur);
		}
	}
}


function processTipClick(cId){
	
	//if there is an active tip
	var oldTip=isTipActive();
	if(oldTip!=null){
		
		//check not the current tip
		if(changeActiveTipNav(cId)){
			
			//see if it's the same slide
			var sId=isInSameSection(cId);
			
			//same section
			if(sId==null){
				//hide tip, show new tip
				$(wgContent+getWgId(oldTip)).parent().effect("transfer",{to:$(wgIcons+getWgId(oldTip)), className:"wg-transfer"},wgTransferDur, function(){
					$(wgContent+getWgId(cId)).show("fade",{},wgPopDur);
					$(wgIcons+getWgId(cId)).effect("transfer",{to:$(wgContent+getWgId(cId)), className:"wg-transfer"},wgTransferDur);	
				});
				$(wgContent+getWgId(oldTip)).hide("fade",{},wgPopDur);

									
				
			}
			//different section
			else{
				//hide tip, transition section, show tip
				$(wgContent+getWgId(oldTip)).parent().effect("transfer",{to:$(wgIcons+getWgId(oldTip)), className:"wg-transfer"},wgTransferDur,function(){
					
					changeActiveSectionNav(sId);
					$(wgImage+wgI).animate({"margin-left": getL(sId)},wgPopDur,function(){
						
						//check that there is no tip active, if something had queued etc
						$(wgContent+getWgId(oldTip)).hide("fade",{},wgPopDur);
						
						$(wgContent+getWgId(cId)).show("fade",{},wgPopDur);
						$(wgIcons+getWgId(cId)).effect("transfer",{to:$(wgContent+getWgId(cId)), className:"wg-transfer"},wgTransferDur);	
					});						
				});
				$(wgContent+getWgId(oldTip)).hide("fade",{},wgPopDur);
			}
			
		}
		//otherwise just hide
		else{
			$(wgContent+getWgId(cId)).parent().effect("transfer",{to:$(wgIcons+getWgId(cId)), className:"wg-transfer"},wgTransferDur);
			$(wgContent+getWgId(cId)).hide("fade",{},wgPopDur);
				
		}
	}
	
	//no current active tip
	else{
		changeActiveTipNav(cId);
		//see if it's the same slide
		var sId=isInSameSection(cId);
		
		//same section
		if(sId==null){
			//show tooltip
			$(wgContent+getWgId(cId)).show("fade",{},wgPopDur);
			$(wgIcons+getWgId(cId)).effect("transfer",{to:$(wgContent+getWgId(cId)), className:"wg-transfer"},wgTransferDur);
		}
		//different section
		else{
			//slide
			changeActiveSectionNav(sId);
			$(wgImage+wgI).animate({"margin-left": getL(sId)},wgPopDur,function(){
				$(wgContent+getWgId(cId)).show("fade",{},wgPopDur);
				$(wgIcons+getWgId(cId)).effect("transfer",{to:$(wgContent+getWgId(cId)), className:"wg-transfer"},wgTransferDur);	
			});
		}
		
	}
}

//get the position
function getTipIconPos(cId){
	$(wgIcons+getWgId(cId)).position();
}

//takes a tip id and checks if that is the currently active section
//returns null if the same, returns the section id if not
function isInSameSection(cId){
	
	//get the current active section
	var sId=getTipSection(cId);	
	if(sId!=getActiveSection()){
		return sId	;
	}	
	return null;
}

/*gets the active section*/
function getActiveSection(){
	return wgSection+getWgId($(wgSection+wgA).attr('id'));	
}

function getTipSection(cId){
	//get the right one
	var classList =$(cId).attr('class').split(/\s+/);
	var retVal;	
	$.each( classList, function(index, str){
		if (str.indexOf('rs-')!= -1) {
		   retVal= wgSection + getWgId(str);
		}
	});
	
	return retVal;
}


function isTipActive(){
	if($(wgNav+wgA).length != 0){
		return $(wgNav+wgA).attr('id');	
	}
	return null;
}


function getWgId(str){
	return str.substr(str.lastIndexOf('-'),str.length);
}




/*******************************************
 *
 * CAROUSEL CODE 
 * Functions and Init for the carousel
 * 
 * Params:
 * 
 * wrapper - identifier to unique wrapper
 * controller - object holding next, 1,2,3,etc,prev links
 * duration - time between transitions
 *
**
*****************************************/

function initCarousel(idPrefix,duration){
	
	//convert the prefix into names
	wrapper='#'+idPrefix+'inner';
	controller='#'+idPrefix+'controller';
	
	//set initial active count and numbers
	$(wrapper).attr('active',1);
	$(wrapper).attr('queued',0);
	$(wrapper).attr('paused',0);
	$(wrapper).attr('animating',0);
	$(wrapper).attr('count',$(wrapper).children().length);
	
	//attach click handlers - next
	$('.next',controller).click(function(ev){
		ev.preventDefault();
		//stop the timer
		$(wrapper).stopTime(idPrefix);
		
		slideChange(idPrefix,1,duration);	
	});
	//attach click handlers - prev
	$('.prev',controller).click(function(){
		ev.preventDefault();
		//stop the timer
		$(wrapper).stopTime(idPrefix);
		
		slideChange(idPrefix,-1,duration);	
	});
	
	//attach click handlers - numbers
	$('li.slide-no',controller).each(function(){
		$(this).click(function(ev){
			ev.preventDefault();
			var newSlide=$('a',this).attr('class').replace('s','');
			
			if(newSlide!=$(wrapper).attr('active')){			
				//stop the timer
				$(wrapper).stopTime(idPrefix);
				gotoSlide(idPrefix,newSlide,duration);
			}
		});
	});
	//attach click handlers - play/pause
	$('.pause',controller).click(function(ev){
		ev.preventDefault();
		//pause
		if($(wrapper).attr('paused')=='0'){
			//stop the timer
			$(wrapper).stopTime(idPrefix);
			
			$(wrapper).attr('paused','1');
	
			$(this).addClass('play');
			$(this).text('play');
			
		}
		//play
		else{
			//change to the text slide already

			slideChange(idPrefix,1,duration);

			$(wrapper).attr('paused','0');
			
			$(this).removeClass('play');	
			$(this).text('pause');					
		}
	});
	
	//set the active item
	$('a.s1',controller).addClass('active');
	
	
	//start the timer
	$(wrapper).oneTime(duration,idPrefix,function(){
		slideChange(idPrefix,1,duration);
		});
}


function gotoSlide(idPrefix, newSlide,duration){
	
	wrapper='#'+idPrefix+'inner';
		
	//load variables
	var old=parseInt($(wrapper).attr('active'));	
	var count=parseInt($(wrapper).attr('count'));		
	
	if(newSlide<=0 && newSlide > count) newSlide=1;
	
	transitionCarousel(idPrefix,newSlide,old,duration);
	
	$('a.active',controller).removeClass('active');	
	$('a.s'+newSlide,controller).addClass('active');
	
}

/*function slideChange() 
 *calculates the new slide position and initiates the transition
*/
function slideChange(idPrefix, changeCount,duration){

	wrapper='#'+idPrefix+'inner';
	controller='#'+idPrefix+'controller';
		
	//load variables
	var old=parseInt($(wrapper).attr('active'));	
	var count=parseInt($(wrapper).attr('count'));	
	
	//calculate new slide
	var active= old+changeCount;
	
	if(active>count)
		{active-= (Math.floor(active / count) * count);
	}
	else if(active==0){
		active=count;
	}
	else if(active<1){
		active+= (Math.floor((-active) / count) * count);
	}
	
	transitionCarousel(idPrefix,active,old,duration);
	
	//change the active controller entry
	$('a.active',controller).removeClass('active');	
	$('a.s'+active,controller).addClass('active');
	
}

/*function transitionCarousel()
 *Transitions and animates the carousel
*/
function transitionCarousel(idPrefix,newSlideNo,oldSlideNo,duration){	
	
	wrapper='#'+ idPrefix +'inner';
	
	//check if already animating and queue if so
	if($(wrapper).attr('animating')=='1'){
		
		//set a queue
		$(wrapper).attr('queued',newSlideNo);
	}
	else{	
		//set the animation flag
		$(wrapper).attr('animating','1');
		$('#'+idPrefix+oldSlideNo).hide('fade',{easing: 'easeOutQuad'},1500,function(){
		});
		
		$('#'+idPrefix+newSlideNo).show('fade',{easing: 'easeInQuad'},2200,function(){		
			$(wrapper).attr('active',newSlideNo);
			$(wrapper).attr('animating','0');
			
			//check for a queue
			if($(wrapper).attr('queued')!='0'){
				//process the queue
				var queue=$(wrapper).attr('queued');
				$(wrapper).attr('queued','0');	
				
				//perform the transition
					
				transitionCarousel(idPrefix,queue,$(wrapper).attr('active'),textHandle,imageHandle);
			}
			else{
				//restart the timer
				if($(wrapper).attr('paused')!=1){
					$(wrapper).oneTime(duration,idPrefix,function(){
						slideChange(idPrefix,1,duration);
					});	
				}
			}
			
		});
	
	}	
}








