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


function initMaster(){

	
	/*detect when leaving*/
	$("#sub-nav").data('q',0)
	
	$("#header-nav li a").hoverIntent( {over: navOver, timeout: 300, out: navOut } );
	
	$("#sub-nav").hover(function(){$('#sub-nav').data('hovering',1);},
			function(){$('#sub-nav').data('hovering',0);closeNav();} );
	
	$("#header-nav").hoverIntent({
			over: function(){$('#header-nav').data('hovering',1);},
			timeout: 300,
			out: function(){$('#header-nav').data('hovering',0);closeNav();} });
	
}

function colorChange(newCol){
	
	$(".blue").removeClass("blue").addClass(newCol);	
	$(".green").removeClass("green").addClass(newCol);	;	
	$(".orange").removeClass("orange").addClass(newCol);	;	
	$(".pink").removeClass("pink").addClass(newCol);	;	
	
}


function navOver(){
	//if(!$(this).hasClass('active')){
		
		//get the old class
		var old=$("#header-nav li a.active").parent().attr('class');
		var n=$(this).parent().attr('class');
		
		//change the link active status
		$("#header-nav li a.active").removeClass('active');
		$(this).addClass('active');
		
		//if there was an old one
		if($("#sub-nav .a").length ==0){
			$("#sub-nav > div").hide();
			$("#sub-nav").show("blind",{direction: 'vertical'}, 800);	
			$("#"+n+"-tab").show();
			$("#"+n+"-tab").addClass('a');
		}
		else{
			
			//animate
			$("#sub-nav .a").hide('slide',{direction: 'left'},1400,function(){
				$("#sub-nav").data('q',0)
			});
			
			$("#sub-nav .a").removeClass('a');
			
			$("#sub-nav").data('q',1)

			$("#"+n+"-tab").show('slide',{direction: 'right'},1400,function(){});
			$("#"+n+"-tab").addClass('a');	
			
			
		}
		
		//change colour
		if(n=='ad')colorChange('blue');
		if(n=='comp')colorChange('green');
		if(n=='dir')colorChange('pink');
		if(n=='mag')colorChange('orange');	
	
	
	//}	
}
function navOut(){
	closeNav();
}


function closeNav(){
	if(	$('#sub-nav').data('hovering')!=1 &&
		$('#header-nav').data('hovering')!=1 &&
		$('#header-nav').data('always-show')!=1){
			
		$("#sub-nav").hide("blind",{direction: 'vertical'}, 800,function(){
			
			
		});	
		$("#sub-nav .a").removeClass('a');	
		
		
	}
}



