jQuery.noConflict();
jQuery(function($) {
	// ===== Tabs Rollovers ===============================================================================
	var tabRollOver = $('.tabs img');
	tabRollOver.each(function(){
		if($(this).attr('id') != 'tabsActive'){
			$(this).hover(
				function(){ $(this).attr('src',$(this).attr('src').replace("_inactive","_active")); },
				function(){ $(this).attr('src',$(this).attr('src').replace("_active","_inactive")); }
			)
		}
	});

	// ===== Global Nav Rollovers ===============================================================================
	var navRollOver = $('.globalnav img');
	navRollOver.each(function(){
		$(this).hover(
			function(){ $(this).attr('src',$(this).attr('src').replace("_off","_on")); },
			function(){ $(this).attr('src',$(this).attr('src').replace("_on","_off")); }
		)
	});

	
	//if (swfobject.hasFlashPlayerVersion("7.0.0") && $('siteHeader')) {
		/*var att = { data:"flash/savage.swf", width:"891", height:"336" };
		var par = { wmode:"transparent", allowScriptAccess:"always", bgcolor:"#000000"};
		var id = "siteHeader";
		swfobject.createSWF(att, par, id);*/
		
	//}
	
	
	// ===== GLOBAL SEARCH HEADER =====
	var redirectURL = "http://outdoorchannel.com/Search.aspx?SearchType=Global&Search="
	// ===== GLOBAL: Trap Enter Key for Search Redirect ==========
	$('.globalSearch input:text').keydown(function(e){
		if (e.keyCode == 13) {
			// submit  field value to search page
			window.location = redirectURL + $(this).val();
			return false;
		}
	});
	$('.globalSearch input.btn').click(function(){
		var val = $('.globalSearch input:text').val();
		window.location = redirectURL + val;
	});
	
	
	
	if ($("#videoGallery") && $("#photoGallery")) {
		var videoScript = $('#tabItems div:first').html(); 
		$('#tabItems div').hide(); // Hide all divs
		$('#tabItems div:first').show(); // Show the first div
		$('#tabs ul li:first').addClass('active'); // Set the class of the first link to active
		$('#tabs ul li a').click(function(event){
			  
										  
		var tab = $(this).text()				  
		$('#tabs ul li').removeClass('active'); // Remove active class from all links
		$(this).parent().addClass('active'); //Set clicked link class to active
		var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
		if (tab == "VIDEOS"){
			/*$(currentTab).html(videoScript)*/
		} else {				
			/*$("#tabItems div:first").empty()*/
		}
		$("#tabItems div:visible").hide()
		$(currentTab).show() 
		event.preventDefault();	
		return false;
			
		});


	
}
	
	
	
	/* insert two small ads next to tabs */
	loadEpisodeData('/shows/episodes.xml');
});
/* functions for loading episode data */
function loadEpisodeData(data){
	jQuery.get(data,function(data){
    	insertEpisodes(data);
	});
}

function insertEpisodes(xml){
	
	var episodeHeadline=jQuery('#current_episode h1') ;
	var episodeText=jQuery('#current_episode div.text') 
	//episodeBox.html("TEST");
	var episodeXML =  xml.getElementsByTagName('shows');
	if (episodeXML.length){    
		var show =  xml.getElementsByTagName('UMF')[0];
		//var showName =  show.getAttribute('id');
		//if (showName == "DuckCommander"){
			/* get xml values */
			var showHeadline =  show.getElementsByTagName('headline')[0].firstChild.nodeValue;
			var showSubHeadline =  show.getElementsByTagName('subhead')[0].firstChild.nodeValue;
			var showLink =  show.getElementsByTagName('link')[0].firstChild.nodeValue;
			var showDescription =  show.getElementsByTagName('description')[0].firstChild.nodeValue;
			var showTimes =  show.getElementsByTagName('times')[0].firstChild.nodeValue;
			/* build html */
			episodeHeadline.html(showHeadline);
			var tempHTML = "<p><a href='"+showLink+"'><span style='font-size:13px'><strong>"
					+ showSubHeadline + "</strong><br />" + showDescription
					+ "<br /><br /></span><strong>"+showTimes+"</strong></a>";
			episodeText.html(tempHTML);





		//}
	}








}
/* END: functions for loading episode data */

function loadData(data){
	jQuery.get(data,function(data){
    	insertAds(data);
	});
}

var leftAdHTML = "";
var rightAdHTML = "";

function insertAds(xml){
	var leftAd = jQuery('.adspace_180x30_left');           
	var rightAd = jQuery('.adspace_180x30_right');
	
	var AdXML =  xml.getElementsByTagName('Ad');
	for (var y=0,y1=AdXML.length; y<y1;y++){      
		var tempAdHTML = "";
		var tempAd =  xml.getElementsByTagName('Ad')[y];
		try {
			var tempType = xml.getElementsByTagName('Ad')[y].getAttribute('type');
			var tempURL =  xml.getElementsByTagName('Ad')[y].getElementsByTagName('URL')[0].firstChild.nodeValue;
			var tempURLTarget = "";
			if (xml.getElementsByTagName('Ad')[y].getElementsByTagName('URLTarget')[0].hasChildNodes()){
				tempURLTarget = xml.getElementsByTagName('Ad')[y].getElementsByTagName('URLTarget')[0].firstChild.nodeValue;
			};
			
			var tempImage = xml.getElementsByTagName('Ad')[y].getElementsByTagName('Image')[0].firstChild.nodeValue;
			var tempImageAlt = "";
			
			if (xml.getElementsByTagName('Ad')[y].getElementsByTagName('URLTarget')[0].hasChildNodes()){
				tempImageAlt = xml.getElementsByTagName('Ad')[y].getElementsByTagName('ImageAlt')[0].firstChild.nodeValue;
			};
			if (tempType == "left"){
				leftAdHTML = formatAdXML(tempType, tempURL, tempURLTarget,tempImage, tempImageAlt);
			} else if (tempType == "right"){
				rightAdHTML = formatAdXML(tempType, tempURL, tempURLTarget,tempImage, tempImageAlt);
			};
		} catch (err){
			//console.log(err);
		}
	}
	
	leftAd.each(function(element){
		element.innerHTML = leftAdHTML;
	});
	rightAd.each(function(element){
		element.innerHTML = rightAdHTML;
	});

}

function formatAdXML(tempType, tempURL, tempURLTarget,tempImage, tempImageAlt){
	//console.log(tempType+" "+tempURL+" "+tempURLTarget+" "+tempImage+" "+tempImageAlt);
	var HTMLText = "";
	if (tempURL != ""){
		HTMLText = '<a href="'+tempURL+'"';
		if (tempURLTarget != ""){
			HTMLText = HTMLText + ' target="'+ tempURLTarget +'"';
		};
		HTMLText = HTMLText + '>';
	};
	if (tempImage != ""){
		HTMLText = HTMLText + '<img src="'+tempImage+'"';
		if (tempImageAlt != ""){
			HTMLText = HTMLText + '" alt="'+ tempImageAlt +'"';
		};
		HTMLText = HTMLText + ' border="0" />';
		//<img src="/~/media/Images/Ads/180x30/Free Satellite Hat Button.ashx" alt="Satellite Viewers" border="0"/>
	};
	if (tempURL != ""){
		HTMLText = HTMLText + "</a>";
	};
	return HTMLText;
}

// =======




