/*
@description Add tab JS
@author VC
@created 2009-02
Notes: Uses jQuery v. 1.1.4 + page_setup.js
*/

jQuery(document).ready(function() {
	initTabbedContent();
});

var myContentSavedItemsAjaxApplied = false;

function initTabbedContent(){
	var tab_boxes = jQuery("#primary-content .tab-content");
	
	if(tab_boxes.length){
		var tab_links = jQuery('#tabbed-header ul.tabs li:not(.noajax)');
		
		//init tab hover effect (function in page_setup.js)
		initSimpleRollovers(tab_links, 'active');
		
		//hide tabs
		tab_boxes.hide();
		jQuery('#primary-content .tab-content .tab-title').hide();
		
		//which one to show first?
		var hash = document.location.hash;
		var flagTabShown = false;
		if(hash.length > 0){
			tab_boxes.each(function(){
				if('#' + jQuery(this).attr('id') == hash){
					tabbedContentClicked(jQuery(hash+'_tab'),tab_boxes, tab_links, false);
					flagTabShown = true;
				};
			});
		};
		if(flagTabShown == false){
			//first one
			jQuery('#primary-content .active-tab-content').show();
		};
		
		//actions
		tab_links.click(function(eventObj){
			tabbedContentClicked(this, tab_boxes, tab_links, true);
			eventObj.preventDefault();
		});
		
		//for My Content
		if (!myContentSavedItemsAjaxApplied) {
			initRemoveMyContentItem('#saved_pages .saved_items .remove_button');
			initRemoveMyContentItem('#saved_searches .saved_items .remove_button');
			myContentSavedItemsAjaxApplied = true;
		}
	}	
}

function tabbedContentClicked(tab, tab_boxes, tab_links, fade){
	var selected = jQuery(tab).find('a').attr('href');

	//add to Google Analytics
	try{ 
		var clickedlink = location.pathname + '_' + escape(jQuery(tab).find('a').text());
		pageTracker._trackPageview(clickedlink); 
	} catch(err) { /*do nothing obvious if GA not set up*/ 
		jQuery('body').addClass('GA_Ajax_Problem'); 
	}
	
	if(fade){
		jQuery('#primary-content .active-tab-content').fadeOut('fast', function(){
			jQuery(selected).fadeIn('fast', function(){
				tabRefresh(selected);
			});
		});
	}else{
		tab_boxes.hide();
		jQuery(selected).show();
		tabRefresh(selected);
	}
	
	//update hash (will cause scrolling in IE on tab click)
	//window.location.replace(selected);	
	
	tab_links.removeClass('currently-active');
	jQuery(tab).addClass('currently-active');
}

function tabRefresh(selected){
	//update class
	jQuery('#primary-content .active-tab-content').removeClass('active-tab-content');
	jQuery(selected).addClass('active-tab-content');
	
	//sifr
	applySifr('#primary-content .active-tab-content');
	
	//swfobject
	loadSWFMovInTab();
	loadSWFAudioInTab();
	loadSlideShowInTab();
	
	//for My Content
	if (!myContentSavedItemsAjaxApplied) {
		initRemoveMyContentItem('#saved_pages .saved_items .remove-button a');
		initRemoveMyContentItem('#saved_searches .saved_items .remove-button a');
		myContentSavedItemsAjaxApplied = true;
	}
}

function loadSlideShowInTab(){
	if (jQuery('#tab-content .slideshow-wrapper').length){
		jQuery.getScript( "/javascript/jquery/jquery.scrollTo-1.4.0-min.js",  function(){	
			jQuery.getScript( "/javascript/jquery/jquery.serialScroll-1.2.1-min.js",  function(){
				jQuery.getScript( "/javascript/jquery/jquery.ifixpng.js",  function(){
					jQuery.getScript( "/javascript/slideshow.js");
				});
			});		
		});
	};
}
function loadSWFMovInTab(){
	jQuery("#tab-content .swfobject-flvplayer").each(function(){
		var this_filename = jQuery(this).find("input[name='filename']").val();
		var this_duration = jQuery(this).find("input[name='duration']").val();
		var this_width = jQuery(this).find("input[name='width']").val();
		var this_height = jQuery(this).find("input[name='height']").val();
		var this_image = jQuery(this).find("input[name='image']").val();
		var this_container_id = jQuery(this).attr('id');
		
		var flashvars = {
			file: this_filename,
			skin: "http://kf2009.torchboxapps.com/swf/jwplayer/skins/kf.swf",
			controlbar: "over",
			lightcolor: "AEBA33",
			backcolor:"333333",
			icons:"false",
			image: this_image,
			duration: this_duration
		};
		var params = {
			menu: "false",
			allowfullscreen: "true",
			allowscriptaccess: "always"
		};
		swfobject.embedSWF("http://kf2009.torchboxapps.com/swf/jwplayer/player.swf", 
		this_container_id, this_width, this_height, "9.0.0", false, flashvars, params, false);
	});
}
function loadSWFAudioInTab(){
	jQuery("#tab-content .swfobject-audioplayer").each(function(){
		var this_filename = jQuery(this).find("input[name='filename']").val();
		var this_duration = jQuery(this).find("input[name='duration']").val();
		var this_container_id = jQuery(this).attr('id');
		
		var flashvars = {
			file: this_filename,
			skin: "http://kf2009.torchboxapps.com/swf/jwplayer/skins/kf_audio.swf",
			lightcolor: "AEBA33",
			backcolor:"333333",
			duration: this_duration
		};
		var params = {
			menu: "false",
			allowscriptaccess: "always"
		};
		swfobject.embedSWF("http://kf2009.torchboxapps.com/swf/jwplayer/player.swf", 
		this_container_id, "250", "27", "9.0.0", false, flashvars, params, false);
	});
}

/* stuff for My Content */
function initRemoveMyContentItem(selector){
	var items = jQuery(selector);
	items.click(function(eventObj){
		/* after submitting the remove request, reload sidebar to reflect removal */
		var ret = jQuery.get(jQuery(this).attr('href'), {}, loadMyContentSidebar);
		var this_li = jQuery(this).parents('li');
		jQuery(this_li).slideUp('normal', function(){jQuery(this_li).remove();});
		eventObj.preventDefault();
	});
}