/*
 * main - default Javascript document
 *
 * FG Forrest, a. s. (fg.cz)
 *
 * $Date: 2010/04/22 08:28:54 $
 * $Rev: 1 $
 */

eval("try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}");

/* Test na zapnuté CSS */
function hasCSS() {
	var _d = document.createElement('div')
	_d.id = 'css_test'
	$('body').append(_d)
	var _v = ($('#css_test').width() != 1) ? false : true
	$('#css_test').remove()
	return _v
}

var colorItems = [];
var surfaceItems = [];
var textLocDictionary = '';
var YWASearchInit = false;

$(document).ready(function() {

if(hasCSS()) {
						   
	/* start focusInput Type Text	*/
	$('.focinput').fitt();
	
	/* 3D postup stavby - sendvič */
	$("#procedure-3d").stepbystep();
	
	/* ToolTip init */
	$(".tooltip").tooltip(-25,-95,'tooltip',false,false);
	$(".tooltipcolor").tooltip(17,-147,'tooltipcolor',true,false);
	$(".tooltipperson").tooltip(27,-147,'tooltipperson',true,false);
	$("acronym").tooltip(15,-70,'tooltipglossary',true,true);
	$(".tooltipskin").tooltip(20,-168,'tooltipskin',false,false);
	$(".tooltip-remove, .delete-from-basket").tooltip(16,-18,'tooltip-remove',false,false);
	
	/*  */
	/*$(".basket-content .ks, .basket-content .palet").orderBox("#order-basket");*/
	$(".p-detail .price .order").orderBox("#order-basket", "dynamic");
	$(".product-list .p-item .price .order").orderBox("#order-basket", "dynamic");
	$("#p-mid .order .buttons .rename-template ").orderBox("#rename-template", "static");
	$("#p-mid #my-account .new-order").orderBox("#box-new-order", "static");
	$("#p-mid .order .buttons .new-adress").orderBox("#new-delivery-adress", "static");
	$("#template-detail-new-order").orderBox("#template-detail-new-order-box", "static");
	
	/* highslide from product */
	$(".highslide-thumb-color").highslideFromArray("hs-big-thumb-color","img-gallery no-resize big-thumb", "Barva", colorItems, true);
	$(".p-detail .color-gallerie").highslideFromArray("hs-big-thumb-color","img-gallery no-resize big-thumb", "Barva", colorItems, false);
	$(".p-detail .surface-gallerie").highslideFromArray("hs-big-thumb-surface","img-gallery no-resize big-thumb", "Povrch", surfaceItems, false);
	
/* Scheme animate show/hiden */
	$('.scheme-thumb-box').hover(
			function () {
			$('#p-bottom > .change-scheme-box > .change-scheme').stop().animate({ right:"0" }, function() {
				$('.btn-scheme').removeClass("open").addClass("close");
			});
			},
			function () {
			$('#p-bottom > .change-scheme-box > .change-scheme').stop().animate({ right:"-" + $(".select-scheme").outerWidth() + "px" }, function() {
				$('.btn-scheme').removeClass("close").addClass("open");
			});
			}
		);
	
	}else{
	/* Scheme animate show/hiden - not CSS - remove HTML code */
		$("#p-bottom > .change-scheme-box").remove();
	/* remove search forms tabs*/
		$("#tabs").remove();
	/* remove tooltip hiden HTML */
		$(".tooltiphtml").remove();
	/* 3D postupy - not CSS - remove HTML code */
		$("#procedure-3d").html('<p><strong>Funkce není pro tuto verzi webu dostupná.</strong></p>');
		$("#prev-3d").remove();
		$("#next-3d").remove();
	}// hasCSS END
});// document.ready

/*
 * ToolTip v1.2
 *
 * Date: 2009-11-27 17:10
 */
