 // JQUERY - CUSTOM.JS

$(document).ready(function() {		

	//*****************************************************************************************//	
	// HIDE CLASS SINCE WE HAVE JAVA ENABLED
	//*****************************************************************************************//

	// 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 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 = $('body').css('font-size');

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

	// INCREASE Font Size
	$("#increaseFont").click(function(){
		var currentFontSize = $('body').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 < 6) {
			$('body').css({'font-size': newFontSize});
			$.cookie($cookie_name, newFontSize);
			increaseclicks++;
			decreaseclicks--;
		}
		return true;
	});

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

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

		if (decreaseclicks <6) {
			// $('html').css('font-size', newFontSize); braces is an IE6 fix.
			$('body').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 plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

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

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

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(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;
};


