document.createElement("header");
document.createElement("nav");
document.createElement("article");
document.createElement("aside");
document.createElement("section");
document.createElement("footer");



// Get the URL
var url = new String(window.location.href);
var urlArray = url.split('/');
var urlArrayLen = urlArray.length;


// Are we in any kind of work section?
// Loop through the current URL and set to true or false.
var urlInWork = false;

for ( var i = 0, len = urlArrayLen; i<len; ++i ) {

	if ( urlArray[i] == "work" ) {
		
		urlInWork = true;
		
		break;
		
	};

};


$(document).ready(function() {

	$('#page-right-services').columnize({ width: 230 });

	$(".blog-col a[href]").each(function() { 

		var href = new String( $(this).attr("href") );
		var hrefArray = href.split('/');
		var hrefArrayLen = hrefArray.length;

		if ( hrefArray[hrefArrayLen - 2] == urlArray[urlArrayLen - 2] ) {
			
			if ( hrefArray[hrefArrayLen - 2] != "blog" ) {
				
				$(".blog-col a").removeClass("current");
				$(this).addClass("current");

			};
			
			return false;
			
		};

	});


	
	
	// Indent paragraphs on the blog article pages.
	$('#blog-entry p').each(function() {
		
		if ( $(this).find('img').size() == 0 ) {
			$(this).addClass("text");
		}
		
	});

	// Validate forms
	$("#commentform").validate();

	// Animate page images
	$('#animate-images').wrapInner('<ul class="cycle-work" />');
	$('#animate-images img').each(function(j) {
		
		$(this).wrap('<li />');
	
	});
	
	
	// Cycle work images
	$('.cycle-work') 
		.cycle({ 
			fx: 'fade', 
			speed: 500, 
			timeout: 3000 
	});
						   
	// Add social button functionality
	$(".open-fb").click(
								
		function () {
			
			//var theURL = encodeURIComponent( $(this).attr("href") );
			var theURL = $(this).attr("href");
			window.open(theURL,'Share','toolbar=0,status=0,width=1000,height=590');
	
			return false;
			
		}
	
	);
	
	$(".open-tw").click(
								
		function () {
			
			var theURL = $(this).attr("href");
			window.open(theURL,'Tweet','toolbar=0,status=0,width=700,height=340');
	
			return false;
			
		}
	
	);
	
	// Hover for slides
	$(".work").hover(
		function () {
			
			if ( $(this).children().css('opacity') != 0.05 ) {
				$(this).find(".work-slide").stop(true, true).fadeIn(350);
			}
			
		}, 
		function () {
			
			if ( $(this).children().css('opacity') != 0.05 ) {
				$(this).find(".work-slide").stop(true, true).fadeOut(350);
			}
			
		}
	);
	
	$(".lead-board").hover(
		function () {
			$(this).find(".lead-board-slide").stop(true, true).fadeIn(350);
		}, 
		function () {
			$(this).find(".lead-board-slide").stop(true, true).fadeOut(350);
		}
	);
	
	$(".board").hover(
		function () {
			$(this).find(".board-slide").stop(true, true).fadeIn(350);
		}, 
		function () {
			$(this).find(".board-slide").stop(true, true).fadeOut(350);
		}
	);
	
	
	
	// The show all link on work page
	$("#work-menu-all").click(
							
		function () {

			$(".work").children().animate({
				opacity: 1
				}, 350, function() {	
					// Complete
			});
			
			$(this).siblings().removeAttr("class");
			$(this).addClass("current-menu-item");
			
			// Re-init Lava lamp menu effect
			$("#work-menu").lavaLamp({
				fx: "easeInOutExpo",
				speed: 500
			});
	
			return false;
			
		}, 
		
		function () {
			return false;	
		}

	);

	
	// The client links on the work page
	$("#work-menu-client a").click(
							
		function () {
	
			var val = $(this).attr("href");
			val = val.substring(1, val.length);
			
			if( val.length ) { 
	
				
				$(".work[data-client*='" + val + "']").children().animate({
					opacity: 1
					}, 350, function() {	
						// Complete
				});
				
	
				$(".work:not([data-client*='" + val + "'])").children().animate({
					opacity: 0.05
					}, 350, function() {	
						
				});

				var theItem = $(".work[data-client*='" + val + "']").eq(0);
				$.scrollTo( theItem, 500 );
	
			}
			
			$(this).closest("#work-menu-client").siblings().removeAttr("class");
			$(this).closest("#work-menu-client").addClass("current-menu-item");
			
			$("#work-menu").lavaLamp({
				fx: "easeInOutExpo",
				speed: 500
			});
	
			return false;
			
		}, 
		
		function () {
			return false;	
		}
	
	);

	// The type links on the work page
	$("#work-menu-type a").click(
							
		function () {

			var val = $(this).attr("href");
			val = val.substring(1, val.length);
			
			if( val.length ) { 

				$(".work[data-type*='" + val + "']").children().animate({
					opacity: 1
					}, 350, function() {	
						// Complete
				});
	
				$(".work:not([data-type*='" + val + "'])").children().animate({
					opacity: 0.05
					}, 350, function() {
						// Animation complete.
				});
				
			}
			
			$(this).closest("#work-menu-type").siblings().removeAttr("class");
			$(this).closest("#work-menu-type").addClass("current-menu-item");
			
			$("#work-menu").lavaLamp({
				fx: "easeInOutExpo",
				speed: 500
			});
	
			return false;
			
		}, 
		
		function () {
			return false;	
		}

	);
	
	// The client links on the work page <SPAN />
	$(".work-client-inline span").click(
							
		function () {

			var val = $(this).attr("data-client-inline");
			
			if( val.length ) { 

				$(".work[data-client*='" + val + "']").children().animate({
					opacity: 1
					}, 350, function() {	
						// Complete
				});

				$(".work:not([data-client*='" + val + "'])").children().animate({
					opacity: 0.05
					}, 350, function() {
						// Animation complete.
				});
				
			}
	
			return false;
			
		}, 
		
		function () {
			return false;	
		}

	);
	
	// The type links on the work page <SPAN />
	$(".work-type-inline span").click(
							
		function () {

			var val = $(this).attr("data-type-inline");
			
			if( val.length ) { 

				$(".work[data-type*='" + val + "']").children().animate({
					opacity: 1
					}, 350, function() {	
						// Complete
				});

				$(".work:not([data-type*='" + val + "'])").children().animate({
					opacity: 0.05
					}, 350, function() {
						// Animation complete.
				});
				
			}
	
			return false;
			
		}, 
		
		function () {
			return false;	
		}

	);

	
	// GOOGLE weather API (not officially supported, so beware of breakage one day and use weather.com API proxy in /branch/api/ )
	$.ajax({
		type: "GET",
		url: "/development/wp-content/themes/branch/api/weather-google.php",
		dataType: "xml",
		success: parseXml
	});
	
	function parseXml(xml){

		var currentWeatherCondition = $(xml).find("current_conditions > condition").attr("data");
		var currentWeatherImage = $(xml).find("current_conditions > icon").attr("data");
		
		switch (currentWeatherImage) {
			
			case "/ig/images/weather/sunny.gif":
				var chosenWeatherImage = "weather-sunny.png";
				break;
			case "/ig/images/weather/mostly_sunny.gif":
				var chosenWeatherImage = "weather-sunshine.png";
				break;
			case "/ig/images/weather/partly_cloudy.gif":
				var chosenWeatherImage = "weather-cloudy.png";
				break;
			case "/ig/images/weather/mostly_cloudy.gif":
				var chosenWeatherImage = "weather-cloudy.png";
				break;
			case "/ig/images/weather/rain.gif":
				var chosenWeatherImage = "weather-rain.png";
				break;		
			case "/ig/images/weather/cloudy.gif":
				var chosenWeatherImage = "weather-cloudy.png";
				break;
			case "/ig/images/weather/mist.gif":
				var chosenWeatherImage = "weather-rain.png";
				break;
			case "/ig/images/weather/storm.gif":
				var chosenWeatherImage = "weather-storm.png";
				break;
			case "/ig/images/weather/thunderstorm.gif":
				var chosenWeatherImage = "weather-storm.png";
				break;
			case "/ig/images/weather/sleet.gif":
				var chosenWeatherImage = "weather-snowy.png";
				break;
			case "/ig/images/weather/snow.gif":
				var chosenWeatherImage = "weather-snowy.png";
				break;
			case "/ig/images/weather/icy.gif":
				var chosenWeatherImage = "weather-snowy.png";
				break;

			default:
				var chosenWeatherImage = "weather-cloudy.png";

		}
		
		switch (currentWeatherCondition) {
			
			case "Mostly Cloudy":
				var currentWeatherCondition = "It's mostly cloudy out there...";
				break;
			
			case "Partly Cloudy":
				var currentWeatherCondition = "It's darn cloudy out there...";
				break;
				
			case "Cloudy":
				var currentWeatherCondition = "It's a little bit cloudy out there...";
				break;
				
			case "Clear":
				var currentWeatherCondition = "It's all fresh outside...";
				break;
				
			case "Rain":
				var currentWeatherCondition = "It's raining cats and dogs...";
				break;
				
			case "Light rain":
				var currentWeatherCondition = "It's raining kittens and puppies...";
				break;
				
			case "Overcast":
				var currentWeatherCondition = "It's all very overcast right now...";
				break;
				
			case "Drizzle":
				var currentWeatherCondition = "It's a bit of a drizzle...";
				break;

			default:
				var currentWeatherCondition = currentWeatherCondition;

		}
		
		var theDate = new Date();
		var myDate = dateFormat(theDate, "dddd, mmmm dS, yyyy");

		$("#weather-description").append( currentWeatherCondition + "and" + "&nbsp;" );
		$("#weather-temperature").append( $(xml).find("current_conditions > temp_c").attr("data") + "&#8451;" );
		$("#thedate").append(myDate);
		
		$("#weather-image").append( "<img src='/development/wp-content/themes/branch/images/" + chosenWeatherImage + "' />" );

	}
	

	// Lava lamp menu effect
	$("#menu-primary").lavaLamp({
		fx: "easeInOutExpo",
		speed: 500,
		work: urlInWork // This boolean comes from the top of the file. WARNING: DIRTY HACK! Change sometime soon.
		/*click: function(event, menuItem) {
			return false;
		}*/
	});
	
	// Lava lamp menu effect
	$("#work-menu").lavaLamp({
		fx: "easeInOutExpo",
		speed: 500
		/*click: function(event, menuItem) {
			return false;
		}*/
	});
	
	// Remove right margin from specific placed items.
	$("#work-items .work:nth-child(4n)").css({ "margin-right" : "0"});
	
	$("#home-page-panels .board:odd").css({ "margin-right" : "0"});
	$("#home-page-panels .board:last-child").prev('.board').andSelf().css({ "margin-bottom" : "0"});
	
	// Remove border from last work row
	$("#work-rows .work-row:last-child").css({ "border" : "0" });
	
	// Last images
	$("#page-left p:last-child").css({ "margin-bottom" : "0" });
	
	// Last post
	$("#blog-right .post:last").css({ "border" : "0", "margin" : "0", "padding" : "0" });
	
	// Last person
	$("#people .person:last-child").css({ "margin-bottom" : "0", "border" : "0" });
	
	
	// Slider breaks
	$(".slide-header br").replaceWith("<div class='slide-header-break' />");
	
	
	// Slider controls on home page
	$('.slide').each(function(j) {	
		
		var j = j + 1;
		$(".slider-controls").append("<button class='button_" + j + "'>" + j + "</button>");
		
	});
	
	// Set the first button as selected for home banner
	$(".button_1").addClass("current-control");
	
	var slideCount = $(".slider-controls > button").size();

	$('[title]').removeAttr('title');

	// Slider panel on home page	
	$(".externalControl .slider").jCarouselLite({
		//pauseOnHover: true,
		visible: 1,
		start: 0,
		speed: 750,
		auto: 3000,
		circular: true,
		easing: "easeInOutExpo",
		//btnNext: ".externalControl .next",
		//btnPrev: ".externalControl .prev",
		btnGo:
		[
			".externalControl .button_1", 
			".externalControl .button_2",
			".externalControl .button_3", 
			".externalControl .button_4",
			".externalControl .button_5", 
			".externalControl .button_6",
			".externalControl .button_7", 
			".externalControl .button_8",
			".externalControl .button_9", 
			".externalControl .button_10"
		],
		beforeStart: function(e) {

			if ( ! $.browser.msie ) {
				Cufon.refresh();
				$(".slide-header").fadeTo(300, 0);
			};

		},
		afterEnd: function(e) {

			var currentSlide = new Number( $(e[0]).index() );

			if ( ! $.browser.msie ) {
				Cufon.refresh();
				$(".slide-header").fadeTo(300, 1);
			};




			if ( $.browser.msie && (currentSlide == (slideCount + 1)) ) {
				
				if ( $(".slide").index() == (currentSlide == (slideCount)) ) {

					//$(".slide").eq(currentSlide).find(".slide-header").eq(0);
					//Cufon.refresh();
					
				}
				
			};
			

			if ( currentSlide > slideCount) {
				currentSlide = 1;
			}
			
			$(".slider-controls button").removeClass("current-control");
			$(".button_" + currentSlide).addClass("current-control");
			
		}	
		
	});
	
	
	// CoolClock not available for IE < 9
	if ( $.browser.msie && parseInt($.browser.version, 9) < 9 ) {
		
		$("#logo").after('<div id="flashclock"><object type="application/x-shockwave-flash" data="/development/wp-content/themes/branch/flash/clock.swf" width="39" height="39"><param name="movie" value="/development/wp-content/themes/branch/flash/clock.swf" /></object></div>');
	
	} else {
		
		$("head").prepend('<script src="/development/wp-content/themes/branch/js/excanvas.js"></script><script src="/development/wp-content/themes/branch/js/jquery.clock.js"></script>');
		$("#logo").after('<div id="clock"><canvas id="c1" class="CoolClock:branch:19"></canvas></div>');
	
	}

	
	/*
	
	http://www.google.com/ig/images/weather/sunny.gif
	http://www.google.com/ig/images/weather/mostly_sunny.gif
	http://www.google.com/ig/images/weather/partly_cloudy.gif
	http://www.google.com/ig/images/weather/mostly_cloudy.gif
	http://www.google.com/ig/images/weather/chance_of_storm.gif
	http://www.google.com/ig/images/weather/rain.gif
	http://www.google.com/ig/images/weather/chance_of_rain.gif
	http://www.google.com/ig/images/weather/chance_of_snow.gif
	http://www.google.com/ig/images/weather/cloudy.gif
	http://www.google.com/ig/images/weather/mist.gif
	http://www.google.com/ig/images/weather/storm.gif
	http://www.google.com/ig/images/weather/thunderstorm.gif
	http://www.google.com/ig/images/weather/chance_of_tstorm.gif
	http://www.google.com/ig/images/weather/sleet.gif
	http://www.google.com/ig/images/weather/snow.gif
	http://www.google.com/ig/images/weather/icy.gif
	http://www.google.com/ig/images/weather/dust.gif
	http://www.google.com/ig/images/weather/fog.gif
	http://www.google.com/ig/images/weather/smoke.gif
	http://www.google.com/ig/images/weather/haze.gif
	http://www.google.com/ig/images/weather/flurries.gif
	
	*/
	

});
