/*
///////////////////////////////////////////////////////
Author  : Adam Collison [adam.collison@digitaloverload] 
Agency  : Digital Overload
Client  : Archant
Project : Regional Paper Templates
File    : Main Javascript File
///////////////////////////////////////////////////////
*/
var t;

$(document).ready(function(){

	// IE detection, if its ie then 
	// we cant use the nice slide effects
	var slide = true;
	var showMap = false;
	
	// Detect if we should use the slide animation (IE does not like it so non IE only)
	if($.browser.msie)
	{
		slide = false;
	}

	// Hide anything that should be hidden if JS is enabled
	$('.js-hide').hide();			
		
	// Detect rollovers on tabbed news callouts
	$('.tabbed-news-callouts .tabs li').hover(function() {
		$(this).addClass('hover');													   
	},
	function() {
		$(this).removeClass('hover');													   
	});
	
	// Add shadows to li elements either side of the selected nav item
	$('#navigation li.selected').prev('li').addClass('prev');
	$('#navigation li.selected').next('li').addClass('next');
	
	
	// Start Tabbed News Callouts
	// --------------------------------------------------------------------
	// If its tabbed news callouts, add the tabs as these will only work with javascript enabled.
	$('.tabbed-news-callouts').prepend('<ul class="tabs"><li class="selected"><a href="#showEmail" rel="showEmail">More sport &nbsp;</a></li><li><a href="#showComment" rel="showComment">Non League &nbsp;</a></li><li class="last"><a href="#showRead" rel="showRead">Ipswich Witches &nbsp;</a></li></ul>');
	
	// Hide items in tabbed news list when page loads so the tabs can show and hide them.
	$('.tabbed-news-callouts h2').addClass('inactive');
	$('.tabbed-news-callouts .articles').each(function() {
		if(!$(this).hasClass('active'))
		{
			$(this).addClass('inactive');	
		}
	});
	
	// functionality to show hide sections on click of tabbed news callouts tabs.
	$('.tabbed-news-callouts .tabs li a').click(function() {
		$('.tabbed-news-callouts .tabs li').each(function() {
			$(this).removeClass('selected');
		});
		var rel = $(this).attr('rel');
		$('.tabbed-news-callouts .active').fadeOut('fast',function() {
				$(this).removeClass('active');
				$("#"+rel).fadeIn('fast');
				$("#"+rel).addClass('active');
		});
		$(this).parents('li').addClass('selected');
		$(this).blur();
		return false;
	});
	// --------------------------------------------------------------------
	// End Tabbed News Callouts


	// Start Calendar type a + b 
	// --------------------------------------------------------------------
	// Add the calendar key and show calendar if JS is active
	$('.calendar-type-a .calendar,.calendar-type-b .calendar').append('<p class="current-date">Today</p><p class="event">Event</p>');
	$('.calendar-type-a .calendar,.calendar-type-b .calendar').css({display:'block'});
	$('.calendar-type-a ul').addClass('active');
	
	// Highlight cal cells when you rollover a event relating to that calendar item.
	$('.cal-event-listing li a').hover(function(){
		var id = $(this).attr('rel');
		$("."+id).addClass('hover');
	},
	function() {
		var id = $(this).attr('rel');
		$("."+id).removeClass('hover');
	});
	
	$("input[value='dd/mm/yyyy']").css({color:'#666'});
	$("input[value='dd/mm/yyyy']").click(
		function(){ 
			$(this).css({color:'#000'});
		}
	);
	// --------------------------------------------------------------------
	// End Calendar type a + b 
	
	
	// Start Form Actions 
	// --------------------------------------------------------------------	

	
	
	// Close
	$('.login .close a').click(function() {
		if(slide) {
			$('.login').slideUp('fast');
		} else {
			$('.login').hide();
		}
		return false;
	});
	
	// Open
	if($('form.login').attr('action'))
	{
		$('li.log-in a').click(function() {
			if(slide) {
				$('form.login').slideDown();							  
			} else {
				$('form.login').show();							  
			}
			return false;
		});	
	}
	
	// Close
	$('.register-form .close a').click(function() {
		if(slide) {
			$('.register-form').slideUp('fast');
		} else {
			$('.register-form').hide();
		}
		return false;
	});
	
	// Open
	if($(".register-form").attr('action'))
	{
		$('li.register a').click(function() {
										  
			if(slide) {
				$('.register-form').slideDown();							  
			} else {
				$('.register-form').show();							  
			}
			return false;
		});	
	}
	
	$('.message-panel .close a').click(function() {
		if(slide) {
			$('.message-panel').slideUp('fast');
		} else {
			$('.message-panel').hide();
		}
		return false;
	});
	// --------------------------------------------------------------------	
	// End Form Close Button Actions 
		
	
	// Start Poll Functionality 
	// --------------------------------------------------------------------	
	animatePollAnswers();
	
	$('form.poll-widget button').click(function() {
												
		// AJAX CALL HERE to actually get the results...
		
		// If its an inpage, still get AJAX results but done multiple percentage by 2 just replace border left
		// with actual rounded percentage figure.
		if($(this).parents('form').hasClass('in-page'))
		{
			$('form.poll-widget fieldset').replaceWith('<div class="results"><p class="yes"><strong>Yes</strong> <em style="border-right-width:75px;">75%</em></p><p class="no"><strong>No</strong> <em style="border-right-width:25px;">25%</em></p></div>');
		}
		else
		{
			$('form.poll-widget fieldset').replaceWith('<div class="results"><p class="yes"><strong>Yes</strong> <em style="border-right-width:150px;">75%</em></p><p class="no"><strong>No</strong> <em style="border-right-width:50px;">25%</em></p></div>');
		}
		
		animatePollAnswers();	
		
		return false;									  
	});
	// --------------------------------------------------------------------	
	// End Poll Functionality 
	
	
	// Start Save Search 
	// --------------------------------------------------------------------	
	$('.save-search a').click(function(){
		if(slide) {
			$('.frm-save-search').slideToggle('fast');
		} else {
			$('.frm-save-search').toggle();
		}
		return false;									
	});
	
	$('.saved-searches .actions a').click(function() {
		$(this).parents('li').fadeOut('fast');											   
		return false;
	});
	
	// --------------------------------------------------------------------	
	// End Save Search 
	
	
	// Start View Map 
	// --------------------------------------------------------------------	
	$(".view-map").click(function() {
		var text = $(this).text();
		if(text == 'Hide Map')
		{
			$(this).text('Show Map');
		}
		else
		{
			$(this).text('Hide Map');
		}
		if(!showMap)
		{
			if(slide) {
				$('.map').slideToggle('fast');
			} else {
				$('.map').toggle();
			}
			showMap = true;
			return false;
		}
		else
		{
			if(slide) {
				$('.map').slideToggle('fast');
			} else {
				$('.map').toggle();
			}
			showMap = false;
			return false;
		}
	});
	// --------------------------------------------------------------------	
	// End View Map 
	
	
	// --------------------------------------------------------------------	
	// Start Profile
	$(".profile button[name='new-email-address'],.profile button[name='new-password'],.profile button[name='new-screen-name']").click(function(){
		var itemClass = $(this).attr('name');
		if($('fieldset.'+itemClass).data('visible') || !$('fieldset.'+itemClass).hasClass('hide'))
		{
			$(this).text("Change");
			$('fieldset.'+itemClass).removeData('visible');
			$('fieldset.'+itemClass).hide();
			$('fieldset.'+itemClass).addClass('hide');
			$(this).removeClass('btn-changeable');
			return false;
		}
		else
		{
			$(this).text("Cancel");
			$('fieldset.'+itemClass).data('visible','1');
			$('fieldset.'+itemClass).show();
			$(this).addClass('btn-changeable');
			return false;
		}
	});
	// End Profile
	// --------------------------------------------------------------------	
	
	
	// --------------------------------------------------------------------	
	// Start Rating
	$('.rating ul').hover(function() {
		$(this).children('li').each(function() {
			if($(this).hasClass('active'))
			{
				$(this).removeClass('active');
				$(this).data('wasActive','1');
			}
		});
	}
	,function() {
		$(this).children('li').each(function() {
			if($(this).data('wasActive'))
			{
				$(this).addClass('active');
			}
		});
	});
	// --------------------------------------------------------------------	
	// End Rating
	

	
	
	// --------------------------------------------------------------------	
	// Start Misc
	$('.password-reminder a').click(function() {
		$('.forgotten-password').toggle();
		return false;
	});
	
	$('button.btn').hover(function() {
		$(this).addClass('active');							   
	}
	,function() {
		$(this).removeClass('active');							   
	});
	
	/* This JS is used to give inpage images a title without the need
	to put in additional content through the CMS 
	$('.object-left,.object-right').each(function() {
		if($(this).children('img').attr('alt') != '')
		{
			$(this).data('title',$(this).children('img').attr('alt'));
			$(this).append("<em>" + $(this).data('title') + "</em>");
		}
	}); */
	
	$('.print a, a.print').click(function() {
		window.print();
		return false;
	});
	
	$('.close-window a, a.close-window').click(function() {
		window.close();
		return false;
	});
	
	$('.share').click(function() {
		$('.sharing').toggle();
	});
	
		$('.send-to-friend').click(function() {
		$('#send-to-friend-email').toggle();
	});
	
	// End Misc
	// --------------------------------------------------------------------	
		
	$('#sub-navigation').data('defaultNav','');
	// append datya object with sub nav items
	$('#navigation > li').each(function() {
		
		var secondaryNavigation = $(this).children('ul');
		secondaryNavigation.addClass('secondary-repositioned');
		$(this).data('subNav',secondaryNavigation);
		$(this).children('ul').remove();
		
		if($(this).hasClass('selected'))
		{
			$(this).data('selected',1);									 
			$('#sub-navigation').html($(this).data('subNav'));
			$('#sub-navigation').data('defaultNav',$(this).data('subNav'));
		}
							 
		$(this).mouseover(function() {
			clearTimeout(t);
			$('#navigation .highlight').removeClass('highlight');
			$(this).addClass('highlight');
			$('#sub-navigation').html($(this).data('subNav'));
			rebindRolloverActions(secondaryNavigation);
		});
				
		$(this).mouseout(function() {
			t = setTimeout(detectMouseOut,1000);
		});
						
	});
	
	$('#navigation, #sub-navigation').addClass('repositioned');	
		
	$(".secondary-news-headline .thumb img[align='left'],.feature-content .thumb img[align='left'],.news-headline-c .thumb img[align='left'],.category-detail-b .thumb img[align='left']").addClass('left');
	$(".secondary-news-headline .thumb img[align='right'],.feature-content .thumb img[align='right'],.news-headline-c .thumb img[align='right'],.category-detail-b .thumb img[align='right']").addClass('right');
	
});

function detectMouseOut() {
	$('#sub-navigation').html($('#sub-navigation').data('defaultNav'));
	$('#navigation .highlight').removeClass('highlight');
}

function rebindRolloverActions(navigationalItem)
{
	navigationalItem.mouseover(function() {
		clearTimeout(t);
	});
	
	navigationalItem.mouseout(function() {
		t = setTimeout(detectMouseOut,1000);
	});
}

function animatePollAnswers()
{
	$('.results .yes em').each(function() {
		var amount = $(this).css('border-right-width');
		$(this).css('border-right-width','0');
		$(this).animate({ 
        borderRightWidth: amount
     	}, 1500, "linear");
	});
	
	$('.results .no em').each(function() {
		var amount = $(this).css('border-right-width');
		$(this).css('border-right-width','0');
		$(this).animate({ 
        borderRightWidth: amount
      }, 1500, "linear");
	});
}

function highlightEvent(id)
{
	$('.cal-event-listing li').each(function() {
		if($(this).children('a').attr('rel') == id)
		{
			$(this).addClass('highlight');
		}
	});	
}

function removeHighlights()
{
	$('.cal-event-listing li').each(function() {
		$(this).removeClass('highlight');
	});	
}
