$.trackPage('UA-21837154-1',{ callback:function() { } });

var currentFocus = null; var AllowsPlaceholders = $.browser.webkit;

$.fn.serializeFields = function() { var vars = '';
	this.find(":input").each(function() { 
		if($(this).attr("name") == undefined || $(this).attr("name") == '') { return true; }
		var type = this.type, tag = this.tagName.toLowerCase();
		
		//if($(this).attr("isrequired") == undefined) { $(this).attr("isrequired",false); }
		//vars += '&required[' + $(this).attr("name") + ']=' + $(this).attr("isrequired");
		
		if(type == "select-multiple" && $(this).attr("name").substring($(this).attr("name").length-2) == '[]') { var name = $(this).attr("name"); $(this).find(':selected').each(function(i,selected) { vars += '&' + name + '=' + encodeURIComponent(trim($(selected).val())); }); return true; }
		if(type == "radio") { if($(this).attr("checked") == true) { vars += '&' + $(this).attr("name") + '=' + encodeURIComponent(trim($(this).val())); } return true; }
		if(type == "checkbox") { vars += '&' + $(this).attr("name") + '='; vars += ($(this).attr("checked") == true) ? 1 : 0; return true; }
		if(type != "button") { vars += '&' + $(this).attr("name") + '=' + encodeURIComponent(trim($(this).val())); return true; }
	});
	
	return vars;
};

$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase(); //alert(type + ': ' + tag);
		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 = 0; }
  });
};

$.fn.clearError = function() { 
	$(".ErrorSummary").slideUp("fast");
	$(".ErrorButton").removeClass("ErrorButton");
	return $(this).each(function() { 
		if(this.tagName.toLowerCase() == 'form') { return $('*',this).clearError(); }
		$(this).removeClass('warning error');
	}); 
};

$.fn.clearPlaceholders = function() { 
	if(!AllowsPlaceholders) {
		return $(this).each(function() { 
			if(this.tagName.toLowerCase() == 'form') { return $(':input',this).clearPlaceholders(); } //alert(this.type);
			if(this.type != 'password' && $(this).attr("placeholder") != undefined && $(this).val() == $(this).attr("placeholder")) { $(this).val(""); } 
		}); 
	}
};

$.fn.resetPlaceholders = function() { 
	if(!AllowsPlaceholders) {
		return $(this).each(function() { 
			if(this.tagName.toLowerCase() == 'form') { return $(':input',this).resetPlaceholders(); }
			if(this.type != 'password' && $(this).attr("placeholder") != undefined && $(this).val() == "") { $(this).val($(this).attr("placeholder")); $(this).addClass("placeholder"); } 
		}); 
	}
};





$(document).ready(function() {
	/*$("#frontpage-scroller").scrollable({circular: true, mousewheel: false, keyboard: false }).navigator({
		navi: "#frontpage-scroller-tabs",
		naviItem: 'a',
		activeClass: 'active',
		history: false
	}).autoscroll({ interval: 7777 });*/
	
	//$("ul.sf-menu").supersubs({ minWidth: 13.5, maxWidth: 25, extraWidth: 1 }).superfish({ animation: { opacity:'show', height:'show' }, autoArrows: false, speed: 'fast', delay: 500 });
	$("ul.sf-menu").superfish({ animation: { opacity:'show', height:'show' }, autoArrows: false, speed: 'fast', delay: 500 });

    
    InitializeForm();
    InitializeLink("","EmailLink");
	InitializeLink();
    InitializePlaceHolders();
 
	$('#sidebar a').track({ category:'sidebar' });
	//$('#sidebar a').track({ category:'navigation' });
	$('#content a').track();
	
	if($.browser.msie && $.browser.version.substr(0,1)=="6") {
		$('#sidebar').supersleight({shim: '/blank.gif',apply_positioning:false});
		$('#content').supersleight({shim: '/blank.gif',apply_positioning:false});
	}
	
	/*
	Shadowbox.setup("a.movie", {
        gallery:            "My Movies",
        autoplayMovies:     true
    });
    */
    
    Shadowbox.init({ continuous:true,handleOversize:"drag" },SetupOverlay);
});

function SetupOverlay() {
	/*Shadowbox.setup('#navigation a.overlay', { handleOversize:"drag" });*/
}



var loadingtimer;

function filterByProperty(arr, prop, value) { return $.grep(arr,function (item) { return item[prop] == value }); }
function populateSelect(el,items) { el.options.length = 0; $.each(items,function () { el.options[el.options.length] = new Option(this.name, this.value); }); }

