window.onload = function() {
	preloadImages();
	//setupMasthead();  /* 2011 site refresh - removed */
	socialLinks();
	var input = document.getElementById('email-address-input');
	var clicked = 0;
	
	/*
	input.onclick = function() {
		if (!clicked) {
			this.value = '';
			clicked = 1;
		}
	};
	*/
}

function setupMasthead() {
	var leftRepeat = $("<div id='left-repeat'></div>");
	var rightRepeat = $("<div id='right-repeat'></div>");
	
	leftRepeat.insertAfter('#masthead');
	rightRepeat.insertAfter('#masthead');
}

function socialLinks() {
	var URL = encodeURIComponent(document.location.href);
	var title = encodeURIComponent(document.title);
	
	var digg = $("a[href='http://digg.com/submit']");
	var diggFinal = 'http://digg.com/submit?url=' + URL + '&title=' + title + '&topic=tech_news';
	digg.attr('href', diggFinal);
	
	var stumbleUpon = $("a[href='http://www.stumbleupon.com/submit']");
	var stumbleUponFinal = 'http://www.stumbleupon.com/submit?url=' + URL + '&title=' + title;
	stumbleUpon.attr('href', stumbleUponFinal);
}

function preloadImages() {
	logoHover = new Image();
	logoHover.src = "/wp-content/themes/swellpath/img/logo-hover.png";
	learnMoreHover = new Image();
	learnMoreHover.src = "/wp-content/themes/swellpath/img/learn-more-arrow-hover.png";
	ourServicesHover = new Image();
	ourServicesHover.src = "/wp-content/themes/swellpath/img/our-services-tab-hover.png";
	ourClientsHover = new Image();
	ourClientsHover.src = "/wp-content/themes/swellpath/img/our-clients-tab-hover.png";
	aboutUsHover = new Image();
	aboutUsHover.src = "/wp-content/themes/swellpath/img/about-us-tab-hover.png";
	serviceBgTopHover = new Image();
	serviceBgTopHover.src = "/wp-content/themes/swellpath/img/service-bg-top-hover.jpg";
	serviceBgMiddleHover = new Image();
	serviceBgMiddleHover.src = "/wp-content/themes/swellpath/img/service-bg-middle-hover.jpg";
	serviceBgBottomHover = new Image();
	serviceBgBottomHover.src = "/wp-content/themes/swellpath/img/service-bg-bottom-hover.jpg";
}