$.prototype.tooltip = function(oy,ox,boxid,loadhtml,isDictionary){
	/* CONFIG */		
		//ox = 50;
		//oy = -168;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */	
	if(boxid=='') boxid = 'tooltip';
	//if(boxpos=='') boxpos = 'bottom';$("a", $(this))
	var tooltip = this;
	tooltip.hover(function(e){											  
	
	if(isDictionary===true){
	//$(this).each( function() {
		/*var textLocDictionary = {
			'locStr': 'xxx'
		};*/
		var url = $("a", $(this)).attr("href");
		var kWord = $("a", $(this)).text();
		var title = $(this).attr("title");
		var rootWord = $("a", $(this)).attr("title");
		$("a", $(this)).attr("title","");
		$(this).data("kWord", kWord);
		$(this).data("url", url);
		$(this).data("title", title);
		$(this).data("rootWord", rootWord);
		$("body").append('<span id="tooltiphtml" class="tooltiphtml"><h5>'+ $(this).data("rootWord")+ '<\/h5><p>'+ $(this).data("title")+ '<\/p><div class="bottom">'+ textLocDictionary +'<\/span><\/div><\/span>');
	//});
	}
		
		if(loadhtml===true){
			/*this.lhtml = 'Html obsah bude zde.';*/
			if(isDictionary===true){
				this.lhtml = $("#tooltiphtml").html();
			}else{
				this.lhtml = $(this).find(".tooltiphtml").html();
			}

			this.t = this.title;
			this.title = "";	
			
			$("body").append("<div id='"+ boxid +"'>"+ this.lhtml +"</div>");
		}else{
			this.t = this.title;
			this.title = "";	
			$("body").append("<div id='"+ boxid +"'>"+ this.t +"</div>");
		}
		
		if(oy<=0){
			$("#"+ boxid +"")
				.css("top",((e.pageY - oy)) + "px")
				.css("left",(e.pageX + ox) + "px")
				.fadeIn("fast");	
		}else{
			this.th = $("#"+ boxid +"").outerHeight();
			$("#"+ boxid +"")
				.css("top",((e.pageY - oy)-this.th) + "px")
				.css("left",(e.pageX + ox) + "px")
				.fadeIn("fast");	
		}
    },
	function(){
		/*if(loadhtml===false){
		}*/
			this.title = this.t;
			if(isDictionary===true){
				$("a", $(this)).attr("title", $(this).data("rootWord"));
				$("#tooltiphtml").remove();
			}
			$("#"+ boxid +"").remove();
    });	
	tooltip.mousemove(function(e){
		if(oy<=0){
			$("#"+ boxid +"")
				.css("top",((e.pageY - oy)) + "px")
				.css("left",(e.pageX + ox) + "px")
				.fadeIn("fast");	
		}else{
			this.th = $("#"+ boxid +"").outerHeight();
			$("#"+ boxid +"")
				.css("top",((e.pageY - oy)-this.th) + "px")
				.css("left",(e.pageX + ox) + "px")
				.fadeIn("fast");	
		}
	});			
};

/*
 * makeTabs v1.0
 *
 * Date: 2009-3-9 14:00
 */
$.prototype.makeTabs = function(activeTab) {
    var tabs = $("#" + this.attr("id") + " li a");
    if(tabs.length == 0) return;
    for (var ti=0; ti < tabs.length; ti++)
    {
        $("div[class*='tabs-" + $(tabs[ti]).attr("href").substring(1) + "']").hide();
        $(tabs[ti]).bind("click", function() {
            for (var tiin=0; tiin < tabs.length; tiin++)
            {
                $(tabs[tiin]).removeClass("active");
                $("div[class*='tabs-" + $(tabs[tiin]).attr("href").substring(1) + "']").hide();
            }
            $(this).addClass("active");
            $("div[class*='tabs-" + $(this).attr("href").substring(1) + "']").show();
            return false;
        });
    }
     if (typeof(activeTab) == "undefined" || isNaN(activeTab) || activeTab < 1 || activeTab > tabs.length) {
        activeTab = 0;
    } else {
        activeTab--;
    }

    $("#" + this.attr("id") + " li a:eq("+activeTab+")").addClass("active");
    $("div[class*='tabs-" + $(tabs[activeTab]).attr("href").substring(1) + "']").show();
};

