$(document).ready(function() {
	var $cookie_name = "textsize";
	var originalFontSize = $("html").css("font-size");
	
	if ($.cookie($cookie_name)) {
		var $getSize = $.cookie($cookie_name);
		$("html").css({fontSize: $getSize + ($getSize.indexOf("px") != -1 ? "" : "px")});
	} else {
		$.cookie($cookie_name, originalFontSize);
	}

	$('.resetFont').bind("click", function(){
		$("html").css("font-size", originalFontSize);
		$.cookie($cookie_name, originalFontSize);
	});

	$('.increaseFont').bind('click', function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum * 1.2;
		if ( newFontSize < 25 ) {
			$('html').css('font-size', newFontSize);
			$.cookie($cookie_name, newFontSize);
		}
		return false;
	});

	$('.decreaseFont').bind('click', function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum * 0.8;
		if ( newFontSize > 13 ) {
			$('html').css('font-size', newFontSize);
			$.cookie($cookie_name, newFontSize);
		}
		return false;
	});
	
	$.fn.pause = function(duration, func) {
		$(this).animate({ dummy: 1 }, duration, 'swing', func);
		return this;
	};
	
	$.extend({
		isset: function($var) {
			return !($var === undefined) && $var != null;
		}
	});
	
	// VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function(){
        return this.each(function(i){
            var ah = $(this).height();
            var ph = $(this).parent().height();
            var mh = (ph - ah) / 2;
            $(this).css('margin-top', mh);
        });
    };
    
	$(".info").vAlign();
	
    $.fn.topLink = function(settings){
        settings = $.extend({
            min: 1,
            fadeSpeed: 200,
            ieOffset: 50
        }, settings);
        return this.each(function(){
            //listen for scroll  
            var el = $(this);
            el.hide(); //in case the user forgot  
            $(window).scroll(function(){
                //stupid IE hack  
                if (!$.support.hrefNormalized) {
                    el.css({
                        'position': 'absolute',
                        'top': $(window).scrollTop() + $(window).height() - settings.ieOffset
                    });
                }
                if ($(window).scrollTop() >= settings.min) {
                    el.fadeIn(settings.fadeSpeed);
                }
                else {
                    el.fadeOut(settings.fadeSpeed);
                }
            });
        });
    };

	$('.current_page_parent').addClass('current_page_item').removeClass('current_page_parent');
	$('.current_page_ancestor').addClass('current_page_item').removeClass('current_page_ancestor');
	$('.current_page_item > ul > li').addClass('current_page_parent');

	$("ul.sf-menu").superfish({
		pathClass:  'current_page_item',
		delay: 2000
	});

	//set the link  
	$('#top-link').topLink({
		min: 400,
		fadeSpeed: 500
	});
	
	//smoothscroll  
	$('#top-link').click(function(e){
		e.preventDefault();
		$.scrollTo(0, 300);
	});
	
	if($('.menu').is(':visible')){
		$('.menu ul').hide();
		$('.menu li.current_page_item > ul').show();
		
		$('.menu li a').click(function() {
			var checkElement = $(this).next();
			
			if(checkElement.is('ul') && (checkElement.is(':visible'))) {
				return true;
			}
			
			if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				if ($(this).parent().siblings(':visible').find('ul')) {
					$(this).parent().siblings(':visible').find('ul').slideUp('normal');
				}
				checkElement.slideDown('normal');
				return false;
			}
		});
	}
	
	if ($('#tabs').is(':visible')) {
		$("#preloader").hide();

		$("ul.tabs").tabs("div.panes > div");
	
		$tabs = $('#tabs').tabs({ 
			remote: true,
			spinner: 'Please wait...',
			cache: false
		});

		$('.download_button').bind('click', function(){
			$tabs.tabs('select', 'downloads');
			return false;
		});
	}
	
	if ($('#authenticate').is(':visible')) {
		$('#authenticate').tabs();
		if($("#authenticate") && document.location.hash){
			$.scrollTo("#authenticate");
		}
		$("#authenticate ul").localScroll({ 
			target: "#authenticate",
			duration:0,
			hash:true
		});
	}
	
	$.fn.clearForm = function() {
		return this.each(function() {
			var type = this.type, tag = this.tagName.toLowerCase();
			if (tag == 'form')
				return $(':input',this).clearForm();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = -1;
		});
	};
	
	$('.dbx-handle').wrap('<div id="handle-wrap"></div>');
	
	//if($.isset($('.dbx-content > ul').attr("class"))) {
	//	$('.dbx-content > ul').addClass('menu');
	//}
	
	//$('.dbx-content > ul li a[href]').filter("[href^=" + document.URL.substring(0, document.URL.length - 1) + "]").parents(':first').addClass('current_page_item');

	$('#user_nav').after('<div id="u_content_cart"></div>');
	$('#user_nav').after('<div id="u_content_account"></div>');
	$('#u_cart, #u_content_cart').hover(
		function(){
			$('#u_cart a').addClass('active');
			$.get(al_base_uri + '/wp-content/themes/itheme/shopp/ajax-cart.php', { request: "cart" }, function(data){
				//alert(data);
				$('#u_content_cart').html(data);
			});
			$('#u_content_cart').show();
		},
		function () {
			$('#u_cart a').removeClass('active');
			$('#u_content_cart').hide();
		}
	);
	
	$('#u_account, #u_content_account').hover(
		function(){
			$('#u_account a').addClass('active');
			$('#u_content_account').load(al_base_uri + '/wp-content/themes/itheme/shopp/ajax-cart.php');
			$.get(al_base_uri + '/wp-content/themes/itheme/shopp/ajax-cart.php', { request: "account" }, function(data){
				$('#u_content_account').html(data);
			});
			$('#u_content_account').show();
		},
		function () {
			$('#u_account a').removeClass('active');
			$('#u_content_account').hide();
		}
	);
	
	/* start search */
	$("li.page_item:last").css({'border': 'none'});
	$("#s").focus(function(srcc) {
		if ($(this).val() == $(this)[0].title) {
			$(this).removeClass("blurred");
			$(this).val("");
		}
	});
	$("#s").blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("blurred");
			$(this).val($(this)[0].title);
		}
	});
	$("#s").blur();
	$("#searchform .go").click(function(){
		if($("#s").val() == "" || $("#s").val() != $("#s")[0].title) {
			$("#searchform").submit();
		} else {
			$(".text_message").fadeIn("slow", function(){$(this).pause(5000).fadeOut();});
			return false;
		}
	});
	/* end search */
});

