 // JQUERY - CUSTOM.JS

$(document).ready(function() {		

	// Since we have javascript enabled, then all classes with has javascript gets removed
	$('.has_javascript').hide();
	
	//*****************************************************************************************//	
	// BOOKMARK
	//*****************************************************************************************//

	$("a.jQueryBookmark").click(function(e){
		e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
		var bookmarkUrl = this.href;
		var bookmarkTitle = this.title;
 
		if (window.sidebar) { // For Mozilla Firefox Bookmark
			//window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
			alert('Dear Firefox user, please press CTRL+D to Bookmark this page!');
		} else if( window.external || document.all) { // For IE Favorite
			window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
		} else if(window.opera) { // For Opera Browsers
			$("a.jQueryBookmark").attr("href",bookmarkUrl);
			$("a.jQueryBookmark").attr("title",bookmarkTitle);
			$("a.jQueryBookmark").attr("rel","sidebar");
		} else { // for other browsers which does not support
			 alert('Your browser does not support this bookmark action');
			 return false;
		}
	});
	
	//*****************************************************************************************//	
	// FADE IN - FADE OUT
	//*****************************************************************************************//

	// Microsoft has hard time with transparent PNG files, so fuck them
	if (!($.browser.msie))  {
	
		// Get what page you are coming from
		// If picture page, must also fade in topline and top
		// In url I added a frompage ampersand 
		var fromwhaturl=getURLParameterByName("frompage");

	    // A - FADE IN           
  		var $javascript_note = $('#javascript_note');
  		var $entire = $('#entire_wrapper');
  		var $topline = $('#topline_wrapper');
  		var $top = $('#top_wrapper');
  		var $middle = $('#middle_wrapper');
  		var $bottom = $('#bottom_wrapper');
  		var $bottomline = $('#bottomline_wrapper');
		var $center_picture = $('#center_picture_wrapper');
		
		// The unload and .hide solve the flicker problem some browsers have.
   		$(window).bind("unload", function() {});
		$javascript_note.css("display","none");
  		$javascript_note.hide();
		if (fromwhaturl == "from_display_picture") {
			$topline.css("display","none");
  			$topline.hide();
			$top.css("display","none");
  			$top.hide();
		}
		$middle.css("display","none");
  		$middle.hide();
		$bottom.css("display","none");
  		$bottom.hide();
		$bottomline.css("display","none");
  		$bottomline.hide();
		$center_picture.css("display","none");
  		$center_picture.hide();
	
		// WAIT TILL THE DOM LOADS (images) BEFORE YOU fade 
		$(window).load(function(){
       		$javascript_note.fadeTo(500,1);
			if (fromwhaturl == "from_display_picture") {
       			$topline.fadeTo(500,1);
       			$top.fadeTo(500,1);
			}
       		$middle.fadeTo(500,1);
			$bottom.fadeTo(500,1);
			$bottomline.fadeTo(500,1);
			$center_picture.fadeTo(500,1);
  		});
		
		// B - FADE OUT (But not on class="ignorefade"
		// But if href is #, its not a redirect. So make a class "ignorefade" to ignore the fade
		$('a').click(function(event){	
			if ($(this).hasClass("going_external")) {
				// remove everything
    	  				event.preventDefault();
       					linkLocation = this.href;
						$entire.fadeTo(250,0,redirectPage);
			}
			else {
				if ($(this).hasClass("leaving_display_picture_page")) {
    	  			event.preventDefault();
       				linkLocation = this.href;
					$center_picture.fadeTo(250,0,redirectPage);
				}
				else {
					// Normal pages
					// Good example why you use this
					if (!($(this).hasClass("ignorefade"))) {
    	  				event.preventDefault();
       					linkLocation = this.href;
						$javascript_note.fadeTo(250,0);
						// If going to picture page, then add tops to fade
						if ($(this).hasClass("going_to_display_picture_page")) {
							$topline.fadeTo(250,0);
							$top.fadeTo(250,0);
						}
						$middle.fadeTo(250,0);
        				$bottom.fadeTo(250,0);
        				$bottomline.fadeTo(250,0,redirectPage);
					}
				}
			}
		});
	}
	
	function redirectPage() {
     	window.location = linkLocation;
  	}
	
	function getURLParameterByName(name) {
    	var match = RegExp('[?&]' + name + '=([^&]*)')
        .exec(window.location.search);
	    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
	}
	
	//*****************************************************************************************//	
	// USER CHOOSE FONT - SAVES INFO IN COOKIE
	//*****************************************************************************************//

	var font_menu_open = 0;
	var originalFont = $('body').css('font-family');

	// SET A COOKIE TO REMEMBER ACROSS ALL PAGES
	var $cookie_font_used_name = "font_used";
	
	if($.cookie($cookie_font_used_name)){
		var $currentfont = $.cookie($cookie_font_used_name);
		$("body").css({'font-family' : $currentfont});
	} else {
		$.cookie($cookie_font_used_name, originalFont);
	}
	
	// Change Font
	$("#chooseFont").click(function(){
		
		// Show the pull down menu with the font choices.
		if (font_menu_open) {
			$("#font_choices_menu").fadeTo(500,0,function(){
				$("#font_choices_menu").hide();
			});  
			font_menu_open = 0;
		}
		else {
			$("#font_choices_menu").fadeTo(500,1);  
			font_menu_open = 1;
		}
		
			// Bold the current font - also need to unbold because we have two menues doing the same thing.
			if ($.cookie($cookie_font_used_name) == "Nobile") {$("#Google_Nobile").css({'background-color' : "#999"});} else {
				$("#Google_Nobile").css({'background-color' : "#ccc"}) }
			if ($.cookie($cookie_font_used_name) == "Arial, Helvetica, sans-serif") {$("#Helvetica").css({'background-color' : "#999"});} else {
				$("#Helvetica").css({'background-color' : "#ccc"}) }
			if ($.cookie($cookie_font_used_name) == "Tahoma, Geneva, sans-serif") {$("#Tahoma").css({'background-color' : "#999"});} else {
				$("#Tahoma").css({'background-color' : "#ccc"}) }
			if ($.cookie($cookie_font_used_name) == "Times New Roman") {$("#Times_New_Roman").css({'background-color' : "#999"});} else {
				$("#Times_New_Roman").css({'background-color' : "#ccc"}) }
			if ($.cookie($cookie_font_used_name) == "Verdana, Geneva, sans-serif") {$("#Verdana").css({'background-color' : "#999"});} else {
				$("#Verdana").css({'background-color' : "#ccc"}) }
	});
	
	$("#close_font_menu").click(function(){
			$("#font_choices_menu").fadeTo(500,0,function(){
				$("#font_choices_menu").hide();
			});  
			font_menu_open = 0;
	});
	
	//Which one is going to be clicked
	$("#Google_Nobile").click(function(){
		$("body").css({'font-family' : "Nobile"});
		//remove old backgrond color
		remove_menu_items_background_colors();
		//set new cookie
		$.cookie($cookie_font_used_name, "Nobile");
		//add new background color
		$("#Google_Nobile").css({'background-color' : "#999"});
	});
	$("#Helvetica").click(function(){
		$("body").css({'font-family' : "Arial, Helvetica, sans-serif"});
		remove_menu_items_background_colors();
		$.cookie($cookie_font_used_name, "Arial, Helvetica, sans-serif");
		$("#Helvetica").css({'background-color' : "#999"});
	});
	$("#Tahoma").click(function(){
		$("body").css({'font-family' : "Tahoma, Geneva, sans-serif"});
		remove_menu_items_background_colors();
		$.cookie($cookie_font_used_name, "Tahoma, Geneva, sans-serif");
		$("#Tahoma").css({'background-color' : "#999"});
	});
	$("#Times_New_Roman").click(function(){
		$("body").css({'font-family' : "Times New Roman"});
		remove_menu_items_background_colors();
		$.cookie($cookie_font_used_name, "Times New Roman");
		$("#Times_New_Roman").css({'background-color' : "#999"});
	});
	$("#Verdana").click(function(){
		$("body").css({'font-family' : "Verdana, Geneva, sans-serif"});
		remove_menu_items_background_colors();
		$.cookie($cookie_font_used_name, "Verdana, Geneva, sans-serif");
		$("#Verdana").css({'background-color' : "#999"});
	});
	
	function remove_menu_items_background_colors() {
		$("#Google_Nobile").css({'background-color' : "#ccc"});
		$("#Helvetica").css({'background-color' : "#ccc"});
		$("#Tahoma").css({'background-color' : "#ccc"});
		$("#Times_New_Roman").css({'background-color' : "#ccc"});
		$("#Verdana").css({'background-color' : "#ccc"});

	}
	
 	//*****************************************************************************************//	
	// USER FONT SIZE (INCREASE/DESCREASE) ONLY ON <p> - Saves INFO IN COOKIE
	//*****************************************************************************************//
    
	// INCREASE, DRECREASE FONT SIZE	
	var increaseclicks = 0;
	var decreaseclicks = 0;
	// This has px at the end of it
	var originalFontSize = $('p').css('font-size');

	// SET A COOKIE TO REMEMBER ACROSS ALL PAGES
	var $cookie_name = "textsize";
	if($.cookie($cookie_name)){
		var $getSize = $.cookie($cookie_name);
		$("p").css({fontSize : $getSize + "px"});
	} else {
		$.cookie($cookie_name, originalFontSize);
	}
			            
	// RESET Font-Size.
	$("#resetFont").click(function(){
		$('p').css('font-size', originalFontSize);
		$.cookie($cookie_name, originalFontSize);
		increaseclicks = 0;
		decreaseclicks = 0;
	});

	// INCREASE Font Size
	$("#increaseFont").click(function(){
		var currentFontSize = $('p').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		// use styles for tags. {IE6 fix} (because tag font size do not change on IE6)
		var newFontSize = currentFontSizeNum + 1;
		
		if (increaseclicks < 3) {
			$('p').css({'font-size': newFontSize});
			$.cookie($cookie_name, newFontSize);
			increaseclicks++;
			decreaseclicks--;
		}
		return true;
	});

	// DECREASE Font Size
	$("#decreaseFont").click(function(){

		var currentFontSize = $('p').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum - 1;

		if (decreaseclicks <3) {
			// $('html').css('font-size', newFontSize); braces is an IE6 fix.
			$('p').css({'font-size': newFontSize});
			$.cookie($cookie_name, newFontSize);
			decreaseclicks++;
			increaseclicks--;
		}
		return false;
	});
	
	//*****************************************************************************************//	
	// SCROLL IN A DIV (e.g. slowly scroll in more text)
	//*****************************************************************************************//
	
	$('.moretext').css("display","none");
  	$('.moretext').hide();

	$(".scrolldiv_button").click(function(){
		$('.moretext').slideToggle(250);
		$(this).text($(this).text() == 'More' ? 'Less' : 'More')
	});
	
	
	//*****************************************************************************************//	
	// AWSTATS
	//*****************************************************************************************//
	 $(".awstats_login").click(function(event){
		 // window.open("/aw-stats");
		 window.location.replace("/aw-stats");
	 });

});
	
//*****************************************************************************************//	
// COOKIE PLUGIN
//*****************************************************************************************//
	
jQuery.cookie = function (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