/*
 * Market places v1.0
 *
 * Date: 2009-11-25 8:59
 */
$.prototype.marketplaces = function(idShowNamePlace){	
	var place = this;
	var defNamePlace = $("#" + idShowNamePlace).text();
	place.hover(function(e){	
											 
		this.nameplace = this.title;
		this.title = "";
		
		$("#r" + this.id).show();
		$("#" + idShowNamePlace).text(this.nameplace);

		return false;
    },
	function(){
		this.title = this.nameplace;
		$("#r" + this.id).hide();
		$("#" + idShowNamePlace).text(defNamePlace);
		
		return false;
	});			
};

/*
 * boxOrder form v1.1
 *
 * Date: 2010-02-03 10:35
 */
$.prototype.orderBox = function(idOrderBasket, boxPosition){
	var orderBox = this;
	var parent;
	var newPositionForBox = 0;
	if (boxPosition==="static"){ changePosition = false; }else{ changePosition = true; }
	orderBox.click(function(e){
		if (changePosition){
			parent = $(this).parent().parent();
			newPositionForBox = parent.position().top;
		}
		else{
			newPositionForBox = $(this).position().top;
		}
		if (!$(idOrderBasket).hasClass("no-data-read")){
			if ($(this).hasClass("ks")){
				$(idOrderBasket+" #number-of").val($(this).html());
				$(idOrderBasket+" #pallets").val(parent.find("a.palet").html());
				$(idOrderBasket+" .itemCode").val(parent.find("td:eq(2)").html());
			}else{
				$(idOrderBasket+" #number-of").val(parent.find("a.ks").html());
				$(idOrderBasket+" #pallets").val($(this).html());
				$(idOrderBasket+" .itemCode").val(parent.find("td:eq(2)").html());
			}
		}
		$(idOrderBasket).css("top",newPositionForBox+"px");
		$(idOrderBasket).removeClass("hide");
		$(idOrderBasket+" .confirm").click(function(){
			$(idOrderBasket).addClass("hide");
		});
		return false;
	});
};

/*
 * HighslideChangeColor v1.0
 *
 * Date: 2010-01-11 11:06
 */
 var arr, parentId;
$.prototype.highslideFromArray = function(mainContentId, highslideWrapperClass, labelName, thumbArray, idFormParent){
	var openElement = this;
	var indexId = 0;
	openElement.click(function(e){
		indexId++;
		if (idFormParent){
			var parentP = $(this).parents().get(3);
			parentId = parentP.id;
			arr = jQuery.grep(thumbArray, function(hodnota){ return (hodnota.elId==parentId);});
			hs.skin.contentWrapper =
				'<div class="highslide-header">'+
					'<div class="highslide-header-buttons clear"><\/div>'+
					'<h1>'+arr[0].title+'<\/h1>'+
				'<\/div>'+
				'<div class="highslide-body"><\/div>';
			$(this).parent().append('<div id="'+mainContentId+'-'+indexId+'" class="highslide-maincontent"><img src="'+arr[0].colorPreviewUrl+'" width="522" height="359" alt="" style="border: 20px solid #ffffff;" /></div><div class="highslide-caption"><div class="highslide-number"><strong>'+actualThumbNumber+'</strong>/'+arr.length+'</div>'+labelName+' - <strong>'+arr[0].label+'</strong></div>');
			return hs.htmlExpand(this, { wrapperClassName : highslideWrapperClass, width: 720});
		}
		else{
			arr = thumbArray;
			hs.skin.contentWrapper =
				'<div class="highslide-header">'+
					'<div class="highslide-header-buttons clear"><\/div>'+
					'<h1>'+arr[0].title+'<\/h1>'+
				'<\/div>'+
				'<div class="highslide-body"><\/div>';
			$(this).parent().append('<div id="'+mainContentId+'-'+indexId+'" class="highslide-maincontent detail"><img src="'+arr[0].previewUrl+'" width="522" height="359" alt="" style="border: 20px solid #ffffff;" /></div><div class="highslide-caption"><div class="highslide-number"><strong>'+actualThumbNumber+'</strong>/'+arr.length+'</div>'+labelName+' - <strong>'+arr[0].label+'</strong></div>');
			return hs.htmlExpand(this, { wrapperClassName : highslideWrapperClass, width: 720});
		}
	});
};