function ProcessResponse(data) {
	var closetimer = 1000;
	
	if(data != null) { //console.log(data);
		for(var i=0;i<data.length;i++) {
			switch(data[i]['type']) {
				case "alert": alert(data[i]['content']); break;
				case "attribute-change": $("#" + data[i]['id']).attr(data[i]['attr'],data[i]['content']); break;
				case "attribute-remove": $("#" + data[i]['id']).removeAttr(data[i]['attr']); break;
				case "checked": $("#" + data[i]['id']).attr('checked',data[i]['content']); break;
				case "class-add": $("#" + data[i]['id']).addClass(data[i]['content']); break;
				case "class-remove": $("#" + data[i]['id']).removeClass(data[i]['content']); break;
				case "error": $("#" + data[i]['id']).addClass('error'); break;
				case "fade-in": $("#" + data[i]['id']).fadeIn(data[i]['content']); break;
				case "fade-out": $("#" + data[i]['id']).fadeOut(data[i]['content']); break;
				case "focus": FocusElement(data[i]['id']); break;
				case "form": InitializeForm(data[i]['id']); break;
				case "function": if(is_function(data[i]['id'])) { eval(data[i]['id'] + "(" + data[i]['content'] + ");"); } break;
				case "hide": $("#" + data[i]['id']).hide(data[i]['content']); break;
				case "html": $("#" + data[i]['id']).html(data[i]['content']); break;
				case "link": InitializeLink(data[i]['id']); break;
				case "email": case "redirect": RedirectPage(data[i]['content']); break;
				case "select-option-add": $("#" + data[i]['id']).append("<option value='" + data[i]['value'] + "'>" + data[i]['content'] + "</option>"); $("#" + data[i]['id']).val(data[i]['value']); break;
				case "select-option-change": $("#" + data[i]['id']).val(data[i]['value']); break;
				case "select-option-update": $("#" + data[i]['id'] + " option:selected").text(data[i]['content']); break;
				case "select-option-delete": $("#" + data[i]['id'] + " option[value=" + data[i]['content'] + "]").remove(); break;
				case "show": $("#" + data[i]['id']).show(data[i]['content']); break;
				case "slide-down": var f = data[i]['focus']; $("#" + data[i]['id']).slideDown(data[i]['content'],function() { if(f != undefined) { FocusElement(f); } }); break;
				case "slide-up": $("#" + data[i]['id']).slideUp(data[i]['content']); break;
				case "timer": closetimer = data[i]['content']; break;
				case "value": $("#" + data[i]['id']).val(data[i]['content']); break;
				case "warning": $("#" + data[i]['id']).addClass('warning'); break;
			}
		}
	}
	
	loadingtimer = setTimeout(function(){ $("#loading").overlay().close(); },closetimer);
}

function InitializeForm(f) { if(f == undefined) { f = ""; } else { f = "#" + f; }
	$("form" + f).submit(function(){
		if($(this).attr('id') == 'AddToCart') { return false; } 
		
		var form = this; var action = $(this).attr('id'); if($(this).attr('id').substring(0,11) == 'SendContact') { action = 'SendContact'; }
		$('input[type=submit]',form).attr('disabled','disabled').addClass("disabled");
		
		LoadMessage(action); $(this).clearError(); $(this).clearPlaceholders(); var vars = $(this).serializeFields(); $(this).resetPlaceholders();
		vars += '&action=' + action + '&page=' + $("#content").attr('class'); // alert(vars);
		var a = "/ajax.php"; //if($(this).hasClass("secure")) { a = $("#securepath").val() + "/ajax.php"; } 
		if($("#issecure").val() == 1) { $.get(a,vars,function(data) { ProcessResponse(data); $('input[type=submit]',form).removeAttr('disabled').removeClass("disabled"); },"json"); } else { $.post(a,vars,function(data) { ProcessResponse(data); $('input[type=submit]',form).removeAttr('disabled').removeClass("disabled"); },"json"); }
		return false;   
	});
}

function InitializeLink(f,c) { if(f == undefined) { f = ""; } else { f = "#" + f; } if(c == undefined || c == "") { c = "post"; }
	$("a" + f + "." + c).click(function(){
		LoadMessage($(this).attr('id'));
		var vars = ''; if($(this).attr('vars') != undefined) { vars += $(this).attr('vars'); }
		vars += '&action=' + (($(this).attr('action') != undefined) ? $(this).attr('action') : $(this).attr('id')) + '&page=' + $("#content").attr('class');
		if($("#issecure").val() == 1) { $.get("/ajax.php",vars,function(data) { ProcessResponse(data); },"json"); } else { $.post("/ajax.php",vars,function(data) { ProcessResponse(data); },"json"); }
		return false;   
	});
	
	$("a" + f + ".overlay").mouseover(function(){ $(this).attr('href',window.location.pathname); });
}

