jQuery.noConflict();

var BASEURI = jQuery("base").attr("href");

/* menu */

jQuery(function(){
	var animation = true;
	var menuTimeout = null;
	
	jQuery('div.web-sitenav-1 ul.level-1 li').mouseover(function(event){
		animation = false;
		jQuery(this).find('> a.label').addClass('web-linkhover');
		jQuery(this).find('> ul.level-2').stop().animate(
			{"top": "-65px"}, 700, 'easeOutCubic'
		);
	}).mouseout(function(event){
		animation = true;
		jQuery(this).find('> a.label').removeClass('web-linkhover');
		jQuery(this).find('> ul.level-2').stop().animate(
			{"top": "-1000px"}, 1000, 'easeInCubic'
		);
	});
    
    jQuery('div.web-sitenav-1 ul.level-2 li').mouseover(function(event){
		animation = false;
		jQuery(this).find('> a.label').addClass('web-linkhover');
		jQuery(this).find('> ul.level-3').stop().animate(
			{"top": "-65px"}, 700, 'easeOutCubic'
		);
	}).mouseout(function(event){
		animation = true;
		jQuery(this).find('> a.label').removeClass('web-linkhover');
		jQuery(this).find('> ul.level-3').stop().animate(
			{"top": "-1000px"}, 1000, 'easeInCubic'
		);
	});
});

/* lights */
function a_lights(){
	var timeout = null;

	var hw = jQuery(window).height();
	var hb = jQuery(document).height();
	var light = null;
	
	getSize();
	light = jQuery('div.web-bottomlight');
	if(light){
		timeout = setTimeout(resizeBottomLight,1);
	}
	
	function getSize() {
		hw = jQuery(window).height();
		hb = jQuery(document).height();
	}
		
	function resizeBottomLight(){
		var pos = light.position();
		if(pos != null){
			var t = 125;
			if(jQuery.browser.msie){
				t = 121;
				if(jQuery.browser.version <= 7){
					t = 125;
				}
				if(hw > hb)
					t = 119;
			}
			light.height(t);
			pos = light.position();
			hb = jQuery(document).height();
			light.height(hb-(pos.top-t));
		}
	}
		
	jQuery(window).bind("resize", function(){
		getSize();
		if(jQuery.browser.msie){
			//clearTimeout(timeout);
			//light.height(500);
			timeout = setTimeout(resizeBottomLight,100);
		} else {
			resizeBottomLight();
		}
	});
}
jQuery(function(){
	a_lights()
});

/* search */
jQuery(function(){
	var sfield = jQuery("#ss-search");
	var sfield2 = jQuery("#ss-search2");
	var sfieldU = jQuery("#ss-search-url");
	if(( sfield || sfield2 )&& sfieldU && sfieldU.length > 0){
		if(sfield){
			sfield.keypress(function(event){
				var k = event.keyCode;
				if(k!=13) { return true; }
				a_search(sfield.val());
			});
		}
		if(sfield2){
			sfield2.keypress(function(event){
				var k = event.keyCode;
				if(k!=13) { return true; }
				a_search(sfield2.val());
			});
			jQuery("div.web-wheretobuy-btn-mini a").click(function(event){
				a_search(sfield2.val());
				return false;
			});
		}
	}
	
	function escapeTxt(os){
		var ns='';
		var t;
		var chr='';
		var cc='';
		var tn='';
		for(i=0;i<256;i++){
			tn=i.toString(16);
			if(tn.length<2) {
				tn="0"+tn;
			}
			cc+=tn;
			chr+=unescape('%'+tn);	
		}
		cc=cc.toUpperCase();
		os.replace(String.fromCharCode(13)+'',"%13");
		for(q=0;q<os.length;q++){
			t=os.substr(q,1);
			for(i=0;i<chr.length;i++){
				if(t==chr.substr(i,1)){
					t=t.replace(chr.substr(i,1),"%"+cc.substr(i*2,2));
					i=chr.length;
				}
			}
			ns+=t;
		}
		return ns;
	}

	function a_search(val){
		var vak = val;
		if(vak.length <= 3) { return false; }
		var url = sfieldU.val();
		window.location.href = BASEURI + (url+"/"+vak);
		return false;
	}

});


