/*=============================================================
Type:				Ahead of the Game Website
Created by:			Strawberrysoup
Contact website:	http://strawberrysoup.co.uk/
Copyright: 			(c) 2008 Mark Perkins	
---------------------------------------------------------------
Creation Date:		04-08-2008
=============================================================*/

maxIncADec = 2;

function createCookie(name,value,days) {
        if (days) {
               var date = new Date();
               date.setTime(date.getTime()+(days*24*60*60*1000));
               var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
}
 
function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
               var c = ca[i];
               while (c.charAt(0)==' ') c = c.substring(1,c.length);
               if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

function readCookieAsInt(name) {
	return parseInt(readCookie(name));
}
 
function eraseCookie(name) {
        createCookie(name,"",-1);
}
function cookieExist(name) {
                if (readCookie(name) != null) {
                                return true;
                } else {
                                return false;
                }
}
var aotg = function()
{
	
	var initSubMenu = function()
	{
		$(".acc li a").click(function(e){
			location.href = $(this).attr("href");
			return false;
		})
		$("#sub_nav ul#news_list").show();
		$("#sub_nav .current ul").slideDown();
		
		$("#feature_slide").click(function(){
			$("#sub_nav ul:not(#news_list)").slideUp()
			$(this).find("ul").slideDown();
	
			return false;
		})
		
		$("body").click(function(){
			$("#sub_nav ul:not(#news_list)").slideUp();
		})
	}
	
	var ieFixMenu = function()
	{
		$("#main_nav li").mouseover(function(){
			$(this).addClass("hover");
		}).mouseout(function(){
			$(this).removeClass("hover");
		});
		
		$("#main_nav ul li:first-child a").css("margin-left","0");
	}
	
	var initLinks = function()
	{
		$("#main_content a").click(function(){

			if ( $(this).attr("href").indexOf( base ) !== -1 || $(this).parent("li").hasClass("acc") ) return true;
			else {
				Shadowbox.open({
					player : 'iframe',
					content : $(this).attr("href")
				})
				return false;
			}
		})
	}
	
	var initGal = function()
	{
		$(".gal_item table").hide();
		$(".gal_item h2").click(function(){
			$(this).next("table").toggle();
		});
	}
	
	var buildCaptions = function()
	{
		$("#main_content img").each(function(){
			if(/ /.test(jQuery(this).attr("title"))) {
			var title_parts = $(this).attr("title").split("-");
			if ( title_parts.length == 2 )
			{
				var width = $(this).width();
				var floatcss = ($(this).attr('align')) ? $(this).attr('align') : '';
				$(this).wrap('<div style="width:'+width+'px; padding-bottom: 5px; text-align: center; padding-bottom: 3px; float: ' + floatcss + '" class="'+$(this).attr("class")+'"></div>');
				var caption = $('<br /><span class="caption" style="padding: 0 15px 0 15px">'+title_parts[1]+'</span>');
				$(this).parent("div").append( caption );
				$(this).attr( "title", title_parts[0] );
			}
			}
		});
	}
	
	return {

		init : function()
		{
				$(document).ready(function(){
				  // Reset Font Size
				  var originalFontSize = $('#content_wrapper').css('font-size');
				    $(".resetFont").click(function(){
				    $('#content_wrapper').css('font-size', originalFontSize);
						window.currentTextIncreases = 0;
						createCookie('textIncreases', 0);
					createCookie('textIncreases', 0);
				  });
				  // Increase Font Size
				  $(".increaseFont").click(function(){
					if(window.currentTextIncreases < maxIncADec) {
				    	var currentFontSize = $('#content_wrapper').css('font-size');
				    	var currentFontSizeNum = parseFloat(currentFontSize, 10);
				    	var newFontSize = currentFontSizeNum*1.2;
				    	$('#content_wrapper').css('font-size', newFontSize);
						createCookie('textIncreases', readCookieAsInt('textIncreases') + 1);
						window.currentTextIncreases += 1;
					}
				    return false;
				  });
				  // Decrease Font Size
				  $(".decreaseFont").click(function(){
					if((window.currentTextIncreases) > 0) {
				    	var currentFontSize = $('#content_wrapper').css('font-size');
				    	var currentFontSizeNum = parseFloat(currentFontSize, 10);
				    	var newFontSize = currentFontSizeNum*0.8;
				    	$('#content_wrapper').css('font-size', newFontSize);
						createCookie('textIncreases', readCookieAsInt('textIncreases') - 1);
						window.currentTextIncreases -= 1;
					}
				    return false;
				  });
				  $('.tahoma').click(function() { $('body').attr('class', 'tahoma'); createCookie('fontStyle', 'tahoma'); });
				  $('.verdana').click(function() { $('body').attr('class', 'verdana'); createCookie('fontStyle', 'verdana'); });
				  $('.arial').click(function() { $('body').attr('class', 'arial'); createCookie('fontStyle', 'arial'); });
				  if(cookieExist('fontStyle')) {
				  	$('body').attr('class', readCookie('fontStyle'));
				  }
			});
			initSubMenu();
			ieFixMenu();
			try {
				Shadowbox.init();
					initGal();
					$("input[name=name]").focus();
				$(".gallery_pic a").tooltip({showURL:false});
			} catch(e) {
				
			}
			buildCaptions();
			
			
		}

	}


}();
$(function(){
	window.currentTextIncreases = 0;
	$('.text_only').click(function() {
		createCookie('text_only', '1');
		location.reload(true);
	});
	$('.activate_styles').click(function() {
		eraseCookie('text_only');
		location.reload(true);
	});
	aotg.init();
	if(!cookieExist('textIncreases')) {
		createCookie('textIncreases', 0);
	} else {
		if(readCookieAsInt('textIncreases') > 0) {
			textI = 0;
			cookieRead = readCookieAsInt('textIncreases');
			createCookie('textIncreases', 0)
			while(textI < cookieRead) {
				$(".increaseFont").click();
				textI += 1;
			}
		} else if(readCookieAsInt('textIncreases') < 0) {
			textI = 0;
			cookieRead = readCookieAsInt('textIncreases');
			createCookie('textIncreases', 0)
			while(textI > cookieRead) {
				$(".decreaseFont").click();
				textI += -1;
			}
		}
	}
	$('a[href$=.pdf]').each(function() {
		$(this).attr('target', '_blank');
	});
	
	$('#main_nav ul li').hover(function() {
		// check for current
		if($(this).hasClass('current')) {
			
		}
		else
		{
		$(this).parent('ul').find('li.show').removeClass('show');
		$(this).parent('ul').find('li.current').find('ul.sub_menu').css('display','none');
		$(this).addClass('hover');
		$(this).find('.sub_menu').addClass("show").css('display','block');
			// console.log($(this).find('.sub_menu').attr('class'));
		}
		
	},function() {
		$(this).find('.sub_menu').removeClass("show").css('display','none');
		$(this).removeClass('hover');
		if($(this).parent('ul').find('li.current').length > 0) {
			$(this).parent('ul').find('li.current').find('ul.sub_menu').css('display','block');
		}
	});
	
	$('input.toggle, textarea.toggle').live('focus', function(){
		if(this.defaultValue == this.value){
			this.value = '';
		}
	}).live('blur', function(){
		if(this.value == ''){
			this.value = this.defaultValue;
		}
	});
	
	if($('#feedback-dd #feedback-container').length > 0) {
		if($('#feedback-dd #feedback-container').html() != '') {
			$('#feedback-dd').slideDown(function() {
				setTimeout(function(){
					$('#feedback-dd').slideUp();
					$('#feedback-dd #feedback-container').html('');
				},6000);
			});
			
		}
	}
	
});

if( !cookieExist('text_only') ) {
	$(window).load(function() {
		newheight = $("#main_content").height() - ($('#london_link').height() + 200);
		$("#london_link").css("margin-top", newheight );
	
		// $('#footer').css('margin-top', '150px')
		$('#london_link_').css('top', '-' + ($('#london_link_').height() + 100) + 'px');
		
		var windowHeight = $(window).height(),
		share = $('body').find('#share_this_widget'),
		$s_height = share.height();
		
		$tmp = (windowHeight / 2) - ( ($s_height / 2) + 30);
		share.css('top',$tmp + 'px');
		setTimeout(function() {
			share.fadeIn();
		},100);
		
	});
}
