
/**
 * Wallpaper Utility Object
 * 
 * Contains various utlitly functions for the website, such as refreshing advert divs and tracking clicks.
 */
var WallpaperUtility = {
	
	/**
	 * Refreshes the MPUs on the page as provided by the array parameter
	 * 
	 * @param		array		jQuery selectors to the divs to refresh (ie #side-mpu)
	 */
	refreshAdDivs: function(adDivs)
	{
		if(adDivs.length > 0)
		{
			jQuery.each(adDivs, function()
			{
				var ad = jQuery(this + '');	// If you don't do +'' jQuery splits the string into an array...

				// Grab the contents of the script that generates the ad if we don't have it already.
				var script;
				if(!jQuery(':first', ad).is('iframe'))
				{
					// First script is enough to generate the ads
					script = jQuery('script:first', ad);

					// Save the contents of this script tag as we'll need it again later:
					ad.data('script_tag', script.html());
				}

				// Wrap in an iFrame and append:
				var iFrame = jQuery('<iframe />');
				iFrame.css({
					border : '0', 
					zIndex: 999, 
					overflow : 'hidden', 
					width : ad.outerWidth(), 
					height : ad.outerHeight()
				}).attr({
					frameborder : '0', 
					scrolling : 'no', 
					allowtransparency : 'true',
					src: ''
				});

				ad.html('');
				ad.append(iFrame);

				var iframeDoc = iFrame[0].contentWindow || iFrame[0].contentDocument;
				if(iframeDoc.document) {
					iframeDoc = iframeDoc.document;
				}

				// We have to create the script with the iFrame document to avoid the included script using the parent
				// windows document.write function. Mental scope swapping.
				var iScript = iframeDoc.createElement('script');
				iScript.type = 'text/javascript';
				iScript.text = ad.data('script_tag');
				iframeDoc.body.appendChild(iScript);

			});
		}
	},
	
	/**
	 * Tracks a click to this current page manually. Used in galleries and pages with dynamic, but click-worthy content
	 */
	trackClick: function()
	{
		if(typeof s != "undefined") {
			void(s.t());
		}
	
		if(typeof localPageTracker0 != "undefined") 
		{
			localPageTracker0._trackPageview()
		}
	}
}


/* jquery rotator for oct 2010 guest eds */
function swapImages(){
	var $active = $('#myGallery .active');
	var $next = ($('#myGallery .active').next().length > 0) ? $('#myGallery .active').next() : $('#myGallery img:first');
	$next.fadeIn().addClass('active');
	$active.fadeOut(function(){
		$active.removeClass('active');
		  
	});
}	   
		
$('#myGallery').click(function(){
	window.location.href = '/print';
}); 

/** 
*	Most popular Tabs 
**/
$(document).ready(function() 
{
  
	//stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
	{
		$("#site-footer").css("position", "static");
	}
  
	$('#fb-toggle').toggle( // facebook sharing
		function() { 
			$('#fb-wrapper').fadeIn('slow'); 
			$(this).html('Hide Facebook sharing');
		}, 
		function() { 
			$('#fb-wrapper').fadeOut('fast'); 
			$(this).html('Share this on Facebook');
		}	   	
	);
	  

	$('#tw-toggle').toggle( // tweet this
		function() { 
			$('#tw-wrapper').fadeIn('slow'); 
			$(this).html('Hide Twitter');
		}, 
		function() { 
			$('#tw-wrapper').fadeOut('fast'); 
			$(this).html('Tweet this');
		}	   	
		);
	
	
	
	$('#topContent').fadeIn('fast'); // set the div to display as is set to display none in css
	$('#topContent').tabs({
		fx: {
			opacity: 'toggle', 
			duration: 'fast'
		}
	});

	$('#viewMoreMostRead').click(function()
	{
		$('.moreMostRead').live().toggle();
		$('.moreEditorsPicks').live().toggle();
		return false;
	});
	 

		
	setInterval('swapImages()', 3500);
		 
		
	/** add the css class hoverBox to a parent div to make all the child links turn red and back to grey [Roll on HTML5 :) ] **/
	$('.hoverBox').hover(function()
	{
		links = $(this).find('a');
		links.css("color","#b00");
		links.css("text-decoration","underline");
	},
	function()
	{
		$x = 0;
		jQuery.each(hoverboxes, function(){
			var links = $(this).find('a');
			$i = 0;
			jQuery.each(links, function(){
				$(this).css('color', hoverBoxData[$x][$i]['color']);
				$(this).css('text-decoration', hoverBoxData[$x][$i]['text-decoration']);
				$i++;
			});
			$x++;
		});
	});
		
		
	/** store all hover box link css in an array so we can revert back to the original colours later. **/
	var hoverboxes = $('body').find('.hoverBox');
	$x = 0;
	var hoverBoxData 	= [];
	jQuery.each(hoverboxes, function(){
		var links = $(this).find('a');
		$i = 0;
		var originalCSS 	= [];
		jQuery.each(links, function(){
			originalCSS[$i] = [];
			originalCSS[$i]['color'] 			= $(this).css('color');
			originalCSS[$i]['text-decoration'] 	= $(this).css('text-decoration');
			$i++;
		});
		hoverBoxData[$x]					= originalCSS;
		$x++;
	});
	
	
	/**
	 * App prompting
	 */
	if((navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i)) && $.cookie('app_leave_alone') == null)
	{
		var device = (navigator.userAgent.match(/iPhone/i))? 'iPhone' : 'iPod Touch';
		if(confirm('We see you are visiting from an ' + device + '. Would you like to download the Free Wallpaper* App?'))
		{
			$.cookie('app_leave_alone', true, { expires: 14 });
			window.location = 'http://itunes.apple.com/app/wallpaper-magazine/id364309858?mt=8';
		}
		else
		{
			$.cookie('app_leave_alone', true, { expires: 14 });
		}
	}
	else if(navigator.userAgent.match(/iPad/i) && $.cookie('app_leave_alone') == null)
	{
		// The iPad overlay is a bit more complex. The HTML is already in the page, but we need to show it
		// and such.
		var overlay = $('#ipad-wrapper');
		overlay.fadeIn(function() {
			$('#ipad-slider').nivoSlider({
				effect: 'fade',
				pauseTime: 1000,
				animSpeed: 300
			});
		});
		
		var goToAppStore = function(e)
		{
			e.preventDefault();
			$.cookie('app_leave_alone', true, { expires: 14 });
			overlay.fadeOut();
			window.location = $(this).attr('href');
		}
		
		var closeiPad = function(e)
		{
			e.preventDefault();
			$.cookie('app_leave_alone', true, { expires: 14 });
			overlay.fadeOut();
		}
		
		$('#ipad-close').bind('touchstart', closeiPad).bind('click', closeiPad);
		$('#ipad-link a').bind('touchstart', goToAppStore).bind('click', goToAppStore);
	}

});

