// functions.js
//
// created: 18/08/08
//
// author: Jamie Boyd

var googleApi = "http://www.google.com/jsapi?key=ABQIAAAAtahBlgfTy6FurKAiarEdThQGq4gOt1kTccTSGvOgHN92Wq4DNxSS1s8lmbr8pfgJnNGERFZmaECA7g";

$(function() {



    // Case Study Services Show/Hide
    $(".caseServices .caseService .intro > *:last-child, .caseServices .caseService .extra > *:last-child").addClass("last");
    $(".caseServices .caseService .extra > *:first-child").addClass("first");  
    $(".caseServices .caseService").each(function(){
        var extraContent = $(this).find(".extra");
        if (extraContent.length){
        var serviceButton = jQuery("<p class=\"extraButton\"><a href=\"\">+More</a></p>").appendTo(this);
        }
    });
    
    $(".caseServices .caseService .extraButton").click(function(){
            var extraState = $(this).parent().find(".extra:visible");
            if (extraState.length){
                $(this).parent().find(".extra").eq(0).slideUp();
                $(this).find("a").eq(0).text("+More");
                return false;
            }
            else {
                $(this).parent().find(".extra").eq(0).slideDown();
                $(this).find("a").eq(0).text("-Less");
                return false;
            }
    });
    
    
    // Job Vacancies Show/Hide
    $(".jobsList dl").each(function(){
        var extraContent = $(this).find("dd.details");
        if (extraContent.length){
        var serviceButton = jQuery("<p class=\"extraButton\"><a href=\"\">+Details</a></p>").appendTo(this);
        }
    });
    
    $(".jobsList dl .extraButton").click(function(){
            var extraState = $(this).parent().find(".details:visible");
            if (extraState.length){
                $(this).parent().find("dd.details").eq(0).slideUp();
                $(this).find("a").eq(0).text("+Details");
                return false;
            }
            else {
                $(this).parent().find("dd.details").eq(0).slideDown();
                $(this).find("a").eq(0).text("-Details");
                return false;
            }
    });
    
    
    
    //$(".caseServices .caseService").

	// Clients Scroller 
	
//	$(".sectorClients").addClass("scroller");
//	
//	// get width of logos in scroller
//	if ($(".sectorClients .inner img").length > 0){
//	    
//	    var visWidth = 642;
//	    var imgWidth = 107;
//	    var imgNum = $(".sectorClients .inner img").length;
//	    var scrollerWidth = imgWidth * imgNum;
//	    $(".scroller ul").css("width",scrollerWidth);
//	    var pageNum = scrollerWidth / visWidth;
//        
//        if (pageNum == 1) {
//            pageNum = 0;
//        }
//        
//	    pageNum = (parseInt(pageNum)) + 1;
//	    
//	    if (pageNum > 1){
//	        var leftBut = document.createElement('div');
//	        var rightBut = document.createElement('div');
//    	
//	        $(leftBut).addClass("leftBut").addClass("lEnd").append(document.createElement('a'));
//	        $(rightBut).addClass("rightBut").append(document.createElement('a'));
//    	
//	        $(".sectorClients .clientList").append(leftBut);
//	        $(".sectorClients .clientList").append(rightBut);
//	    }
//	    else {
//	        
//	        $(".sectorClients").removeClass("scroller");
//	    }
//	    var currPage = 1;
//	}
//	
//	$(".rightBut").click(function(){
//	    var currMargin = $(this).parent().find("ul").css('margin-left');
//	    currMargin = parseInt(currMargin);
//	    
//	    if (currPage < pageNum){
//	        $(".leftBut").removeClass("lEnd");
//	        $(this).parent().find("ul").animate({marginLeft: "-=642"},500);
//	        currPage = currPage + 1;
//	        if (currPage == pageNum){
//	            $(".rightBut").addClass("rEnd");
//	        }
//	    }
//	});
//	
//	$(".leftBut").click(function(){
//	    var currMargin = $(this).parent().find("ul").css('margin-left'); 
//	    currMargin = parseInt(currMargin);
//	    if (currPage > 1){
//	        $(".rightBut").removeClass("rEnd");
//	        $(this).parent().find("ul").animate({marginLeft: "+=642"},500);
//	        currPage = currPage - 1;
//	        if (currPage == 1){
//	            $(".leftBut").addClass("lEnd");
//	        }
//	    }
//	});


	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	// Color Conversion functions from highlightFade
	// By Blair Mitchelmore
	// http://jquery.offput.ca/highlightFade/

	// Parse strings looking for color tuples [255,255,255]
	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	

	
		
});


/*
 * End of jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */




// Colours for transitions
var colors = {
	black:[0,0,0],
	blue:[70,199,242],
	green:[115,190,30],
	pink:[236,0,140],
	white:[255,255,255],
	yellow:[255,255,0],
	orange:[230,79,42],
	red:[255,0,0]
};


