/********************************************************************************\
	On Load Events, initiate jQuery apps on selected items
\********************************************************************************/
$(function() {
	$('#gw-nav .wrapper > ul').gwMenu();
	$().gwReload({ seconds:6000 });
	$('.gw-carousel').gwCarousel({show: 3, showPageControls:true});
	$('.gwtabs').each(function(){ 
		$(this).gwTabs() 
	});
	$('.sf-showcase').gwSectionFront();
});



/********************************************************************************\
	Doubleclick variables 
\********************************************************************************/
var axel = Math.random() + "";
var ord = axel * 1000000000000000000;
var sgi_ord = Math.random()*10000000000000000;
var sgi_tile = 1;



/********************************************************************************\
	Tabbed Content
		- content should have one ul with list of links
		- grabs these links and associates them with <div>s of same class
\********************************************************************************/
jQuery.fn.gwTabs = function() {
	var content = this;
	//first div is visible content
	var visible = $('div:first', content)[0];
	
	//grab list's links and cycle through each
	$('>ul>li>a', content).each(function(){
		//grab window with same class
		var link_class = $(this).attr('class');
		//careful, must make class pair unique and not a substring of another
		var selector = 'div[class*="' + link_class + '"]';
		var window = $(selector, content)[0];
		if(!window) return true; //link does not have window pair
		
		//activate link
		$(this).click(function(e){
			e.preventDefault();
			// remove all active, add to current
			$('>ul>li>a', content).removeClass('active');
			$(this).addClass('active');
			$(window).toggleClass('hide');
			$(visible).toggleClass('hide');
			visible = window;
		});
		
		//add active class to link
		if($(visible).hasClass($(this).attr('class')))
			$(this).addClass('active');		
	});	
};


/********************************************************************************\ 
	Section Front Showcase
\********************************************************************************/
jQuery.fn.gwSectionFront = function(sfDefaults) {
	var sfDefaults = jQuery.extend({		
	}, sfDefaults);
	
	var thumbs = $(this).find('> ul > li');
	var visible = $(this).find('.item')[0];
	
	// Add click events to each sub story thumb
	thumbs.each(function(){
		if(this.id){
			var item = $('#show-' + this.id);
			if(item.length){
				$(this).click(function(e){
					e.preventDefault();
					$(visible).css({display: 'none' });
					$(item).css({display: 'block' });
					visible = item;
				});
			}	
		}
	});
}


/********************************************************************************\
	Ad Refresh app
\********************************************************************************/
jQuery.fn.gwReload = function(reloadDefaults){
	var reloadDefaults = jQuery.extend({
		seconds: 300
	}, reloadDefaults);
	
	var adCount = 1; //used for position and tiling
	var num=1;
	var timer = setInterval(
		function(){			
			var random_ord = (Math.random() + "") * 1000000000000000000;
			//define which ads to reload:
			gwAd({ad:'.homepage #gw-header-ad', width:728, height:90, ord:random_ord });
			gwAd({ad:'.homepage .ad-300x250 div', width:300, height:250, ord:random_ord });
			gwAd({ad:'.homepage .ad-160x600 div', width:160, height:600, ord:random_ord });
			adCount = 1;
			//if(++num>3) clearInterval(timer); //testing
		}, 
		reloadDefaults.seconds*1000
	);
	
	/* ------------------------------------------------------------------
	Create new ad using an iframe:
		- creates new iframe containing ad
		- or refreshes already created iframe with new ord
	------------------------------------------------------------------ */	
	function gwAd(adDefaults){
		var adDefaults = jQuery.extend({
			ad: false,
			width: false,
			height: false,
			pos: 1,
			tile: 1,
			ord: (Math.random() + "") * 1000000000000000000
		}, adDefaults);
		
		//error checking
		if(!adDefaults.ad || !adDefaults.width || !adDefaults.height){ return; }
		
		var url = '/ad/?size=' +  adDefaults.width + 'x' + adDefaults.height + ';'
			+ 'ord=' + adDefaults.ord + ';';
			
		var ads = $(adDefaults.ad);
		ads.each(function(){
			var ad = $(this)
			var src = url + 'tile=' +  adCount + ';' + 'pos='  +  adCount;
			var iframe = ad.find('iframe');			
			if(iframe.length < 1){				
				//create iframe
				var html = 
					'<iframe frameborder="0" scrolling="no"' +
					'width="' + adDefaults.width + '" ' +
					'height="' + adDefaults.height + '" ' +
					'src="' + src + '"' +
					'></iframe>';
				ad.html(html);
			}
			else {
				//iframe already created, refresh src				
				$(iframe[0]).attr('src', src);
			}			
			++adCount;
		});
	}
	
}

