/*=============================================================
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');
	});
	
	//setInterval(function() {
	//	if( !console || !console.log )
	//		return;
	//	
	//	var diff =  $('#content_wrapper').height() - ($('#london_link_').height() - 90);
	//	
	//	var subContent = $('#sub_content'),
	//		subContentHeight = subContent.height(),
	//		subContentOffset = subContent.offset();
	//	
	//	console.log( diff, (subContentHeight - diff) + 60 );
	//	
	//	$('#footer').css( 'marginTop', ((subContentHeight - diff) + 60) + 'px' );
	//	
	//}, 500);
	
});

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');
	});
}