// JavaScript Document

Cufon.replace('.logoText span', { fontFamily: 'DaxMedium', textShadow: '#383736 1px 1px' });
Cufon.replace('div.mainMenu ul li a, div.breadcrumbsWrapper, div.subMenuRowWrapper ul li a, div.goBackLinkHolder a, div.productInfoWrapper div.image span.zoom a, div.printIconHolder a, div.nextPreviousSpan a', { fontFamily: 'Georgia', hover:true });
//Cufon.replace('h1, div.homeBillets h2, div.newsbreefHolder h2, div.newsbreefHolder div.text, div.seriesPage div.billet h2, div.productOverviewList div.item a span.productTitle, div.productInfoWrapper div.info div.nameAndPrice h2, div.productInfoWrapper div.info div.materials h2, div.productInfoWrapper div.info div.materials p, div.productInfoPage h2.sameProducts, div.basketPopupHolder table td.title p, div.basketPopupHolder table td.details p.title, div.sizeChartPopup h2, div.inloggenContent h2, div.orderHistoryOverview div.orderContent div.titleHolder h2', { fontFamily: 'Georgia' });
Cufon.replace('h1, div.homeBillets h2, div.newsbreefHolder h2, div.newsbreefHolder div.text, div.seriesPage div.billet h2, div.productOverviewList div.item a span.productTitle, div.productInfoWrapper div.info div.nameAndPrice h2, div.productInfoWrapper div.info div.materials h2, div.productInfoWrapper div.info div.materials p, div.productInfoPage h2.sameProducts, div.basketPopupHolder table td.title p, div.basketPopupHolder table td.details p.title, div.sizeChartPopup h2, div.inloggenContent h2, div.orderHistoryOverview div.orderContent div.titleHolder h2, div#popupWindow h2, .cufonText, span.pagetitle', { fontFamily: 'Georgia' });
Cufon.replace('.loginForm a, .rightMenu a', { fontFamily: 'Georgia', hover: true, textShadow: '#383736 1px 1px' });
Cufon.replace('div.linksAdvancedItemContainer h2', { fontFamily: 'Georgia', hover: true });



$(document).ready(function() {

    // PREPARING MAIN MENU TABS
    $('div.mainMenu li').after('<li class="tab"><!--  --></li>');
    $('div.mainMenu ul').prepend('<li class="tab"><!--  --></li>');
    $('div.mainMenu li:first').addClass('first');
    $('div.mainMenu li:last').addClass('last');
    $('div.mainMenu li.active').prev().addClass('prevActive');
    $('div.mainMenu li.active').next().addClass('nextActive');
    if ($('div.mainMenu li.first').hasClass('prevActive')) $('div.mainMenu li.first').removeClass('prevActive').addClass('firstActive');
    if ($('div.mainMenu li.last').hasClass('nextActive')) $('div.mainMenu li.last').removeClass('nextActive').addClass('lastActive');
    $('div.mainMenu li.red').prev().addClass('prevRed');
    $('div.mainMenu li.red').next().addClass('nextRed');
    if ($('div.mainMenu li.first').hasClass('prevRed')) $('div.mainMenu li.first').removeClass('prevRed').addClass('firstRed');
    if ($('div.mainMenu li.last').hasClass('nextRed')) $('div.mainMenu li.last').removeClass('nextRed').addClass('lastRed');
    if ($('div.mainMenu li.nextActive').hasClass('prevRed')) $('div.mainMenu li.nextActive').removeClass('prevRed nextActive').addClass('aciveToRed');

    if ($('div.mainMenu li.active a').hasClass('gray')) {
        $('div.mainMenu li.active').removeClass('active').addClass('activeGray');
        $('div.mainMenu li.nextActive').removeClass('nextActive').addClass('nextGray');
        $('div.mainMenu li.firstActive').removeClass('firstActive').addClass('firstGray');
        $('div.mainMenu li.prevActive').removeClass('prevActive').addClass('prevGray');
        $('div.mainMenu li.aciveToRed').removeClass('aciveToRed').addClass('grayToRed');
    }

    $('div.mainMenu li:not(.tab)').hover(
		function() {
		    $(this).addClass('hover');
		    $(this).prev().addClass('prevhover');
		    $(this).next().addClass('nexthover');
		},
		function() {
		    $(this).removeClass('hover');
		    $(this).prev().removeClass('prevhover');
		    $(this).next().removeClass('nexthover');
		}
	);

    $('span.btnHolder').hover(
		function() {
		    $(this).addClass('btnHover');
		},
		function() {
		    $(this).removeClass('btnHover');
		}
	);

    fixLockAllDiv();

    $(window).resize(function() {
        fixLockAllDiv();
    });

    // SWITCH LANGUAGE SELECT BOX
    selectLanguage('div.menuRow div.rightMenu select');
    Cufon.replace('span.selectHolder span.listHolder span.li', { fontFamily: 'Georgia' });
    inputFocus();

});

