/* 
* Skeleton V1.0.3
* Copyright 2011, Dave Gamache
* www.getskeleton.com
* Free to use under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
* 7/17/2011
*/	

jQuery(document).ready(function($) {
	
	$.extend({
	  getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
		  hash = hashes[i].split('=');
		  vars.push(hash[0]);
		  vars[hash[0]] = hash[1];
		}
		return vars;
	  },
	  getUrlVar: function(name){
		return $.getUrlVars()[name];
	  }
	});	
	
	var color_scheme = $.getUrlVar('color_scheme');
	
	if(color_scheme) {
		$("#custom").attr('href', function() {
			return this.href + '&color_scheme=' + color_scheme;
		});
		
		$("nav a").each(function() {
			
			var url = $(this).attr("href");
			var hashes = $(this).attr("href").split('?');
						
			if(!hashes[1]) {				
				$(this).attr('href', function() {
					return this.href + '?color_scheme=' + color_scheme;
				});
			}
				
		});
		
	}
	
		
	/* Prettyphoto
	================================================== */
	$("a[data-rel^='prettyPhoto']").prettyPhoto();
	
	$('.subscriberStats div').tipTip({defaultPosition:'top'});
			
	/* Superfish
	================================================== */
	$(function(){ // run after page loads
		$('#navigation ul.menu')
		.find('li.current_page_item,li.current_page_parent,li.current_page_ancestor,li.current-cat,li.current-cat-parent,li.current-menu-item')
			.addClass('active')
			.end()
			.superfish({autoArrows	: true});
	});
	
	/* Mobile Dropdownmenu
	================================================== */
	$("<select />").appendTo("nav");
	
	// Create default option "Go to..."
	$("<option />", {
	   "selected": "selected",
	   "value"   : "",
	   "text"    : "Go to..."
	}).appendTo("nav select");
	
	// Populate dropdown with menu items
	$("nav a").each(function() {
	 	var el = $(this);
	 	$("<option />", {
		 "value"   : el.attr("href"),
		 "text"    : el.text()
	 	}).appendTo("nav select");
	});
	
	$("nav select").change(function() {
	  window.location = $(this).find("option:selected").val();
	});
	
	/* Tip
	================================================== */	
	$(function(){ 
		$(".tTip").tipTip({maxWidth: "auto", edgeOffset: 5, defaultPosition:"top", delay: 100});
	});
	
	
	
	/* Testimonials
	================================================== */
	$('.lambda-latest-testimonials').hide().eq(0).show();
	(function showNextTestimonial(){

		$('.lambda-testimonials article:visible').delay(3500).fadeOut('slow',function(){

			$(this).appendTo('.lambda-testimonials');

			$('.lambda-testimonials article:first').fadeIn('slow',function(){

				showNextTestimonial();
				
			});
		});
	})();

	
	
	
	
	
	// Style Tags
	$(function(){ // run after page loads
		$('p.tags a')
		.wrap('<span class="st_tag" />');
	});
	
	
	//Youtube WMode
	$('iframe').each(function() {
		var url = $(this).attr("src");
		
		if (url!=undefined) {
			
			var youtube = url.search("youtube");
			
			splitable = url.split("?");
				
			if(youtube > 0 && splitable[1]) {
				$(this).attr("src",url+"&wmode=transparent")
			}
			
			if(youtube > 0 && !splitable[1]) {
				$(this).attr("src",url+"?wmode=transparent")
			}
		
		}
		
	});
	
	
	// Toggle Slides
	$(function(){ // run after page loads
		$(".toggle_container").hide(); 
		//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
		$("p.trigger").click(function(){
			$(this).toggleClass("active").next().slideToggle("normal");
			return false; //Prevent the browser jump to the link anchor
		});
	});
	
	
	// valid XHTML method of target_blank
	$(function(){ // run after page loads
		$('a[rel*=external]').click( function() {
			window.open(this.href);
			return false;
		});
	});


	/* Tabs Activiation
	================================================== */
	var tabs = $('ul.tabs');
	tabs.each(function(i) {
		//Get all tabs
		var tab = $(this).find('> li > a');
		$("ul.tabs li:first").addClass("active").fadeIn('fast'); //Activate first tab
		$("ul.tabs li:first a").addClass("active").fadeIn('fast'); //Activate first tab
		$("ul.tabs-content li:first").addClass("active").fadeIn('fast'); //Activate first tab
		
		tab.click(function(e) {
			
			//Get Location of tab's content
			var contentLocation = $(this).attr('href') + "Tab";
			
			//Let go if not a hashed one
			if(contentLocation.charAt(0)=="#") {
			
				e.preventDefault();
			
				//Make Tab Active
				tab.removeClass('active');
				$(this).addClass('active');
				
				//Show Tab Content & add active class
				$(contentLocation).show().addClass('active').siblings().hide().removeClass('active');
				
			} 
		});
	}); 
	
	/* scroll to top
	================================================== */	
	$('a[href*=#top]').click(function() {
	    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	        && location.hostname == this.hostname) {
	            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
	            if ($target.length) {
	                var targetOffset = $target.offset().top;
	                $('html,body').animate({scrollTop: targetOffset}, 1000);
	                return false;
	            }
	        }
	    });
	
	/* IE Fallback
	================================================== */
	$('#clients li:last-child').css('margin-right', '0px');
	
	
	/* Image Hover Effect
	================================================== */
	$(".portfolio-item, #carousel-portfolio > li .frame, .imagepost").hover(function(){
		
		$(this).find('.hover-overlay').stop().animate({ opacity: 0.45 }, 350, 'easeOutExpo');		
		$(this).find('.pretty-hover').stop().animate({ left: '50%'}, 250, 'easeOutExpo');		
		$(this).find('.permalink-hover').stop().animate({ left: '50%'}, 250, 'easeOutExpo');
		
	}, function(){
		
		$(this).find('.pretty-hover').stop().animate({ left: '150%'}, 250, 'easeInExpo', function(){
			//reset position
			$(this).css('left','-50%');
		});
		
		$(this).find('.permalink-hover').stop().animate({ left: '150%'}, 250, 'easeInExpo', function(){
			//reset position
			$(this).css('right','-50%');
		});
		
		$(this).find('.hover-overlay').stop().animate({ opacity: 0 }, 350, 'easeInExpo');
	});
	
});