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


function getParameterByName(name) {

    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

}


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

	$("div.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" ) {
				
				$("div.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
	$("div.work").hover(
		function () {
			
			if ( $(this).children().css('opacity') != 0.05 ) {
				$(this).find("div.work-slide").stop(true, true).fadeIn(350);
			}
			
		}, 
		function () {
			
			if ( $(this).children().css('opacity') != 0.05 ) {
				$(this).find("div.work-slide").stop(true, true).fadeOut(350);
			}
			
		}
	);
	
	$("div.lead-board").hover(
		function () {
			$(this).find("div.lead-board-slide").stop(true, true).fadeIn(350);
		}, 
		function () {
			$(this).find("div.lead-board-slide").stop(true, true).fadeOut(350);
		}
	);
	
	$("div.board").hover(
		function () {
			$(this).find("div.board-slide").stop(true, true).fadeIn(350);
		}, 
		function () {
			$(this).find("div.board-slide").stop(true, true).fadeOut(350);
		}
	);
	
	
	
	


	
	// 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;
				
			case "Haze":
				var currentWeatherCondition = "There's a bit of a haze out there...";
				break;
				
			case "Snow":
				var currentWeatherCondition = "It might just be snowy enough to make Snowmen...";
				break;
				
			case "Fog":
				var currentWeatherCondition = "It's a bit foggy out there...";
				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;
		}*/
	});
	
	
	
	// 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" });
	
	
	if ( $("#work-rows").length ) { 
	
		// Remove border from last work row
		$("#work-rows .work-row:last-child").css({ "border" : "0", "margin-bottom" : "0" });
	
	}
	
	
	
	/* ======================================== */
	/* WORK PAGE */
	/* ======================================== */
	
	
	if ( $("#work-items").length ) { 
	
	
		var $workItem = $("div.work");
		var workVal = getParameterByName("value");
		var workType = getParameterByName("type");
		
	
		$("#work-items .work:nth-child(4n)").css({ "margin-right" : "0"});
		
		
		if ( workVal && workType ) {
	
	
			if ( workType == "work" ) {
	
				$workItem.filter("[data-type*='" + workVal + "']").children().animate({
					opacity: 1
					}, 350);
		
				$workItem.filter(":not([data-type*='" + workVal + "'])").children().animate({
					opacity: 0.05
					}, 350);
			
			}
			
			
			if ( workType == "client" ) {
	
				$workItem.filter("[data-client*='" + workVal + "']").children().animate({
					opacity: 1
					}, 350);
	
				$workItem.filter(":not([data-client*='" + workVal + "'])").children().animate({
					opacity: 0.05
					}, 350);
	
				var theItem = $("div.work[data-client*='" + workVal + "']").eq(0);
				$.scrollTo( theItem, 500 );
				
			}
					
		
		}
		
		
		// The show all link on work page
		$("#work-menu-all").on("click",
								
			function () {
	
				$workItem.children().animate({
					opacity: 1
					}, 350);
				
				$(this).siblings().removeAttr("class");
				$(this).addClass("current-menu-item");
				
				// Re-init Lava lamp menu effect
				$("#work-menu").lavaLamp({
					fx: "easeInOutExpo",
					speed: 500
				});
		
				return false;
				
			}
	
		);
	
		
		// The client links on the work page
		$("#work-menu-client a").on("click",
								
			function () {
		
				var val = $(this).attr("href");
				val = val.substring(1, val.length);

				if( val.length ) { 
			
					$workItem.filter("[data-client*='" + val + "']").children().animate({
						opacity: 1
						}, 350);
					
					$workItem.filter(":not([data-client*='" + val + "'])").children().animate({
						opacity: 0.05
						}, 350);
	
					var theItem = $("div.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;
				
			}
		
		);
	
		// The type links on the work page
		$("#work-menu-type a").on("click",
								
			function () {
	
				var val = $(this).attr("href");
				val = val.substring(1, val.length);
				
				if( val.length ) { 
	
					$workItem.filter("[data-type*='" + val + "']").children().animate({
						opacity: 1
						}, 350);
		
					$workItem.filter(":not([data-type*='" + val + "'])").children().animate({
						opacity: 0.05
						}, 350);
					
				}
				
				$(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;
				
			}
	
		);
		
		// The client links on the work page <SPAN />
		$(".work-client-inline span").on("click",
								
			function () {
	
				var val = $(this).attr("data-client-inline");
				
				if( val.length ) { 
	
					$workItem.filter("[data-client*='" + val + "']").children().animate({
						opacity: 1
						}, 350);
	
					$workItem.filter(":not([data-client*='" + val + "'])").children().animate({
						opacity: 0.05
						}, 350);
					
				}
		
				return false;
				
			}
	
		);
		
		// The type links on the work page <SPAN />
		$(".work-type-inline span").on("click",
								
			function () {
	
				var val = $(this).attr("data-type-inline");
				
				if( val.length ) { 
	
					$workItem.filter("[data-type*='" + val + "']").children().animate({
						opacity: 1
						}, 350);
	
					$workItem.filter(":not([data-type*='" + val + "'])").children().animate({
						opacity: 0.05
						}, 350);
					
				}
		
				return false;
				
			}
	
		);
	
	
	}
	
	
	/* ======================================== */
	/* HOME PAGE */
	/* ======================================== */
	
	
	if ( $("#banner").length ){ 
	
		
		$("#home-page-panels .board:odd").css({ "margin-right" : "0"});
		$("#home-page-panels .board:last-child").prev('.board').andSelf().css({ "margin-bottom" : "0"});
	
		
		var $slideHeader = $("h3.slide-header");
		
		// Slider controls on home page
		$('li.slide').each(function(j) {	
			
			var j = j + 1;
			$("div.slider-controls").append("<button class='button_" + j + "'>" + j + "</button>");
			
		});
		
		// Set the first button as selected for home banner
		$("button.button_1").addClass("current-control");
		
		var slideCount = $("div.slider-controls > button").size();
	
		$('[title]').removeAttr('title');
	
		// Slider panel on home page	
		$("div.externalControl div.slider").jCarouselLite({
			//pauseOnHover: true,
			visible: 1,
			start: 0,
			speed: 750,
			auto: 3000,
			circular: true,
			easing: "easeInOutExpo",
			btnGo:
			[
				"div.externalControl button.button_1", 
				"div.externalControl button.button_2",
				"div.externalControl button.button_3", 
				"div.externalControl button.button_4",
				"div.externalControl button.button_5", 
				"div.externalControl button.button_6",
				"div.externalControl button.button_7", 
				"div.externalControl button.button_8",
				"div.externalControl button.button_9", 
				"div.externalControl button.button_10"
			],
			beforeStart: function(e) {
	
				$slideHeader.fadeTo(300, 0);
	
			},
			afterEnd: function(e) {
	
				var currentSlide = new Number( $(e[0]).index() );
				$slideHeader.fadeTo(300, 1);
	
				if ( currentSlide > slideCount) {
					currentSlide = 1;
				}
				
				$("div.slider-controls button").removeClass("current-control");
				$("button.button_" + currentSlide).addClass("current-control");
				
			}	
			
		});
			
	
	}
	
	/* ======================================== */



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

	/* ======================================== */


});
