jQuery.noConflict();

jQuery(document).ready(function($){
	
	$.maxZIndex = $.fn.maxZIndex = function(opt) {
		var zmax = 0;
		var def = {inc:10,group:"*"};
		$.extend(def, opt);    
		$(def.group).each(function(){
			var cur = parseInt($(this).css('z-index'));
			zmax = cur > zmax ? cur : zmax;
		});
		if (!this.jquery)return zmax;
		return this.each(function(){
			zmax += def.inc;
			$(this).css("z-index", zmax);
		});
	}
	
	/**/
	
	try{
	var adv_search = $('<form id="adv_search" action="templates/prod_search_temp_init.asp" method="post"><div class="mid"><div class="oneline"><input type="text" name="search-string" id="search-string" value="Advanced Search" title="Advanced Search"></div><p class="left first"><label for="search_SKU">SKU:</label><input type="text" name="search_SKU" id="search_SKU"></p><p class="left"><label for="search_price_from">Price from:</label><input type="text" name="search_price_from" id="search_price_from"></p><p class="left"><label for="search_price_to">Price to:</label><input type="text" name="search_price_to" id="search_price_to"></p><div class="line_select"><p class="w1p"><label for="search_categories">Categories:</label><select name="search_categories" class="cssselect_adv_search" id="categories"></select></p><p class="w2p"><label for="search_brands">Brands:</label><select name="search_brands" class="cssselect_adv_search" id="brands"></select></p></div><div class="scrollbutton right"><div class="scrollbutton_c"><div class="scrollbutton scrollbutton_face bt_search"><div class="scrollbutton_c"><p class="search">Search</p></div><div class="outcor"></div></div></div><div class="outcor"></div></div><div class="scrollbutton right"><div class="scrollbutton_c"><div class="scrollbutton scrollbutton_face bt_cancel"><div class="scrollbutton_c"><p class="cancel">Cancel</p></div><div class="outcor"></div></div></div><div class="outcor"></div></div><p class="searchAssistline"></p></div></form>');
	
	var adv_search_categories = $("#categories",adv_search);
	var adv_search_brands = $("#brands",adv_search);
	
	$(".searchAndSuppBox").append( adv_search );
	
	var posY = adv_search.offset().top;
	
	$('#search-string', adv_search).change(function(){
		$("#search_qu").val( $(this).val() );
	})
	$("#search_qu").change(function(){
		$('#search-string', adv_search).val( $(this).val() );
	});
	
	$('input', adv_search).focus(function(){
		if( $(this).attr('value') == $(this).attr('title') )
		{
			$(this).attr('value','');
		}
	}).blur(function(){
		if( !$(this).attr('value') )
		{
			$(this).attr('value',$(this).attr('title'));
		}
	});
	
	adv_search.offset({"top":"-1000"}).hide();
	
	$(".searchAndSuppBox .i-adv").click(function(){
		
		if( adv_search_categories.html() == "" )
		{
			$.ajax({ type: "POST", async:true, url: "templates/catalog_list.asp", dataType: 'html', data: {}, success: function(html){
					adv_search_categories.html(html);
				$.ajax({ type: "POST", async:true, url: "templates/brand_list.asp", dataType: 'html', data: {}, success: function(html){
					adv_search_brands.html(html);
					adv_search.show();
					$(".cssselect_adv_search", adv_search ).iSelectBox({maxViewLine:6});

					adv_search.offset({"top":posY}).hide().fadeIn();
				} });
			} });
		}
		
		adv_search.fadeIn();
		
	$('#search-string', adv_search).focus();})
	$(".cancel", adv_search).parent().parent().click(function(){adv_search.fadeOut();$("#search_qu").focus();})
	$(".search", adv_search).parent().parent().click(function(){
		adv_search.submit()
	})
	
	}catch(e){}
	
	try{
		var scroll_top = 0;
		var offset_lmenu = 0;
	
		var lmenu_top = parseInt ($('.leftcol').offset().top);
		var leftm_height = parseInt ($("#leftm").height());

		$(window).scroll(function () {

			var rightcol_top = parseInt ($('.rightcol').offset().top);
			var rightcol_height = parseInt ($('.rightcol').height());
			var lmenu_height = parseInt ($('.leftcol').height());
			
			var leftm_top = parseInt ($("#leftm").offset().top);
			
			if( lmenu_height < rightcol_height )
			{
				var vector = (scroll_top>$(document).scrollTop())?0:1;
				scroll_top = $(document).scrollTop();

				if( !vector && ( ( scroll_top - leftm_top ) < 0 ) ) // if Scroll Up
				{
					offset_lmenu = scroll_top - lmenu_top;
				}
				else if( vector && (leftm_top + leftm_height) < scroll_top + $(window).height() ) // if Scroll Down
				{
					if( leftm_height > $(window).height())
					{
						offset_lmenu = scroll_top + $(window).height() - leftm_height - lmenu_top;
					}
					else
					{
						offset_lmenu = scroll_top - lmenu_top;
					}
				}
				
				if(scroll_top <= lmenu_top )
				{offset_lmenu = 0;}
				
				if( rightcol_height + rightcol_top < scroll_top + lmenu_height  )
				{offset_lmenu = rightcol_height - lmenu_height}
				
				$('.leftcol').stop().animate({top:offset_lmenu},{"duration":600},"easein");
			}
		
		});
	}catch(e){}
		
	/**/
	
	$('input.i-input').focus(function(){
		if( $(this).attr('value') == $(this).attr('title') )
		{
			$(this).attr('value','');
		}
	}).blur(function(){
		if( !$(this).attr('value') )
		{
			$(this).attr('value',$(this).attr('title'));
		}
	});
	
	$( 'form.i-emailform' ).submit(function(){
		var e = $( 'input.i-input', $(this) );
		if( ( e.attr('value') == null ) || ( e.attr('value') == "" ) ){
			alert( "Please Enter your Email!" );
			e.focus();
			return false;
		}
		if( echeck( e.attr('value') ) == false ){
			e.value = "";
			e.focus();
			return false;
		}
	});
	
	function echeck(str) {
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
        alert("Invalid E-mail!")
        return false
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        alert("Invalid E-mail!")
        return false
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Invalid E-mail!")
        return false
    }
    if (str.indexOf(at,(lat+1))!=-1){
        alert("Invalid E-mail!")
        return false
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Invalid E-mail!")
        return false
    }
    if (str.indexOf(dot,(lat+2))==-1){
        alert("Invalid E-mail!")
        return false
    }
    if (str.indexOf(" ")!=-1){
        alert("Invalid E-mail!")
        return false
    }
    return true}
	function getOffset( el ) {
    var _x = 0;
    var _y = 0;
    while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
        _x += el.offsetLeft - el.scrollLeft;
        _y += el.offsetTop - el.scrollTop;
        el = el.parentNode;
    }
    return { top: _y, left: _x };}
	
	/* Wish List */
	
	$('#form1 .istrok').click(function(){Validate($('#form1'));return false;});
	
	/* Init JQuery Modules */

	//try{$(".jsonslider").jsonSlider("/templates/slider.asp", "/templates/getcategory.asp",{autoPlay:false});}catch(e){}
	
	/* Cert VAlign Bug fix */
	
	try{$("#footer_main #footer_ribbon img").each(function(){var img = $(this);var imgHeight = img.height();img.css({"margin-top":(84-imgHeight)/2})});}catch(e){}

	try{$(".accessories_box_mini .minipic img").each(function(){
		var img = $(this);
		if( !/\.\w{3,4}/.test(img.attr('src')) )
		{
			img.parent().parent().parent().hide();
		}
	});
	
	}catch(e){}
	
	try{$("#search_top input.i-input").focus(function(){$("#search_top input.i-submit").addClass("focus")}).blur(function(){$("#search_top input.i-submit").removeClass("focus")})}catch(e){}

	Number.prototype.formatMoney = function(c, d, t)
	{
		var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
		return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
	};
	String.prototype.getFloat = function(){return parseFloat(this.replace(",",""))}

	$(".cssselect").change(function(){
		var price = $("#form_price").val().getFloat();
		var cssselect = $(".cssselect option:selected");
		cssselect.each(function(i,v){
			price += ($(v).attr("class").split("|")[1]).getFloat();
		});
		$(".priceline p").text("$"+price.formatMoney(2,".",","));
	});
	
});

function newWin(a, w, h) {window.open(a, '_blank', 'resizable=yes,scrollbars=yes,location=yes,status=yes,width=' + w + ',height=' + h)};
function cert(a, b) { if (a == 'verisign') {newWin('https://seal.verisign.com/splash?form_file=fdf/splash.fdf&dn=' + b + '&lang=en', 750, 550) }if (a == 'hsafe') {newWin('https://www.scanalert.com/RatingVerify?ref=' + b, 750, 550)}};