function InitializePlaceHolders(f) { if(f == undefined) { f = ""; } else { f = "#" + f + " "; }
	if(!AllowsPlaceholders) {
		$(f + ':input[type=text]').val(function() {
			if($(this).attr("placeholder") != undefined && $(this).attr("placeholder") != "" && ($(this).val() == "" || $(this).val() == $(this).attr("placeholder"))) { $(this).addClass("placeholder"); return $(this).attr("placeholder"); }
			return $(this).val();
		});
		
		$(f + ':input[type=text]').focus(function() { 
			currentFocus = this;
			if($(this).attr("placeholder") != undefined && $(this).attr("placeholder") != "") { if($(this).attr("placeholder") == $(this).val()) { $(this).val(""); } $(this).removeClass("placeholder"); }
		
		}).blur(function() { 
			currentFocus = null; 
			if($(this).attr("placeholder") != undefined && $(this).attr("placeholder") != "") { if($(this).val() == "" || $(this).attr("placeholder") == $(this).val()) { $(this).addClass("placeholder"); $(this).val($(this).attr("placeholder")); } }
		});
	}
	
	$(f + ':input[type=textarea]').val(function() {
		if($(this).attr("placeholder") != undefined && $(this).attr("placeholder") != "") {
			if($(this).val() == "" || $(this).val() == $(this).attr("placeholder")) { $(this).addClass("textarea_blur"); }
			if(!AllowsPlaceholders) { $(this).addClass("placeholder"); return $(this).attr("placeholder"); }
		}
		
		return $(this).val();
	});
		
	$(f + ':input[type=textarea]').focus(function() { 
		currentFocus = this;
		if($(this).attr("placeholder") != undefined && $(this).attr("placeholder") != "") { 
			$(this).removeClass("textarea_blur");
			if(!AllowsPlaceholders) { if($(this).attr("placeholder") == $(this).val()) { $(this).val(""); } $(this).removeClass("placeholder"); }
		}
		
	}).blur(function() { 
		currentFocus = null; 
		if($(this).attr("placeholder") != undefined && $(this).attr("placeholder") != "") { 
			if($(this).val() == "" || $(this).attr("placeholder") == $(this).val()) { $(this).addClass("textarea_blur"); if(!AllowsPlaceholders) { $(this).addClass("placeholder"); $(this).val($(this).attr("placeholder")); } } 
		}
	});
}

function LoadMessage(id) {
	clearTimeout(loadingtimer);

	$("#loading").overlay({ 
		closeOnClick:false, closeOnEsc:false, top:0,
		onClose:function(){ $("#loading .loading").html(""); },
		onBeforeLoad:ChooseMessage(id)
	});
	
	$("#loading").overlay().load();
}

function ChooseMessage(id) {
	if(id.length > 0) {
		switch(id.substring(0,4)) {
			case "Save": case "Send": case "Dele": $("#loading .loading").html("Please wait while we update our database."); break;
			case "Load": $("#loading .loading").html("loading..."); break;	
		}
	}
}

function clear_form(ele) {
	$(ele).find(':input').each(function() {
		switch(this.type) {
			case 'password': case 'text': case 'textarea': $(this).val(''); break;
			case 'select-multiple': case 'select-one': this.selectedIndex = -1; break;
			case 'checkbox': case 'radio': this.checked = false;
		}
	});
}

function SubmitSuccess(f,m,h) { 
	if(f != undefined && f != "") { if(h == undefined || h == "") { h = 250; }
		$('#' + f + 'Container').animate({ opacity:0,height:h +'px' },500,function() { 
			$('#' + f).hide(); 
			if(m != undefined && m != "") { $('#' + f + 'SuccessContainer').html(m); } 
			$('#' + f + 'SuccessContainer').show(); 
			$('#' + f + 'Container').animate({ opacity:1 },500); 
		}); 
	} 
}

function FocusElement(f) { if(f!="") { $("#" + f).focus(); if($("#" + f).attr('type') == 'text' || $("#" + f).attr('type') == 'textarea') { $("#" + f).select(); } } }
function RedirectPage(l) { location.replace(l); }
function is_function(f) { if(typeof window[f] != 'function') { return false; } return true; }

function ltrim(str,charlist) {
    charlist = !charlist ? ' \\s\u00A0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    var re = new RegExp('^[' + charlist + ']+', 'g');
    return (str+'').replace(re,'');
}

function rtrim(str,charlist) {
    charlist = !charlist ? ' \\s\u00A0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\\$1');
    var re = new RegExp('[' + charlist + ']+$', 'g');
    return (str+'').replace(re,'');
}

function trim(str,charlist) { return rtrim(ltrim(str,charlist),charlist); }

function str_replace(search, replace, subject, count) {
	var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,f = [].concat(search),r = [].concat(replace),s = subject,ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) { this.window[count] = 0; }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') { continue; }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) { this.window[count] += (temp.length-s[i].length)/f[j].length; }
        }
    }
    
    return sa ? s : s[0];
}
