$(document).ready(function() {
  
	// testing section class
	var test1 = '', test2 = 10; // default
	//var test1 = 'sectionBusiness', test2 = 0; // business
	//var test1 = 'sectionEntertainment', test2 = 1; // entertainment
	//var test1 = 'sectionSport', test2 = 2; // sport
	$('body').addClass(test1);
	$('#section ul li').eq(test2).addClass('selected');
	// test code ####################################################
	
	highlightSiteSection.init();
	repositionContent.init();
	styleHeaders.init();
	inputClearing.init();
	pag.init();
	mediaPanelCarousel.init();
	mediaPrint.init();
	mediaPanelLightBox.init();
	sponsorCarousel.init();
	cycleHeroImages.init();
	skyscraper.init();
	
	if($('#tmplHome').length > 0) {
		$('.cPanelMid').ceNormaliser({ parent : '#linksRow' });
		$('.cPanelMid').ceNormaliser({ parent : '#newsRow' });
	}
	
});


var repositionContent = {
	init: function() {
	
		if($('#featuredCollationArticles .cFeaturePanels').length > 0) { $('#content .cols .leftCol').append($('#featuredCollationArticles .cFeaturePanels')); }
		if($('#minorCollationArticles .cPanel').length > 0) { $('#content .cols .leftCol').append($('#minorCollationArticles .cPanel')); }
		if($('#listGrouping #searchResults').length > 0) { $('#content .cols .leftCol').append($('#listGrouping #searchResults')); }
		
		if($('#contentBot .cPanels').length > 0 && $.trim($('#contentBot .cPanels').html()) == '') { $('#contentBot').remove(); }

	}
}


var highlightSiteSection = {
	init: function() {
		
		$('#section li a').each(function() {
			if(window.location.pathname.indexOf($(this).attr('href'), 0) === 0) { 
				$(this).parent().addClass('selected'); 
			}
		});
		
		$('#nav li a').each(function() {
			if( (window.location.pathname == '/' && $(this).attr('href') == '/') || (window.location.pathname != '/' && $(this).attr('href') != '/' && window.location.pathname.indexOf($(this).attr('href'), 0) != -1) ) {
				$(this).parent().addClass('selected');
			}
		});
		
	}
}


var skyscraper = {
	init:function() {
		if($('#skyscraper a').length < 1) { return; }
		$('#skyscraper a').eq(Math.floor(Math.random()*$('#skyscraper a').length)).css('display','block');
	}
}


var cycleHeroImages = {
	
	speed : 600,
	interval : 4000,
	timeout : null,
	
	init: function() {
		if($('#subHomeIntro .img').length > 1) {

			var obj = this;
			$('#subHomeIntro .img').eq(0).addClass('selected');
			$('#subHomeIntro .img').mouseenter(function() {
				if(obj.timeout != null) { obj.clrImgTimeout(); }
			});
			$('#subHomeIntro .img').mouseleave(function() {
				if(obj.timeout == null) { obj.setImgTimeout(); }
			});

			obj.setImgTimeout();
			
		}
	},
	
	setImgTimeout: function() {
		var obj = this;
		obj.timeout = setTimeout(function() { obj.doFade(); }, obj.interval);
	},
	
	clrImgTimeout: function() {
		var obj = this;
		clearTimeout(obj.timeout);
		obj.timeout = null;
	},
	
	doFade: function() {
		
		var obj = this;
		var curr = $('#subHomeIntro .img.selected').eq(0);
		var next = $('#subHomeIntro .img.selected').eq(0).next().length > 0 ? $('#subHomeIntro .img.selected').eq(0).next() : $('#subHomeIntro .img').eq(0);
		
		curr.css('z-index','1');
		next.css('z-index','2');
		
		next.fadeIn(obj.speed, function() { 
			curr.css('display','none');
			curr.removeClass('selected');
			next.addClass('selected'); 
		});
		
		obj.setImgTimeout();
	}
}


var mediaPanelLightBox = {
	
	init: function() {
		if($('#mediaPanel').length < 1) { return; }
		$('#mediaPanel a[rel="imageshow"]').colorbox();

		if($('#colorbox_target').length < 1) {
			$('body').append('<div style="display: none"><div id="colorbox_target" style="width: 640px; padding-top: 5px; padding-left: 6px; padding-right: 6px;height: 386px;"><div id="colorbox_target_video"></div></div></div>'); // originally "580", "400"
		}

		$('#mediaPanel a[rel="videoshow"]').colorbox({
			slideshow : false,
			current:'',
			inline:true, 
			href: "#colorbox_target",
			onComplete:function(){ 
				var flashvars = { 
					file:$(this).attr('href'), 
					autostart:true
				}
				var params = { wmode:'transparent', allowfullscreen:'true', allowscriptaccess:'always' }, attributes = {};
				swfobject.embedSWF("/javascript/player.swf", 'colorbox_target_video', "640", "386", "9.0.0", false, flashvars, params, attributes);	
			}
		});


	}
}

