/*
Name: Corona Extra Blog
Date: 07.03.2011
Author: Anthony J. Smith

*/

$(document).ready(function(){
	
	//Insert Recent Header 
	$("body.home #content div.post:first").after('<h3 id="recentPosts">Recent</h3>');	
		
	//Handle Posts hover functionality	
	$("#content div.contentSlide").hide();
	$("#content div.contentShow a.readMore").hide();	
	$("#popular ul li a.readMore").hide();
			
	$("#content div.post").hover(
	  function () {
		$(this).find("div.contentSlide").show();
		$(this).find("a.readMore").show();
	  },
	  function () {
		$(this).find("div.contentSlide").hide();
		$(this).find("div a.readMore").hide();		
	  }
	);
	
	$("#popular li").hover(
		function () {
			$(this).find("a.readMore").show();
		},
		function () {
			$(this).find("a.readMore").hide();
	
		}
	);
	
	isHome = $("body").hasClass('home');
	if (isHome) {
		widgetLaptopScroll()//Handle Wiget laptop scroller
		popularReadMore()//Inject Read More links into popular posts on homepage
		inputFieldClear()//register form
		flickrHover()
		}
	
	//Add alt class to Author Blocks
	isAuthor = $("#content").hasClass('authors');
	if (isAuthor) {altAuthor()}
	
	//If a Post page check to see if there are any Related links, if not hide the DIV
	isPostPage = $("body").hasClass('single-post');
	if (isPostPage) {relatedLinkCheck()}
	
	//Add _target attr to footer links
	$("#menu-fosters-footer li a").attr("target", "_blank");
	$("ul.blogroll li a").attr("target", "_blank");
	
	//Call header search form setup
	searchHeaderSetup();
	
	//Make external links open in new window
	jQuery("a[title=externalLink]").attr("target","_blank");
	
	
	if($("div.post div.contentSlide").length > 0 || $("div.post div.contentShow").length > 0) {
		previewClickable()//Make the whole preview clickable for sample posts	
	}
	
});

window.onload = function() {
		//Append widget OS check
		OStitle = osDetection();

		$("#widget-download").click(function (){
			_gaq.push(['_trackEvent', 'Carousel', 'Download Widget', 'Downloaded ' + OStitle + ' version']);
		})
};



/**********************
	Scan the popular posts, grab their href and inject that into a new read more link
*******************/
function popularReadMore() {

	//Put class of last on 3rd LI
	$("div#popular li").eq(2).addClass('last');

	var target = $("div#popular li");
	
	$(target).each(function(index) {
    	var aLink = $(this).find("h4 a").attr('href');
		var html = '<a class="readMore" href="' + aLink + '">Read More </a>';
	
		$(this).append(html);
  	});

}

/**********************
	Flickr Hover
*******************/
function flickrHover() {
	var controls = $("div.flickrstream	#flickrControls");
	controls.hide();
	
	$("#wrapper #primary div.flickrstream").hover(function(){
		controls.slideDown();
	},function(){
		controls.slideUp();
	});
}

/**********************
	Scan the popular posts, grab their href and inject that into a new read more link
*******************/
function altAuthor() {
	$("#content div.authorBlock:odd").addClass('alt');
}

/**********************
Detect client's OS when downloading widget to provide correct file
*******************/
function osDetection() {
	
	var OSName="Unknown OS";
	if (navigator.appVersion.indexOf("Win")!=-1) OSName="win";
	if (navigator.appVersion.indexOf("Mac")!=-1) OSName="mac";
	if (navigator.appVersion.indexOf("X11")!=-1) OSName="other";
	if (navigator.appVersion.indexOf("Linux")!=-1) OSName="other";

	var downloadLink = $("a#widget-download");

	if (OSName == "win") {
		downloadLink.attr('href', 'http://coronaextra.com.au/widgetdl/desktopInstaller.exe');
		return "PC";
		} else if (OSName == "mac") {
			downloadLink.attr('href', 'http://coronaextra.com.au/widgetdl/desktopInstaller.dmg');
			return "Mac";
		   } else {
			   alert("I'm sorry we don't support your operating system");
			   return "failed";
   }

}

/**********************
Check if any Related links, if not hide DIV
*******************/
function relatedLinkCheck() {
	
	var linkCount = $("#related-external-links ul.related_exlinks li").length;

	if (linkCount == 0) {
		$("#related-external-links").hide();	
	}

}

/**********************
Setup the search bar in the header
*******************/
function searchHeaderSetup() {
	var input = $("#header #searchform input#s");
	
	input.val("Search corona extra").addClass('empty');
	
	input.focusin(function() {
		if (input.hasClass('empty')) {
        	$(this).removeClass('empty').val("");
		}
    });

	input.focusout(function() {
		var inputSize = $(this).val().length;		
		if (inputSize < 1) {
			input.val("Search corona extra").addClass('empty');	
		}
    });

	
}

/**********************
Scroll through Widget laptop images
*******************/
function widgetLaptopScroll() {
	
	var imgs = $("#wrapper #primary #widgetPanel span.widgetLaptop");
	var length = imgs.length;
	
	imgs.hide();
	imgFader(0);	


	//
	function imgFader(i) {
		i++;
		if (i >= length) i = 0;
		imgs.eq(i).show();
		
		var last = i - 1;
		imgs.eq(last).hide();
		
		setTimeout(function(){
	      imgFader(i);
		}, 5000);
	}
	//
	
}

/**********************
Make whole preview clickable on home page
*******************/
function previewClickable() {
	
	$("div.post div.contentSlide").click(function(){
		var alink = $(this).find("a.readMore").attr("href");
		window.location.href = alink;
	});
	
	$("div#popular li img").click(function(){
		var alink = $(this).parent().find("a.readMore").attr("href");
		window.location.href = alink;
	});
	
	$("div.post div.contentShow").click(function(){
		var alink = $(this).find("a.readMore").attr("href");
		window.location.href = alink;
	});
	
}


/*
	Input field remove default value
------------------------------------------------------------------*/
function inputFieldClear() {
	var input = jQuery("#subForm input");
	
	//Store previous text in data-text attribute
	if (input.hasClass("empty")) {
		for (i=0; i < input.length; i++) {
			input.eq(i).attr("data-default", input.eq(i).val());	
		}
	}

	input.focusin(function() {
		if (jQuery(this).hasClass('empty')) {
			jQuery(this).removeClass('empty').val("");
		}
	});
	
	input.focusout(function() {
		var inputSize = jQuery(this).val().length;		
		var emptyText = jQuery(this).attr("data-default");
		if (inputSize < 1) {
			jQuery(this).val(emptyText).addClass('empty');	
		}
	});
}






