jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
		jQuery("<img>").attr("src", arguments[i]);
	}
}
$.preloadImages("/images/menu_promotionso.gif", "/images/menu_ordero.gif", "/images/menu_deliveryo.gif", "/images/menu_warrantyo.gif", "/images/menu_paymento.gif", "/images/carto.jpg", "/images/carto_full.jpg", "/images/search_btno.gif", "/images/search_submito.gif", "/images/login_submito.gif", "/images/register_submito.gif", "/images/refresh_submito.gif", "/images/invoice_newdatao.gif", "/images/order_completeo.gif", "/images/save_changeso.gif", "/images/make_maino.gif", "/images/erase_btno.gif", "/images/secmenu_hint_top.png", "/images/secmenu_hint_middle.gif", "/images/secmenu_hint_bottom.png");

function addBookmark(){
	bookmarkurl = "http://www.stroi.bg/";
	bookmarktitle="Stroi.bg";
	if (document.all)
		window.external.AddFavorite(bookmarkurl,bookmarktitle)
	else if ( window.sidebar ) {
		window.sidebar.addPanel(bookmarktitle, bookmarkurl,"");
	}
}

function hintHide(id) {
	$("#"+id).parent().find(".hintSection").hide();
}

function rebindEventsProdSpec() {
	$(".oneProductDescription #frame select").change(function() {
		$.post(
			"/specs-ajax.php",
			{'ref': $("#product_ref").text(), 'name': $(this).attr("name"), 'value': $(this).attr("value")},
			function (data, textStatus) {
				if ("success" === textStatus) {
					$("#frame").html(data);
					rebindEventsProdSpec();
				/*
				if (1 === data.status) {
					console.log(data);
					$.each(data.product, function (key, value) {
						if ("art_id" === key) {
							$("#art_id").attr("value", value);
						}
						else {
							$("." + key).text(value);
						}
					});
					$.each(data.specs, function (spec_id, spec_values) {
						if ($("#s" + spec_id).is("*") && spec_values.values.length > 0) {
							var target = $("#s" + spec_id);
							target.empty();
							
	                        var options = [], i = 1, o = null;
							
                            o = document.createElement("OPTION");
                            o.value = "-1";
                            o.text = "Всички";
                            target.get(0).options[0] = o;
	                        
	                        for (i = 1; i < spec_values.values.length; i++) {
	                            // required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
	                            console.log(spec_values.values[i]);
	                            o = document.createElement("OPTION");
	                            o.value = o.text = spec_values.values[i];
	                            target.options[i] = o;
	                        }
                            if (spec_values[i].selected) {
                            	alert(spec_values[i].selected);
                            	o.selected = "selected";
                            }
						}
					});
				}*/
				}
			},
			//"json"
			"html"
		);
	});
}

$(function() {
    jQuery('#mycarousel').jcarousel({
        scroll: 1
    });

    $('input:text').hint();

    var offSet = 8;
    var hintSection = $(".hintSection");
	hintSection.hide();
	var hintShower = $(".hintShower");
	var hintShower1 = $(".textLink");

	hintShower.hover(function(){
		var hint = $(this).parent().find(".hintSection");
		if (hint.is(":visible")){
			clearTimeout(t);
		}
		var hintShowerXPos = $(this).position().left;
		var hintShowerXWidth = $(this).width();
		hint.css("left", (hintShowerXPos + hintShowerXWidth + offSet) + "px");
		hint.show();
	},
	function(){
		var hint = $(this).parent().find(".hintSection");
		var current_id = $(this).attr("id");
		t = setTimeout("hintHide('"+current_id+"')", 200);
	});

	hintShower1.hover(function(){
		var hint = $(this).parent().find(".hintSection");
		if (hint.is(":visible")){
			clearTimeout(t);
		}
		var hintShower = $(this).parent().find(".hintShower");
		var hintShowerXPos = hintShower.position().left;
		var hintShowerXWidth = hintShower.width();
		hint.css("left", (hintShowerXPos + hintShowerXWidth + offSet) + "px");
		hint.show();
	},
	function(){
		var hint = $(this).parent().find(".hintSection");
		var current_id = $(this).attr("id");
		t = setTimeout("hintHide('"+current_id+"')", 200);
	});

	hintSection.hover(function(){
		clearTimeout(t);
	},
	function(){
		$(this).hide();
	});
	$("#company").click(function() {
	  $("#name_field").html("Име на фирмата: <span>*</span>");
	  $("#in_field").html("ИН: <span>*</span>");
	  $(".error").each(function() {
	    $(this).remove();
	  });
	  $(".companyFields").slideDown("fast");
	});
	$("#person").click(function() {
	  $("#name_field").html("Име: <span>*</span>");
	  $("#in_field").html("ЕГН: <span>*</span>");
	  $(".error").each(function() {
	    $(this).remove();
	  });
	  $(".companyFields").slideUp("fast");
	});
	
	var currHeight = 0;
	var maxHeight = 0;
	var currTitleHeight = 0;
	var maxTitleHeight = 0;
	var oneRowLevel = $(".oneRowLevel");
	oneRowLevel.each(function() {
		var titleLeveler = $(this).find(".titleLinkContainerLeveler");
		//console.log(titleLeveler);
		titleLeveler.each(function() {
			currTitleHeight = $(this).height();
			if (currTitleHeight > maxTitleHeight) {
				maxTitleHeight = currTitleHeight;
			}
		});
		titleLeveler.height(maxTitleHeight);
		//console.log(maxTitleHeight);
		currTitleHeight = 0;
		maxTitleHeight = 0;
		
		var levelContainer = $(this).find(".levelContainer");
		//console.log(levelContainer);
		levelContainer.each(function() {
			currHeight = $(this).height();
			if (currHeight > maxHeight) {
				maxHeight = currHeight;
			}
		});
		levelContainer.height(maxHeight);
		//console.log(maxHeight);
		currHeight = 0;
		maxHeight = 0;
	});
	
	if ('undefined' !== typeof(hs)) {
		hs.graphicsDir = '/js/highslide/graphics/';
		hs.align = 'center';
		hs.transitions = ['expand', 'crossfade'];
		hs.outlineType = 'rounded-white';
		hs.fadeInOut = true;
		//hs.dimmingOpacity = 0.75;
		
		// Add the controlbar
		if (hs.addSlideshow) hs.addSlideshow({
			//slideshowGroup: 'group1',
			interval: 3000,
			repeat: false,
			useControls: true,
			fixedControls: false,
			overlayOptions: {
				opacity: 1,
				position: 'top right',
				hideOnMouseOut: false
			}
		});
	}

	
	rebindEventsProdSpec();
	
});