$(function() {
	
	// List Of Services functions
	$(".listOfServices .serviceWrap .serviceContent").hide();
	$(".listOfServices .serviceWrap").removeClass("open");
	
	// This bit of code pases a variable as query string to open a selected service on the What We Do Page	
	dataPassed = unescape(location.search.substring(0))
	if (dataPassed.match("1381")) {
		$(".seoService").addClass("open");
		$(".seoService .serviceContent").slideToggle("fast");
		$(".serviceWrap h2").addClass("closed");
		$(".seoService h2").removeClass("closed");
	}
	if (dataPassed.match("1383")) {
		$(".ppcService").addClass("open");
		$(".ppcService .serviceContent").slideToggle("fast");
		$(".serviceWrap h2").addClass("closed");
		$(".ppcService h2").removeClass("closed");
	}
	if (dataPassed.match("1384")) {
		$(".creativeService").addClass("open");
		$(".creativeService .serviceContent").slideToggle("fast");
		$(".serviceWrap h2").addClass("closed");
		$(".creativeService h2").removeClass("closed");
	}
	if (dataPassed.match("1386")) {
		$(".affiliateService").addClass("open");
		$(".affiliateService .serviceContent").slideToggle("fast");
		$(".serviceWrap h2").addClass("closed");
		$(".affiliateService h2").removeClass("closed");
	}
	if (dataPassed.match("1387")) {
		$(".developmentService").addClass("open");
		$(".developmentService .serviceContent").slideToggle("fast");
		$(".serviceWrap h2").addClass("closed");
		$(".developmentService h2").removeClass("closed");
	}
	if (dataPassed.match("1388")) {
		$(".emarketingService").addClass("open");
		$(".emarketingService .serviceContent").slideToggle("fast");
		$(".serviceWrap h2").addClass("closed");
		$(".emarketingService h2").removeClass("closed");
	}
	
	// List of Services Accordian Function
	$(".listOfServices .serviceWrap h2").click(function(){

		// If the clicked item is the open item leave it alone
		// otherwise close the open item and remove the "open" class from it
		if ($(this).parent().hasClass("open")){
		}
		else {
			$(".listOfServices .open").each(function(){
				$(this).removeClass("open");
				$(this).find(".serviceContent").eq(0).slideUp("fast");
			});
		}
		
		// Add/Remove "open" class to clicked item depending on state
		$(this).parent().toggleClass("open");
		
		// If none are open show all coloured icons
		if ($(".listOfServices .open").length > 0){
			$(".listOfServices .serviceWrap").each(function(){
				if ($(this).hasClass("open")) {
					$(this).find("h2").eq(0).removeClass("closed");
				}
				else {
					$(this).find("h2").eq(0).addClass("closed");
				}
			});
		}
		else {
			$(".listOfServices .serviceWrap h2").each(function(){
				$(this).removeClass("closed");
			});
		}

		// Open/Close content of clicked item depending on current state
		$(this).parent().find(".serviceContent").eq(0).slideToggle("fast");
	});
	
    //var subNav = "<div id=\"subNav\"></div>";
	// Set up sub-nav menu
	//$(".nav").append(subNav);
	//var subNavMenu = $(".nav ul ul").eq(0);
	//var className =  $(".nav ul ul").parents("li").eq(0).attr("class");
	//$("#subNav").append(subNavMenu);
	//$("#subNav ul").addClass(className);
	
	
	// Colour transitions

	$(".nav > ul li.whoR_equator a").hover(function(){$(this).animate( { backgroundColor: 'pink' }, { queue: false, duration: 200 });},function(){$(this).animate( { backgroundColor: 'black' },  { queue: false, duration: 200 });
	});

	$(".nav > ul li.whatWeDo a").hover(function(){$(this).animate( { backgroundColor: 'orange' }, { queue: false, duration: 200 });},function(){$(this).animate( { backgroundColor: 'black' },  { queue: false, duration: 200 });
	});

	$(".nav > ul li.featuredWork a").hover(function(){$(this).animate( { backgroundColor: 'blue' }, { queue: false, duration: 200 });},function(){$(this).animate( { backgroundColor: 'black' },  { queue: false, duration: 200 });
	});

	$(".nav > ul li.clients a").hover(function(){$(this).animate( { backgroundColor: 'red' }, { queue: false, duration: 200 });},function(){$(this).animate( { backgroundColor: 'black' },  { queue: false, duration: 200 });
	});	

	$(".nav > ul li.talkToUs a").hover(function(){$(this).animate( { backgroundColor: 'green' }, { queue: false, duration: 200 });},function(){$(this).animate( { backgroundColor: 'black' },  { queue: false, duration: 200 });
	});


	
	// image hover for client icons
	$(".client dd.services img").hover(
			function(){
			this.src = this.src.replace("_off","_on");
			},
			function()
			{
			this.src = this.src.replace("_on","_off");	
			});
            // setup google map
            if( $(".googleMap").length )
            {
                  $.getScript( googleApi , loadGoogleMapApi );
            }


});


// Google Map functionality
function loadGoogleMapApi(){
    google.load("maps","2", {"callback" : initMap });
}



function initMap()
{
      var map = new GMap2( $(".googleMap").get(0) );
      map.addControl(new GSmallMapControl());
      //map.addControl(new GMapTypeControl());
      map.setMapType(G_NORMAL_MAP);
      map.enableContinuousZoom();
      //var point = new GLatLng( gLat, gLong );
      map.setCenter(new google.maps.LatLng(55.8593609, -4.2784804), 17);
      
      var eqtrIcon = new GIcon(G_DEFAULT_ICON);
      eqtrIcon.image = "/images/mapicon.png";
      eqtrIcon.iconSize = new GSize(61, 95);
      eqtrIcon.shadow = "/images/iconshadow.png";
      eqtrIcon.shadowSize = new GSize(68, 96);
      eqtrIcon.iconAnchor = new GPoint(39, 74);
      eqtrIcon.infoShadowAnchor = new GPoint(15, 0);
      var point = new GLatLng(55.8593609, -4.2784804);
      markerOptions = { icon:eqtrIcon };
      map.addOverlay(new GMarker(point, markerOptions));

      //var marker = new GMarker(point,new GIcon(G_DEFAULT_ICON));
      //map.setCenter( point , 14 );
      //map.addOverlay(marker);
}



