//This file contains a the drop down menu script, a script to fix the z-index issues that occur with the slider/navs in IE, and initializes the colorbox and innerfade script.

//Drop Down Loader
$(document).ready(function(){

	$("ul.topnav li span").hover(function() { //When trigger hovered over...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").stop().slideDown('fast').show('slow', function(){
			$(this).height("auto");
		}); //Drop down the subnav on click


		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").stop().slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

});

//Fixes Z-Index Issues
$(function() {
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
});


//Initializes the Innerfade Script	
$(document).ready(
	function(){
		$('#slides').innerfade({
			speed: 1000,
			timeout: 16000,
			type: 'sequence',
			containerheight: '283px'
		});
});


//Initializes the FancyBox
$(document).ready(function() { /* This is basic - uses default settings */ $("a#single_image").fancybox(); /* Using custom settings */ $("a#inline").fancybox({ 'hideOnContentClick': true }); $("a.group").fancybox({ 'zoomSpeedIn': 500, 'zoomSpeedOut': 500, 'overlayShow': false }); }); 

/* Configuration: */

var tabs = {
	"Twitter" : {
		"feed"		: "http://twitter.com/statuses/user_timeline/101648400.rss",
		"function"	: twitter
	},
	
	"Blog Updates": {
		"feed"		: "http://feeds.feedburner.com/embunweb",
		"function"	: rss
	},
	
	"Latest Videos": {
		"feed"		: "http://gdata.youtube.com/feeds/base/users/adminEW1/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile",
		"function"	: rss
	}
}

var totalTabs;
$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	/* Counting the tabs */
	totalTabs=0;
	$.each(tabs,function(){totalTabs++;})
	

	$('#feedWidget').show().mouseleave(function(){
		
		/* If the cursor left the widet, hide the drop down list: */
		$('.dropDownList').remove();
		$('#activeTab').removeClass('hover');

	}).mouseenter(function(){
		
		if(totalTabs>1) $('#activeTab').addClass('hover');
		
	});

	$('#activeTab').click(showDropDown);

	/* Using the live method to bind an event, because the .dropDownList does not exist yet: */
	$('.dropDownList div').live('click',function(){
		
		/* Calling the showDropDown function, when the drop down is already shown, will hide it: */
		showDropDown();
		showTab($(this).text());
	});
	
	
	/* Showing one of the tabs on load: */
	showTab('Twitter');
	
});


function showTab(key)
{
	var obj = tabs[key];
	if(!obj) return false;
	
	var stage = $('#tabContent');
	
	/* Forming the query: */
	var query = "select * from feed where url='"+obj.feed+"' LIMIT 3";
	
	/* Forming the URL to YQL: */
	var url = "http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent(query)+"&format=json&callback=?";
	
	$.getJSON(url,function(data){

		stage.empty();

		/* item exists in RSS and entry in ATOM feeds: */
		$.each(data.query.results.item || data.query.results.entry,function(){
			try{
				/* Trying to call the user provided function, "this" the rest of the feed data: */
				stage.append(obj['function'](this));
				
			}
			catch(e){
				/* Notifying users if there are any problems with their handler functions: */
				var f_name =obj['function'].toString().match(/function\s+(\w+)\(/i);
				if(f_name) f_name = f_name[1];
				
				stage.append('<div>There is a problem with your '+f_name+ ' function</div>');
				return false;
			}
		})
	});
	
	$('#activeTab').text(key);
}

function showDropDown()
{
	if(totalTabs<2) return false;
	
	if($('#feedWidget .dropDownList').length)
	{
		/* If the drop down is already shown, hide it: */
		$('.dropDownList').slideUp('fast',function(){ $(this).remove(); })
		return false;
	}
	
	var activeTab = $('#activeTab');
	
	var offsetTop = (activeTab.offset().top - $('#feedWidget').offset().top )+activeTab.outerHeight() - 5;
	
	/* Creating the drop down div on the fly: */
	var dropDown = $('<div>').addClass('dropDownList').css({

			'top'	: offsetTop,
			'width'	: activeTab.width()
	
	}).hide().appendTo('#feedWidget')
	
	$.each(tabs,function(j){
		/* Populating the div with the tabs that are not currently shown: */
		if(j==activeTab.text()) return true;
		
			$('<div>').text(j).appendTo(dropDown);
	})
	
	dropDown.slideDown('fast');
}

function twitter(item)
{
	/* Formats the tweets, by turning hashtags, mentions an URLS into proper hyperlinks: */
	return $('<div>').html(
			formatString(item.description || item.title)+
			' <a href="'+(item.link || item.origLink)+'" target="_blank">[tweet]</a>'
	);
}

function rss(item)
{
	return $('<div>').html(
			formatString(item.title.content || item.title)+
			' <a href="'+(item.origLink || item.link[0].href || item.link)+'" target="_blank">[read]</a>'
	);
}

function buzz(item)
{
	return $('<div>').html(
			formatString(item.content[0].content || item.title.content || item.title)+
			' <a href="'+(item.origLink || item.link[0].href || item.link)+'" target="_blank">[read]</a>'
	);
}

function formatString(str)
{
	/* This function was taken from our Twitter Ticker tutorial - http://tutorialzine.com/2009/10/jquery-twitter-ticker/ */
	str = str.replace(/<[^>]+>/ig,'');
	str=' '+str;
	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	return str;
}
