//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Add placeholder detection to jQuery.support
//-------------------------------------------------------------------------------------------------------
(function() {
  var i = document.createElement('input');
  jQuery.support.placeholder = 'placeholder' in i;
})();

$(document).ready(function(){	

	$(".date").dateinput();

	$(".scrollable").scrollable();

//-------------------------------------------------------------------------------------------------------
//	External Links
//-------------------------------------------------------------------------------------------------------
	$('a[href^="http://"]').attr("target", "_blank");

//-------------------------------------------------------------------------------------------------------
//	Share widget
//-------------------------------------------------------------------------------------------------------
	$("#share").hover(
		function () {
			$(this).find("ul").fadeIn( 75 );
		}, 
		function () {
			$(this).find("ul").fadeOut( 75 );
		}
	);

//-------------------------------------------------------------------------------------------------------
//	Share/Email
//-------------------------------------------------------------------------------------------------------
	$("a#share-email").overlay({
		mask: 'darkred',
		onBeforeLoad: function() {
			// grab wrapper element inside content
			var wrap	= this.getOverlay().find(".contentWrap");
			var	url		= this.getTrigger().attr("href");
			wrap.empty().prepend('<iframe id="emailthis" src="'+url+'"></iframe>');
		}
	});

//-------------------------------------------------------------------------------------------------------
//	Social Media Share - Open new window
//-------------------------------------------------------------------------------------------------------
	$("a#share-facebook,a#share-twitter,a#share-linkedin").click(function(event){

		//	Master category for all shares
		var	category	= 'Share';

		//	Share category
		var	subcategory	= $(this).attr("id");
		subcategory		= subcategory.split("-");
		subcategory		= subcategory[ ( subcategory.length - 1 ) ];

		//	Label based on title
		var	label		= $(document).attr("title");

		//	Send the event to Analytics
		_gaq.push(['_trackEvent', category, subcategory, label ]);
		
		var	attributes	= "width=550,height=300,location=0,menubar=0,toolbar=0";
		window.open( $(this).attr("href"), "share", attributes, true );
		event.preventDefault();
	});

//-------------------------------------------------------------------------------------------------------
//	Track Downloads
//-------------------------------------------------------------------------------------------------------
	$("a.download,a[href$='pdf'],a[href$='doc'],a[href$='mp3']").click(function(){

		//	Master category for all downloads
		var	category	= 'Downloads';

		//	Path of file being downloaded
		var	filepath	= $(this).attr("href");

		//	Use the file extention as the "action" parameter for the event
		filepath		= filepath.split(".");
		var	filetype	= filepath[ ( filepath.length - 1 ) ].toUpperCase();

		//	Label based on title or href
		var	label		= $(this).attr("title");

		if( !label )
		{
			label		= $(this).attr("href");
		}

		//	Send the event to Analytics
		_gaq.push(['_trackEvent', category, filetype, label ]);

		//	Open in new tab	
		$(this).attr("target","_blank");
	});

//---------------------------------------------------------------------------------------------------------
//	Expandible widgets
//---------------------------------------------------------------------------------------------------------
    $(".expand").click(function () {
		$(this).next("div.expandable").slideToggle("fast");
    });    
	
	$('.expand').toggle(function(){
		$(this).addClass('open');
	},function(){
		$(this).removeClass('open');
	});

	//---------------------------------------------------------------------------------------------
	//	Input labels
	//---------------------------------------------------------------------------------------------
	$('.textfield').focus(function(event){
		$(this).closest('div').children('.inline').animate( { "opacity" : .5 }, 250 );
	});

	$('.textfield').keydown(function(event){
		$(this).closest('div').children('.inline').fadeOut();
	});

	$('.textfield').blur(function(event){
		if (!$(this).val()) {
			$(this).closest('div').children('.inline').animate( { "opacity" : 1 }, 250 );
		};
	});

//---------------------------------------------------------------------------------------------------------
//	Add submenu indicators
//---------------------------------------------------------------------------------------------------------
	$("nav#mainnav li.i1").find("ul").parent().each(function () {
		$(this).find("a.a1").addClass("showsub");
	});

	//	Overlays
	$(".overlay-trigger").overlay({
		onClose: function(){
			var contents = this.getOverlay().find('.inner');
			if (contents != null) {
				var clone = contents.html();
				 contents.empty();
				 contents.html(clone);
			};

		}
	});
	
	//	Porfolio widget
	$('.show').click(function(){
		var id = $(this).attr('rel');
		$('.portfolio-item').hide()
		$('.portfolio-text').hide()
		$('#'+id).show();
		$('#'+id+'-text').show();
	});
	
//---------------------------------------------------------------------------------------------------------
//	Fix stuff
//---------------------------------------------------------------------------------------------------------
	$('#right .sidebar:last-child').css('border-bottom','none');

	if($.browser.msie && parseInt($.browser.version) < 8){
		$('#footernav ul').css('width','300px');
		$('#footernav li.i0').css('float','left');
	}
	
	if($.support.placeholder == false)
	{
		$('form#search input#findtext').val('Search').focus(function(event){
			$(this).val('');
		}).blur(function(event){
			var value = $(this).val();
			if (value == '') {
				$(this).val('Search');
			};
		});
	}
	
});