var mediaPrint = {
	init:function() {
		if($('#mediaPanel .pr').length < 1) { return; }
		$('#mediaPanel .pr').click(function(e) {
			e.preventDefault();
			window.print();
		});
	}
}

var mediaPanelCarousel = {
	init:function() {
		if($('#mediaPanel #carousel .slider .slide-item').length > 5) {
			$('#mediaPanel #carousel .slider').slider({
				show 		:	5,
				step		:	5,
				itemSpeed	:	250
			});
		}
	}
}

var sponsorCarousel = {
	init:function() {
		if($('#sponsors .slider .sponsorGroup').length > 1) {
			$('#sponsors .slider').slider({
				show 		:	1,
				step		:	1,
				itemSpeed	:	600,
				autoScroll	:	true,
				autoTimer	:	2200,
				continuous	:	true
			});
		}
	}
}

var pag = {

	maxH : 0,
	numItems : 6,
	
	init: function() {
		if($('#listItems').length < 1) { return; }

		var obj = this;
		var wrapElem = $('div.listItem');
		wrapElem.each(function(i) { if( i % obj.numItems == 0 ) wrapElem.slice(i,i+obj.numItems).wrapAll('<div class="listItems">'); });
		
		$('div.listItems').each(function() { obj.maxH = ($(this).height() > obj.maxH ? $(this).height() : obj.maxH); });
		$('div.listItems:eq(0)').addClass('selected');
		
		var pag = '<p>Page</p><ul>';
		$('div.listItems').each(function(i, elem) {
			$(this).css('min-height',obj.maxH);
			var insertCLass = $(this).hasClass('selected') ? 'selected' : '';
			pag += '<li class="' + insertCLass + '"><a href="">' + (i+1) + '</a></li>'; 
		});
		pag += '</ul>';
		pag += '<a href="" class="prev">Previous</a><a href="" class="next">Next</a>';
		
		$('#searchResults .pag').each(function() { 
			$(this).empty();
			$(this).html(pag); 
		});

		$('#searchResults .pag ul li').each(function() {
			var that = $(this);
			$(this).find('a').click(function(e) {
				e.preventDefault();
				$('div.listItems, #searchResults .pag ul li').removeClass('selected');
				var elemIndex = $(this).parents('ul').find('li').index(that);
				$('div.listItems').eq(elemIndex).addClass('selected');
				$('#searchResults .pag ul').each(function() { $(this).find('li').eq(elemIndex).addClass('selected'); });
				$('html,body').scrollTop($('#searchResults').offset().top); // return to top of results
			});
		});
		
		$('#searchResults .pag .next').click(function(e) {
			e.preventDefault();
			if($(this).parents('.pag').find('li.selected').next().length > 0) { $(this).parents('.pag').find('li.selected').next().find('a').click();} 
			else { $(this).parents('.pag').find('li:eq(0) a').click(); }
		});
		
		$('#searchResults .pag .prev').click(function(e) {
			e.preventDefault();
			if($(this).parents('.pag').find('li.selected').prev().length > 0) { $(this).parents('.pag').find('li.selected').prev().find('a').click();} 
			else { $(this).parents('.pag').find('li:last-child a').click(); }
		});
		
		if($('div.listItems').length < obj.numItems) {
			$('#searchResults .pag .next, #searchResults .pag .prev').remove();
		}
		
	}
}


var styleHeaders = {
	
	init: function() {
		
		if($('#mediaPanel h1').length > 0) {
			var spanHead = $('#mediaPanel h1').html().replace(/(\b[A-za-z0-9]+\b)/g,"<span>$1</span>");
			spanHead = $.trim(spanHead);
			$('#mediaPanel h1').html(  spanHead  );
		}
		
		if($('#shortHeader h1').length > 0) {
			var spanHead = $('#shortHeader h1').html().replace(/(\b[A-za-z0-9]+\b)/g,"<span>$1</span>");
			spanHead = $.trim(spanHead);
			$('#shortHeader h1').html(  spanHead  );
		}
	
	}

}


var inputClearing = {
	init:function(){
		$.each(['#globalNav form'], function(s, e){ // add all the forms/sub elements of forms to check here
			if($(e).length > 0) {
				$(e).find('input[type=text]').focus(function() {
					if(!$(this).data('fv')) { $(this).data('fv', { value: $(this).attr('value'), colour: $(this).css('color') }); }
					if($(this).attr('value') == $(this).data('fv').value) { $(this).attr({value:''}); }
					$(this).css({'color':'#000'});
				});	
				$(e).find('input[type=text]').blur(function() {
					if($(this).attr('value') == '' || $(this).attr('value') == $(this).data('fv').value) { $(this).attr({'value':$(this).data('fv').value}); }
					$(this).css({'color':$(this).data('fv').colour});
				});				
			}
		});
	}
}