/**
 * Gets the specified querystring from the browser url
 * 
 * @param {Object} key
 */
function qs(key, default_) {
	if (default_==null) default_="";
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&]*)");
	//var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if(qs == null)
		return default_;
	else
		return qs[1];
} 

//Specify affected tags. Add or remove from list:
var tgs = new Array( 'div','td','tr');

//Specify spectrum of different font sizes:
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;

function ts( trgt,inc ) {
	if (!document.getElementById) return
	var d = document,cEl = null,sz = startSz,i,j,cTags;
	
	sz += inc;
	if ( sz < 0 ) sz = 0;
	if ( sz > 6 ) sz = 6;
	startSz = sz;
		
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

	cEl.style.fontSize = szs[ sz ];

	for ( i = 0 ; i < tgs.length ; i++ ) {
		cTags = cEl.getElementsByTagName( tgs[ i ] );
		for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
	}
}

function winopen(url) {
	var winname;
	var nwin;
	var nwin2;
	var hname;

	// strip any dots from host name - used to make unique window name
	hname = self.location.host.replace(/\./g,"_");
	winname = hname + "_TexBox1";

	nwin = window.open('', winname, 'height=474,width=754,location=no,scrollbars=no,menubars=no,toolbars=no,resizable=yes,status=no');

	if (nwin.location != url) {
		nwin.location = url;
		nwin.opener = self;
		self.defaultStatus = "Opened Communicator window";
	} else {
		self.defaultStatus = "Communicator window already open";
		nwin.focus();
	}
}