/*
 * Box show and hide v1.0
 *
 * Date: 2010-01-29 09:51
 */
$.prototype.showAndHideBox = function(options){
	var defaults = {
		speed:					200,
		selector:				"showHideBoxAnimate",
		boxIdentificator:		"box",
		buttonIdentificator:	"btn",
		classForShow:			"show",
		buttonActiveBox:		"h4 a.showBox",
		hideText:				"Show box",
		showText:				"Hide box"
	}; 
	
	var options = $.extend(defaults, options);
	
	if (!$("." + options.selector + "-" + options.boxIdentificator + ", ." + options.selector + "-" + options.buttonIdentificator).hasClass("show")) {
		$("." + options.selector + "-" + options.boxIdentificator + ", ." + options.selector + "-" + options.buttonIdentificator).hide();
	}
	$(options.buttonActiveBox).click(function(){
		if ($("."+options.selector+"-"+options.boxIdentificator).is(":hidden")){
			$("."+options.selector+"-"+options.boxIdentificator+", ."+options.selector+"-"+options.buttonIdentificator).slideDown(options.speed);
			$(this).parent().addClass(options.classForShow).find("a").html(options.hideText);
		}else{
			$("."+options.selector+"-"+options.boxIdentificator+", ."+options.selector+"-"+options.buttonIdentificator).slideUp(options.speed);
			$(this).parent().removeClass(options.classForShow).find("a").html(options.showText);
		}
		return false;
	}).removeClass(options.classForShow).html(options.showText);
};

/*
 * Change After Loads v1.0
 *
 * Date: 2010-03-25 16:05
 */
(function ($) {
    $.fn.changeAfterLoads = function (options) {
        var opts = $.extend({}, $.fn.changeAfterLoads.defaults, options);
        return this.each(function () {
            var $this = $(this);
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

						/* Zjistíme počet itemů k posunu */
						var numItemInBox = $("#"+ $this.attr("id") +" ."+ o.classChangeBox +" > *").size();
            /* Zjistíme šířku všech itemů */
						var sumWidthItemInBox = 0;
						var wBox = $("#"+ $this.attr("id") +" ."+ o.classChangeBox +" > *").map(function(){  
							sumWidthItemInBox = sumWidthItemInBox + $(this).width();
							return sumWidthItemInBox;   
     				}).get((numItemInBox-1));  
            /* Nastavíme šířku boxu posunu */
						$("#"+ $this.attr("id") +" ."+ o.classChangeBox).css("width",wBox + "px");
            /* Podíváme se do cookie */
						if ($.cookie(o.cookieName)) {
							// uložíme do cookie novou hodnotu
							if(Number($.cookie(o.cookieName))>=numItemInBox){
								var setCookieValue = 1;
							}else{
								var setCookieValue = Number($.cookie(o.cookieName))+1;
							};
							$.fn.changeAfterLoads.set($this, setCookieValue, o.cookieExp, o.cookiePath, o.cookieName);
							// existuje > posuneme box dle hodnoty
							$("#"+ $this.attr("id") +" ."+ o.classChangeBox).css("left", "-" + ((wBox/numItemInBox) * (Number($.cookie(o.cookieName))-1)) + "px");
            } else {
							// neexistuje nastavíme cookie
              $.fn.changeAfterLoads.set($this, 1, o.cookieExp, o.cookiePath, o.cookieName);
            }
						
        }); //return
    };
    $.fn.changeAfterLoads.set = function (obj, value, expi, path, cookiename) {
            $.cookie(cookiename, value, {
                "expires": expi,
                "path": path
            });
        return false;
    };
    $.fn.changeAfterLoads.defaults = {
        classChangeBox: 'banners',
        cookiePath: '/',
        cookieExp: 365,
        cookieName: 'FG_changeAfterLoads'
    };
})(jQuery);