jQuery(function(){
	var sfield = jQuery("#wtb-search");
	var sfieldC = jQuery("#wtb-search-country");
	var sfieldU = jQuery("#wtb-search-url");
	var geocoder = null;
	if(sfield && sfield.length > 0 && sfieldC && sfieldC.length > 0 && sfieldU && sfieldU.length > 0){
		geocoder = new google.maps.Geocoder();
		sfield.keypress(function(event){
			var k = event.keyCode;
			if(k!=13) { return true; }
			a_searchPlace();
		});
		
		jQuery(".web-wheretobuy-btn-mini").click(function(event){
			return a_searchPlace();
		});
	}
	
	function a_searchPlace(){
		if(geocoder != null) {
			var vak = sfield.val();
			if(vak.length <= 3) { return false; }
			var ciso = sfieldC.val();
			var url = sfieldU.val();
			geocoder.geocode( { 'address': vak+" "+ciso}, function(results, status) {
		      if (status == google.maps.GeocoderStatus.OK) {
		      	var lat = results[0].geometry.location.lat();
		      	var lon = results[0].geometry.location.lng();
		        window.location.href = BASEURI + (url+"/"+lat+";"+lon)
		      }
		    });
		}
		return false;
	}
});


jQuery(function(){
	jQuery(".box-close").click(function(event){
		var menuid = jQuery(this).parent().attr("menuid");
		if(menuid != null){
			var menu = jQuery("li[menuid='menu-"+menuid+"']");
			if(menu != null){
				jQuery(menu).removeClass("web-menu-box-opened");
			}
		}
		if(jQuery.browser.msie && jQuery.browser.version > 7){
			jQuery(this).parent().hide();
			a_lights();
		} else {
			jQuery(this).parent().fadeOut(function(){
				a_lights();	
			});
		}
	});
	
	jQuery(".box-close-compare").click(function(event){
		var partnumber = jQuery(this).parent().removeClass("web-compare-full").find(".web-comp-partnumber").html();
		var cookComp = jQuery.cookie("a-compare");
		if(cookComp != null){
			var prods = jQuery.evalJSON(cookComp);
			if(prods!= null){
				var eles = jQuery.evalJSON(prods.eles);
				var pg = prods.pg;
				for (var i=0;i<eles.length;i++){
					if(eles[i].id == partnumber){
						eles[i] = null;
						eles.splice(i,1);
						prods = {'pg': pg, 'eles': eles};
						jQuery.cookie("a-compare", jQuery.toJSON(prods), { path: '/' });
					}
				}
				a_preloadCompareBox();
			}
		}		
		return true;
	});
	
	jQuery(".web-menu-openbox").click(function(event){
		var boxid = jQuery(this).attr("boxid");
		var box = jQuery("div[menuid='"+boxid+"']");
		if(box != null){
			if(jQuery(this).hasClass("web-menu-box-opened")){
				jQuery(this).removeClass("web-menu-box-opened");
				jQuery(box).fadeOut(function(){
					a_lights();
				});
			} else {
				jQuery(this).addClass("web-menu-box-opened");
				jQuery(box).show();
				a_lights();
			}
		}
	});
	
});

function a_preloadCompareBox(openit){
	var cookieName = "a-compare";
	var cookComp = jQuery.cookie(cookieName);
	
	if(cookComp != null){
		var prods = jQuery.evalJSON(cookComp);
		if(prods!= null){
			var eles = jQuery.evalJSON(prods.eles);
			for (var i=0;i<3;i++){
				a_setProduct(eles[i],i);
			}
			if(eles.length >= 2) {
				jQuery(".web-compare-btn").show();
			} else {
				jQuery(".web-compare-btn").hide();	
			}
			var lab = jQuery(".web-info-tools li[boxid='compare']");
			if(lab != null){
				if(eles.length <= 0){
					lab.find(".web-counter").html("");
				} else {
					lab.find(".web-counter").html(" ("+eles.length+")");
				}
			}
		}
	} else {
		var prods = {'pg':'','eles':[]};
		jQuery.cookie(cookieName, jQuery.toJSON(prods), { path: '/' });
	}
	
	if(openit){
		var box = jQuery.find(".web-product-comparebox");
		var menuid = jQuery(box).attr("menuid");
		if(menuid != null){
			var menu = jQuery("li[menuid='menu-"+menuid+"']");
			if(menu != null){
				jQuery(menu).addClass("web-menu-box-opened");
			}
		}
		jQuery(box).show();
		a_lights();	
	}
	
	function a_setProduct(obj, i){
		var box = jQuery(".web-compare-single-product[pos='"+i+"']");
		if(box != null){
			if(obj != null){
				box.addClass("web-compare-full");
				box.find(".web-comp-name").html(obj.name);
				box.find(".web-comp-descr").html(obj.descr);
				box.find(".web-comp-partnumber").html(obj.id);
				box.find(".web-comp-img").attr("src",obj.image);
			} else {
				box.removeClass("web-compare-full");
				box.find(".web-comp-name").html("");
				box.find(".web-comp-descr").html("");
				box.find(".web-comp-partnumber").html("");
				box.find(".web-comp-img").attr("src","http://static.acer.com/up/Static/shim.gif");
			}
		}
	}
}
