// Change the extension (ext1) of an image with a specific DOM selector (inside the element with
// a specific ID) to ext2. -ONLY for IE6 or below  
var homeChannelArray = [];
var currentChannel = 0;

function replaceImgExtension(selector, ext1, ext2) {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ieversion < 7) {	// browser is IE version < 7
			if($(selector)) {
				currPath = $(selector).attr("src");
				if (currPath != null)
					$(selector).attr("src", currPath.replace(ext1, ext2));
			}				
		}
	}
}

function menuInit() {
	// $('#nav ul li ul').append('<div></div>');  though it's the right way to do it, this doesn't work in IE6 :S
}

function homeChannelSwitch(element){
	if(element=="auto"){
		element = homeChannelArray[currentChannel][0];
	}else{
		clearInterval(interval);
	}
	var parentdiv = $($(element).parent().get(0));
	var parentlink = $(parentdiv.parent().get(0));

	if (!parentdiv.hasClass("active")) {
		var linkId, linkSubstring;

		// update the see more link
		$('.slideshow-menu li div').removeClass('active');
		parentdiv.addClass("active");

		// update the channel viewer
		linkId = parentlink.attr("id");
		linkSubstring = linkId.substring(0, linkId.indexOf("-"));
		$('#viewer div.active').removeClass('active');
		$('#' + linkSubstring + '-viewer').addClass("active");
		if(currentChannel < (homeChannelArray.length - 1)){
			currentChannel++;
		}else{
			currentChannel = 0;
		}
	}
}

function homeChannelViewerInit() {
	// Are we targeting the home page (div with class 'home') 
	if ($('#main-wrapper > div.home')) {
		// Change HTML
		var tmpLink = $('#promo-link a').attr('href');
		var tmpTitle = $('#promo-link a').attr('title');
		var tmpLabel = $('#promo-link a').html();
		var tmpHtml = '<div class="active"><a class="refLink">' + tmpLabel + '</a><a class="seeMore" href="' + tmpLink + '">' + tmpTitle + ' &gt;</a></div>';
		$('#promo-link').html(tmpHtml);
		
		tmpLink = $('#twitter-link a').attr('href');
		tmpTitle = $('#twitter-link a').attr('title');
		tmpLabel = $('#twitter-link a').html();
		tmpHtml = '<div><a class="refLink">' + tmpLabel + '</a><a class="seeMore" href="' + tmpLink + '">' + tmpTitle + ' &gt;</a></div>';
		$('#twitter-link').html(tmpHtml);
		
		tmpLink = $('#flickr-link a').attr('href');
		tmpTitle = $('#flickr-link a').attr('title');
		tmpLabel = $('#flickr-link a').html();
		tmpHtml = '<div><a class="refLink">' + tmpLabel + '</a><a class="seeMore" href="' + tmpLink + '">' + tmpTitle + ' &gt;</a></div>';
		$('#flickr-link').html(tmpHtml);
		
		tmpLink = $('#foodieview-link a').attr('href');
		tmpTitle = $('#foodieview-link a').attr('title');
		tmpLabel = $('#foodieview-link a').html();
		tmpHtml = '<div><a class="refLink">' + tmpLabel + '</a><a class="seeMore" href="' + tmpLink + '">' + tmpTitle + ' &gt;</a></div>';
		$('#foodieview-link').html(tmpHtml);
		
		tmpLink = $('#youtube-link a').attr('href');
		tmpTitle = $('#youtube-link a').attr('title');
		tmpLabel = $('#youtube-link a').html();
		tmpHtml = '<div><a class="refLink">' + tmpLabel + '</a><a class="seeMore" href="' + tmpLink + '">' + tmpTitle + ' &gt;</a></div>';
		$('#youtube-link').html(tmpHtml);
		
		// Set Actions (on mouse click)
		$('a.refLink').click(function(){
			homeChannelSwitch(this);
		});
		
		//channel rotation
		$('a.refLink').each(function(){
			homeChannelArray.push($(this));
		});
			// 	currentChannel = 1;
			interval = null;
			//	interval = setInterval("homeChannelSwitch('auto')",5000);
		
	}
}
function homeChannelInterval(){
	homeChannelSwitch();
}
$(function() {
	homeChannelViewerInit();
	replaceImgExtension("#intro img",".png",".gif");      // Header images at the social media pages
	replaceImgExtension("#featured img",".png",".gif");   // CTA images (not to be confused with promo buckets)
	replaceImgExtension("#promo-viewer img",".png",".gif");	// Big promo image in the home page (in the channel viewer)
	
	
	$("#flickr a.fancybox").fancybox({
		'overlayOpacity': 0.8,
		'overlayColor': '#000',
		'showCloseButton': true
	});
	$("#flickr-viewer a.fancybox").fancybox({
		'overlayOpacity': 0.8,
		'overlayColor': '#000',
		'showCloseButton': true
	});
	
	$("#YouTube a.fancybox").fancybox({
		'frameWidth': 480,
		'frameHeight': 390,
		'overlayOpacity': 0.8,
		'overlayColor': '#000',
		'showCloseButton': true
	});

});
$(document).ready(function(){
//Goole Analytics tracking	
	$("#twitter .feed a, #twitter .column a ").click(function (){
		pageTracker._trackEvent('social','exit_link','twitter');
	});
	$("#foodieview .feed a, #foodieview .column a").click(function (){	
		pageTracker._trackEvent( 'social','exit_link','FoodieView');
	});	
	$("#flickr a.fancybox").click(function (){	
		pageTracker._trackEvent( 'social','photo_view','flickr');
	});		
	$("#YouTube a.fancybox").click(function (){	
		pageTracker._trackEvent('social','video_play', this.title);
	});
	$("#follow-me-twitter-cta, #social-twitter #intro a").click(function (){
		pageTracker._trackEvent('social','exit_link','twitter_subscribe');
	});
	$("#foodieview-cta, #social-foodieview #intro a").click(function (){
		pageTracker._trackEvent('social','exit_link','FoodieView_subscribe');
	});
	$("#flickr-cta, #social-flickr #intro a").click(function (){
		pageTracker._trackEvent('social','exit_link','flickr_subscribe');
	});
	$("#youtube-cta, #social-youtube #intro a").click(function (){
		pageTracker._trackEvent('social','exit_link','youtube_subscribe');
	});
	$("#breakfast-builder-cta, #breakfast-builder-promo").click(function (){
		pageTracker._trackEvent('breakfast','exit_link',document.title);
	});
	$("#nut-number").click(function (){
		pageTracker._trackEvent('image','download','badge');
	});
	$(".recipeHeader").toggle(function (){
		var s = $(this).find("h3").text();
		pageTracker._trackEvent('recipes','view', s);
	}, function(){});
	
	

	
});