function inputFocus() {
    var value = $('input.search-text').val();

    $('input.search-text').focus(function() {
        if ($(this).val() == value) {
            $(this).val("");
        }
    });
    $('input.search-text').blur(function() {
        if ($(this).val() == "") {
            $(this).val(value);
        }
    });

}

function selectLanguage(selectObj) {
    $(selectObj).each(function() {
		if ($(this).hasClass('selectHadToggled')) return;
		$(this).addClass('selectHadToggled');
        var $parentSelect = $(this);
        var selectName = $parentSelect.attr("name");
        $parentSelect.wrap('<span class="selectHolder"></span>').wrap('<span class="text"></span>').after('<ins class="inputBox"></ins><span class="arrow"></span>');
        var $selectHolder = $parentSelect.parent().parent();
        $selectHolder.attr("style", $parentSelect.attr("style"));
        var $selectInput = $selectHolder.find("span.text").find("ins.inputBox");
        $selectInput.css("width", $parentSelect.width() - 26);
        var $selectArrow = $selectHolder.find("span.text").find("span.arrow");
        $selectHolder.append('<span class="listHolder"></span>');
        var $listHolder = $selectHolder.find('span.listHolder');
        $listHolder.append(doSelectList($parentSelect, ''));
        $parentSelect.hide();
        $listHolder.css("width", $selectHolder.width() - 20);
		$listHolder.find('span.li:last').addClass('lastChild');
		$listHolder.append('<span class="closeList"><!--  --></span>');
		/*$listHolder.find('span.closeList').css("width", $selectHolder.width() - 20);*/
        var defaultVal = $(this).find("option:selected").text();
        $selectInput.text(defaultVal);
		Cufon.replace($selectInput, { fontFamily: 'Georgia', hover:true, textShadow: '#383736 1px 1px' });
        
        var hasFocus = false;
        var keepFocus = false;
        
        $selectArrow.mousedown(function() {
            $(this).css("background-position", "0px -21px");
        })
        .mouseup(function() {
            $(this).css("background-position", "0px 0px");
        })
        .click(function() {
            $(this).css("background-position", "0px 0px");
            //$selectInput.focus();
			hasFocus = true;
			$selectHolder.addClass("positionRelative");
			$listHolder.show();
        });

        $listHolder.mousedown(function() {
            keepFocus = hasFocus;
        });
        
        $listHolder.find("span.li").mousedown(function() {
            keepFocus = false;
            hasFocus = false;
            $selectInput.text($(this).text());
            $parentSelect.val($(this).attr("value"));
            $parentSelect.change();
			hasFocus = false;
			if ($.browser.mozilla) keepFocus = false;
			if (!keepFocus) {
				$listHolder.hide();
				$selectHolder.removeClass("positionRelative");
			} else {
				keepFocus = false;
				//$(this).focus();
			}
			Cufon.refresh($selectInput);
        });
        
		$selectInput
		.click(function() {
			if (hasFocus) {
				hasFocus = false;
				if ($.browser.mozilla) keepFocus = false;
				if (!keepFocus) {
					$listHolder.hide();
					$selectHolder.removeClass("positionRelative");
				} else {
					keepFocus = false;
					//$(this).focus();
				}
			} else {
				hasFocus = true;
				$selectHolder.addClass("positionRelative");
				$listHolder.show();
			}
		})
		.hover(
			function() {
				$(this).addClass("hover");
				Cufon.refresh($selectInput);
			}, 
			function() {
				$(this).removeClass("hover");
				Cufon.refresh($selectInput);
			}
		);
    });
    
    $("span.selectHolder span.listHolder span.li:not(.selected)").hover(
        function() {
            $(this).addClass("hover");
			Cufon.refresh('span.selectHolder span.listHolder span.li');
        }, 
        function() {
            $(this).removeClass("hover");
			Cufon.refresh('span.selectHolder span.listHolder span.li');
        }
    );
}