/* jquery 	*/
				 $(document).ready(function() {
	
					/* Home */	
					$(window).load(function() {					
						var intInterval;
					    var firstCycle = true;
					    var number_of_images = $('.hpbanner-slide').size();
	                    var counter = number_of_images-1;

					  	function rotateSlides(){ 
						//alert(counter);
						    if(counter == number_of_images-1 && firstCycle == false){ //--> stop timer (at end of slides and not first cycle)
							    clearInterval(intInterval);
							}else{
								if(counter == 0){
									$('#background-animation-' + counter).fadeOut(400);
								}else if(counter == 1){
								    $('#slide-' + 0).fadeOut(400);
								    $('#slide-' + 1).fadeOut(400);
							    }else if(counter == 2) {
								    $('#background-animation-' + 1).fadeOut(400);
							    }  
								counter++;
								if(counter == number_of_images){
								   counter = 0;
								}
								fadeInSlide();
							}
							firstCycle = false;
					    }

					    function fadeInSlide(){
						    if(counter == 2){
							    $('#slide-' + counter).fadeIn(400); //.delay(200)
						    }else if(counter == 4){
							    $('#slide-' + counter).fadeIn(400);
							    $('.secondary').fadeIn(400);
							    $('.tertiary').fadeIn(400);
					    	}else{
							    $('#slide-' + counter).fadeIn(400);
							}
				    	}
				
					
						function checkCookie() {
						  if (Cookie.test()) {
						    if (Cookie.get('testCookie')) {
						        $('#background-animation-1').fadeOut(400);
						        $('#background-animation-0').fadeOut(400);
					
								$('#slide-4').fadeIn(400);
							    $('.secondary').fadeIn(400);
							    $('.tertiary').fadeIn(400);
						    }
						    else {
						      Cookie.set('testCookie','true');
							  intInterval = window.setInterval(rotateSlides, 1300);  //-->start automatic slideshow
							  rotateSlides();
						    }
						  }
						}
					
					checkCookie();
					});
					

					
					/* Services */
					
					function animate_info_graph_nav(sectionPosition, areaID, event){	
						var NewSectionContentHeight = jQuery("#" + areaID + "-content-block").height() + 30; //-->30 is padding	

						var newSectionPosition = sectionPosition;
						
						jQuery("#slide-out-content-container p").fadeOut(300, function() {     //-->fadeout text
							jQuery("#slide-out-content-container").animate({                   //-->slide up content container
								height: 14
							  }, 300, function() {
								jQuery("#slide-out-content-container p").text('');
								jQuery("#slide-out").animate({                                 //-->slide slider
									top: newSectionPosition
								  }, 300, function() {
									jQuery("#slide-out-content-container").stop(true, true).animate({           //-->slide down content container
										height: NewSectionContentHeight
									  },300, function() {
										jQuery("#slide-out-content-container p").text(jQuery("#" + areaID + "-content-block").clone().html());
										jQuery("#slide-out-content-container p").fadeIn(300);  //-->fadein text
									});
								});
							});
						});
						return false;
			    	}
			
			        function animate_info_graph_graphic(section, areaID){
				        //alert(section);
				        jQuery(".info-graph-area").removeClass("selected");
				        jQuery(".info-graph-sect").fadeOut(300, function(){
                            
						}); 
						jQuery("#" + areaID).addClass("selected")
						section.fadeIn(1000);  //.delay(1000)
				    }


					jQuery("#_Image-Maps_3201104221233475 area").click(function(e){ 
						this.blur();
						e.preventDefault();
						var currentID = jQuery(this).attr("id");
						jQuery(".info-graph-area").removeClass("selected");
					    jQuery("#" + currentID).addClass("selected");
					    
						jQuery('.info-graph-area').each(function(index) {   
					    	if(jQuery(this).hasClass("selected")){
								//-->do nothing
							}else{
								jQuery("#info-graph-" + this.id + "-container").stop(true, true).fadeOut(200);
							}
						});
					
					    // call function to switch nav
					    var selectSection = jQuery("#" + currentID + "-link");
					    var parentSection = jQuery("#info-graph-nav");
					
					    var selectSectionNavPosition = selectSection.offset().top - parentSection.offset().top;
					   // var relativeX = event.pageY
					
					    //alert(selectSectionNavPosition);
					    animate_info_graph_nav(selectSectionNavPosition, currentID, e);
					    return false;
					});	


					jQuery("#_Image-Maps_3201104221233475 area").hover(
					  	function () {
						     //var currentID = this.id; //jQuery(this).id;//attr("id");
							 jQuery("#info-graph-" + this.id  + "-container").stop(true, true).fadeIn(200); 
						  }, 
						  function () {
							  //var currentID = this.id;//jQuery(this).id;//attr("id");
						    jQuery('.info-graph-area').each(function(index) {   
						    	if(jQuery(this).hasClass("selected")){
									//-->do nothing
								}else{
									jQuery("#info-graph-" + this.id + "-container").stop(true, true).fadeOut(200);
								}
							});	  
						  }
					);

					function init(event){										
						var startingPostition = "30.5";
						var startingSection = "strategy"
						var startingContentHeight = jQuery("#" + startingSection + "-content-block").height() + 30; //3-->0 is padding
						jQuery("#slide-out").animate({                 //-->slide up content container
							top: startingPostition
						  }, 300, function() {
							jQuery("#slide-out").slideDown(300, function() {  
								jQuery("#slide-out-content-container p").fadeIn(300);
								jQuery("#slide-out-content-container p").html("");
								jQuery("#slide-out-content-container p").html(jQuery("#strategy-content-block").clone().html());
								jQuery("#info-graph-strategy-container").fadeIn(300);
								jQuery("#slide-out-content-container").css("height", startingContentHeight + "px");
							});
						  });
					}

				    jQuery("#info-graph-nav ul li a").click(function(e){
					    e.preventDefault();
						var currentSection = jQuery(this);	
						var currentID = jQuery(this).attr("id");
						
					    var parentSection = jQuery("#info-graph-nav");
					
					    var selectSectionNavPosition = currentSection.offset().top - parentSection.offset().top;

						jQueryexploded = currentID.split('-');
					    newID = jQueryexploded[0];
						
						animate_info_graph_nav(selectSectionNavPosition, newID);
						
					
					    //alert(newID)
						var selectSection = jQuery("#info-graph-" + newID + "-container");
						animate_info_graph_graphic(selectSection, newID);
					    return false;
				    });
                   
                    if($('#info-graph-container').length != 0){
	                    init("event");
                    }
					
					   
					$("#s").focus(function () { 
					    $(this).attr('value', "");  
						/*$(this).css('background-image', "url('../img/search-background.jpg') no-repeat 10% 0%");	 */	
						$(this).css("text-indent", "2px");
					});
					
					
					$("#s").blur(function () { 
					    //$(this).attr('value', "search");  
					    $(this).css("text-indent", "24px");	    	
					});
					
					$("#email_signup_field").focus(function () { 
					    $(this).attr('value', "");  	
					});
					
					$("#email-address-input").focus(function () { 
					    $(this).attr('value', "");  	
					});
					
					
					$("#email_signup_field").blur(function () { 
					    //$(this).attr('value', "Enter you email address");     	
					});
					
					
					$('img.sshot').hover(
				 		function(){
							$(this).attr('src',$(this).attr('src').replace('1.jpg','.jpg'));			 	
				 		},
				 		function(){
							$(this).attr('src',$(this).attr('src').replace('.jpg','1.jpg'));
				 		}
				 	);
				
					jQuery('.stayconnected li a img').each(function(index) {   
				    	var socialTag = $(this).attr("alt");
				        $(this).attr("onClick", "_gaq.push(['_trackEvent', 'Global', 'Offsite Link', '" + socialTag + "'])");
					});
					
					jQuery('#stay-connected li a img').each(function(index) {   
				    	var socialTag = $(this).attr("alt");
				        $(this).attr("onClick", "_gaq.push(['_trackEvent', 'Global', 'Offsite Link', '" + socialTag + "'])");
					});
					
					jQuery('.twtr-join-conv').html("follow us &rsaquo;");
					
					//var value = $("#text").val()+""; // value = 9.61 use $("#text").text() if you are not on select box...
					//value.replace(".", ":");
				
						
						/* if there is a trackmaster class form check to see if there are tracking elements in it */
						/* if not create them.  Regardless parse the cookie and populate the elements */
						/* this could be abstracted further to a function that takes a class name, so that, say,
						 * all "wpcf7-form" class forms go through this 
						 */

						if($('form.trackmaster').length > 0){
						    /* taken from http://cutroni.com/blog/2007/10/29/integrating-google-analytics-with-a-crm/ */
						    function _uGC(l,n,s) {
							    if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
							    var i,i2,i3,c="-";
							    i=l.indexOf(n);
							    i3=n.indexOf("=")+1;
							    if (i > -1) {
								i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
								c=l.substring((i+i3),i2);
							    }
							    return c;
						    }
						    var zString = _uGC(document.cookie, '__utmz=', ';');
						    /* end taken */
						    /* make sure there is a parsed cookie before we bother */
						    if(zString.length > 0){
							/*wrap _uGC to be useful to the checkNCreate */ 
							function getuGC(zString,dtype){
							    return _uGC(zString,dtype + '=','|');
							}
							/* if the required elements don't exist, add them */
							/* then assign their values by calling the above*/
							function checkNCreate(zString,formLabel,cookieLabel){
							    if($('form.trackmaster input[name=' + formLabel + ']').length==0){
								$('form.trackmaster').prepend('<input  id="' + formLabel + '" name="' + formLabel + '" type="hidden" value="" />');
							    } 
							    $('form.trackmaster input#' + formLabel).val(getuGC(zString,cookieLabel));
							    
							}
							/* add what we need to the form */
							checkNCreate(zString, 'LEADCF2','utmcsr'); /* source */
							checkNCreate(zString, 'LEADCF3','utmcmd'); /* medium */
							checkNCreate(zString, 'LEADCF4','utmctr'); /* term */
							checkNCreate(zString, 'LEADCF5','utmcct'); /* content */
							checkNCreate(zString, 'LEADCF6','utmccn'); /* campaign */
							/*checkNCreate(zString, '00NA0000001xYUI','utmclid'); */ /* gclid */
						    }
						}

					 });