function initAreaMapping(){

	//load the map
	var latlng = new google.maps.LatLng(53.500, -1.550);
    var myOptions = {
      zoom: 11,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("advertise-map"), myOptions);

	//the polygon lat/long for the areas
    var S35Lat=new Array(53.43796824240261,53.4314743990752,53.43367321304325,53.43203689719724,53.41316357802398,53.4300936903041,53.44936841692429,53.459733328349415,53.48231463725135,53.503862957395086,53.526318741784785,53.51284669921913,53.49804292660514,53.48108881129566,53.46919640705826);
	var S35Long=new Array(-1.4514891480118686,-1.4603297089249343,-1.4949194763809892,-1.510454830995247,-1.5372340058003542,-1.5640990112935182,-1.5685622070944794,-1.5533606347990658,-1.564518624301019,-1.582371407504144,-1.5720717248869365,-1.5404860315275815,-1.519200020785394,-1.4977423486662533,-1.463762936465456);

    var S36Lat=new Array(53.510999219802905,53.46953381907076,53.45829237024653,53.460448757683466,53.482212486441185,53.49426458422598,53.50376085847742,53.527849384653734,53.55058837712181,53.55527948328799,53.55058837712281,53.54140862170534,53.524880044385036,53.516715404205996);
    var S36Long=new Array(-1.7750517626965867,-1.6521422167980693,-1.5920607348641758,-1.5569655237150815,-1.5676085290861552,-1.5792815027189477,-1.5868346033049052,-1.5751616296721127,-1.6112868090820665,-1.650425603027379,-1.6823546191412198,-1.732136418457646,-1.7856947680676005,-1.7856947680676005);

    var S75Lat=new Array(53.486475600995895,53.47465264392114,53.48231463725135,53.49804292660514,53.51445291235865,53.530781491202944,53.563623679869494,53.585640185226126,53.61416303917392,53.611311620408735,53.5935877727002,53.58176774214159,53.56749778500845,53.55301905114755,53.54730776538683,53.5232303058053,53.51179892378924);
    var S75Long=new Array(-1.470299885054347,-1.4743964014006283,-1.4967123804045346,-1.515080147738519,-1.5375911448203716,-1.5770732615191707,-1.5894328806597555,-1.6100322458947902,-1.576386616011758,-1.5063487742149828,-1.497765705367187,-1.488839313765884,-1.4854060862265617,-1.501198932906509,-1.514931843062839,-1.4936458323203516,-1.4936458323203516);
	
	var HD8Lat=new Array(53.597395,53.631611,53.683695,53.720279,53.674748,53.642196,53.597395,53.520717,53.523166,53.563152);
	var HD8Long=new Array(-1.587524,-1.675415,-1.726227,-1.777039,-1.808624,-1.904755,-1.955566,-1.856689,-1.778412,-1.625977);
	
    //convert each set of arrays to co-ordinate objects
    var S35Co=LatLngToCoords(S35Lat, S35Long);
    var S36Co=LatLngToCoords(S36Lat, S36Long);
    var S75Co=LatLngToCoords(S75Lat, S75Long);
	var HD8Co=LatLngToCoords(HD8Lat, HD8Long);

	//create the areas to be rendered
    var S35Area;
    var S36Area;
    var S75Area;
	var HD8Area;

	S35Area= new google.maps.Polygon(
	{
    	paths: S35Co,
    	strokeColor: "#FF0000",
    	strokeOpacity: 0.6,
    	strokeWeight: 2,
    	fillColor: "#FF0000",
    	fillOpacity: 0.6
    });
    S36Area= new google.maps.Polygon(
	{
    	paths: S36Co,
    	strokeColor: "#00FF00",
    	strokeOpacity: 0.6,
    	strokeWeight: 2,
    	fillColor: "#00FF00",
    	fillOpacity: 0.6
    });
    S75Area= new google.maps.Polygon(
	{
    	paths: S75Co,
    	strokeColor: "#0000FF",
    	strokeOpacity: 0.6,
    	strokeWeight: 2,
    	fillColor: "#0000FF",
    	fillOpacity: 0.6
    });
	HD8Area= new google.maps.Polygon(
	{
    	paths: HD8Co,
    	strokeColor: "#E67203",
    	strokeOpacity: 0.6,
    	strokeWeight: 2,
    	fillColor: "#F68213",
    	fillOpacity: 0.6
    });

	//display the areas on the map
    S35Area.setMap(map);
    S36Area.setMap(map);
    S75Area.setMap(map);
	HD8Area.setMap(map);

    //display markers for each region

    var S35Marker = new google.maps.Marker({
    	position: new google.maps.LatLng(53.45,-1.5),
    	map: map,
    	title: "S35 Edition"
    });

    var S36Marker = new google.maps.Marker({
    	position: new google.maps.LatLng(53.51,-1.70),
    	map: map,
    	title: "S36 Edition"
    });

    var S75Marker = new google.maps.Marker({
    	position: new google.maps.LatLng(53.58,-1.53),
    	map: map,
    	title: "S75 Edition"
    });
	
	 var HD8Marker = new google.maps.Marker({
    	position: new google.maps.LatLng(53.622,-1.759),
    	map: map,
    	title: "HD8 Edition"
    });

    //display the info windows

	var S35InfoString=jQuery("#map-s35")[0];
	var S36InfoString=jQuery("#map-s36")[0];
	var S75InfoString=jQuery("#map-s75")[0];
	var HD8InfoString=jQuery("#map-hd8")[0];

	//open the info windows
	var S35InfoWindow= new google.maps.InfoWindow({content: S35InfoString,maxWidth: 220});
	var S36InfoWindow= new google.maps.InfoWindow({content: S36InfoString,maxWidth: 220});
	var S75InfoWindow= new google.maps.InfoWindow({content: S75InfoString,maxWidth: 220});
	var HD8InfoWindow= new google.maps.InfoWindow({content: HD8InfoString,maxWidth: 220});

	S35InfoWindow.open(map,S35Marker);
	S36InfoWindow.open(map,S36Marker);
	S75InfoWindow.open(map,S75Marker);
	HD8InfoWindow.open(map,HD8Marker);

	//allow them to be opened once closed
	google.maps.event.addListener(S35Marker, 'click',function(){S35InfoWindow.open(map, S35Marker);});
	google.maps.event.addListener(S36Marker, 'click',function(){S36InfoWindow.open(map, S36Marker);});
	google.maps.event.addListener(S75Marker, 'click',function(){S75InfoWindow.open(map, S75Marker);});
	google.maps.event.addListener(HD8Marker, 'click',function(){S75InfoWindow.open(map, HD8Marker);});

}

function LatLngToCoords(lat,long){
	var Co = new Array();
	for(var i=0; i<lat.length; i++){
		Co.push(new google.maps.LatLng(lat[i],long[i]));
	}
	return Co;
}

































/*******************************************
 *
 * 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);
					});	
				}
			}
			
		});
	
	}	
}