function doSelectList($selectObj, startStr) {
    var returnString = startStr;
    $selectObj.children().each(function() {
        if ($(this)[0].tagName.toUpperCase() == "OPTION") {
            returnString += '<span class="li';
			if ($(this).attr('selected')) returnString += ' selected';
			returnString += '" value="' + $(this).val() + '">' + $(this).text() + '</span>';
        } else {
            returnString += '<span class="group">' + $(this).attr("label") + '</span>';
            returnString += doSelectList($(this), '');
        }
    });
    return returnString;
}

function fixSelectInputs($holder) {
    $holder.find("span.selectHolder").each(function() {
        $(this).find("span.text ins.inputBox").width(parseInt($(this).css("width")) - 30);
        $(this).find("span.listHolder").width(parseInt($(this).css("width")) - 8);
    });
}

function onFocusInput(inputObj, defaultText) {
    if ($.trim($(inputObj).val()) == defaultText) {
		$(inputObj).val("").addClass("focusInput");
	}
}

function onBlurInput(inputObj, defaultText) {
    if (($.trim($(inputObj).val()) == "") || ($(inputObj).val().toLowerCase() == defaultText.toLowerCase())) {
		$(inputObj).val(defaultText).removeClass("focusInput");;
	}
}

function fixLockAllDiv() {
    $('div#blockAll').width(0).height(0);
    $('div#blockAll').width($(document).width()).height($(document).height());
}

function openInfoHolder(id) {
    var spanHolder = '#' + id + ' span.text';
    var imageHolder = '#' + id + ' img.corner';
    if ($.browser.msie) {
        $(spanHolder).show();
        $(imageHolder).show();
    } else {
        $(spanHolder).fadeIn(150);
        $(imageHolder).fadeIn(150);
    }
}

function hideInfoHolder(id) {
    if ($.browser.msie) {
        $("a.infoHolder span.text").hide();
        $("a.infoHolder img.corner").hide();
    } else {
        $("a.infoHolder span.text").fadeOut(150);
        $("a.infoHolder img.corner").fadeOut(150);
    }
}

function changeItemImg(linkItemObj, imgPath, imgHolder) {
    if (imgHolder == null) {
        if ($(linkItemObj).parents("div.item:first").length > 0) {
            var imageObject = $(linkItemObj).parents("div.item:first").find("img.productImg:first");
        } else {
        var imageObject = $(linkItemObj).parents("div.productDetailHolder:first").find("img.productImg:first");
        }
        $(imageObject).animate({ opacity: 0 }, 150).delay(150, function() {
            $(this).attr("src", imgPath).delay(50, function() {
                $(this).animate({ opacity: 1 }, 150)
            });
        });
    } else {
        var imageObject = $(imgHolder);
        $(imageObject).animate({ opacity: 0 }, 150).delay(150, function() {
            $(this).attr("src", imgPath).delay(50, function() {
                $(this).animate({ opacity: 1 }, 150)
            });
        });
    }
}

$.fn.delay = function(time, callback) {
    jQuery.fx.step.delay = function() { };
    return this.animate({ delay: 1 }, time, callback);
}

