// Java Script Document

Cufon.replace('.cufonText', { fontFamily: 'Century Gothic' });

/*Round Corners*/
$(function() {
    layoutFix();
    thumb();
    thumbt();
    thumbsl();
    showPopUp();
    fixNews();
    hoverSlider();
    errorMes();
    toggleButtons();
    FooterImageClick();
    makeHomeRotator();
    cutLinks();
    watermark();
    orderSteps();
    thumbtop();
    calc();
    calcProductOrder();
    CheckSelected();
    toggleInputFields();
    OrderPageButtonFix();
})
$(function() {
    $('.bottomSlider').click(function() {
        if (footer.is(":hidden")) {
            footer.slideDown();
        }
        else {
            footer.slideUp();
        }
    });
});
function OrderPageButtonFix() {
    $('.contentColumn .newTabs .contentItems .SizeCollection').each(function() {
        if ($(this) != null) {
            var buttonContainer = $(this);
            var margin = buttonContainer.height() / 2 - 15 + "px";
            buttonContainer.next().css("margin-top", margin);
        }
    });
}

function CheckSelected() {
    $('.contentItems .chooseItemCh input').click(function() {
        return CheckProductCount(this);
    });
}

function CheckProductCount(obj) {
    var count = 0;
    var items = $(obj).parent().parent().parent().prev().find('.item').each(function() {
        var input = $(this).find('input');
        if (input.val() == "")
            input.val("0");

        count = count + parseInt(input.val());
    });
    return count != 0;
}

function calcProductOrder() {
    $('.SizeCollection .item .inputSwit input').keyup(function () {
        var value = $(this).val();
        if (value == "" || !IsNumeric(value))
            value = 0;

        var totalPriceEl = $(this).parent().parent().parent().next();
        var totalText = totalPriceEl.find('span');
        var price = totalPriceEl.find('#price').val().replace(",", ".");
        var totalPrice = totalPriceEl.find('#totalPrice');
        var totalValue = (parseFloat(price) * value).toFixed(2);
        totalText.html(totalValue);
        totalPrice.val(totalValue);
    });
}
function underlineContentLinks() {
    var allLinks = $(".textContent a, .textContentNeu a, .contentColumn a");
    var neededLinks = allLinks.filter("[href$=pdf],[href$=doc],[href$=docx],[href$=zip],[href$=rar]");
    $(neededLinks).each(function() {
        $(this).css("text-decoration", "underline");
        $(this).addClass("contentLink");
    });
}
//tbCountVariant

function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;

    if (sText.length > 0) {
        for (i = 0; i < sText.length && IsNumber == true; i++) {
            Char = sText.charAt(i);
            if (ValidChars.indexOf(Char) == -1) {
                IsNumber = false;
            }
        }
        return IsNumber;
    }
    else {
        return false;
    }
}

function calc() {
    $('.sizeContainer .item input.inputText').keyup(function() {
        var value = $(this).val();
        if (value == "" || !IsNumeric(value))
            value = 0;
        var parent = $(this).parent().parent();
        var price = parseFloat(parent.find('#price').val().replace(",", "."));
        var totalPrice = parent.find('.totalPrice');
        var hidden = parent.find('#totalprice');
        var valTo = ((parseFloat(price) * value).toFixed(2)).replace(".", ",");
        totalPrice.text(valTo);
        hidden.val(valTo);
        calcAll();
    });
    $('.sizeContainer .item input').focus(
    function() {
        if ($(this).hasClass('waterMark')) {
            $(this).val("");
        }
    }
    );
    $('.sizeContainer .item input.waterMark').blur(
    function() {
    if ($(this).val() == 0) {
        $(this).addClass('waterMark');
         }
        else {
            $(this).removeClass('waterMark');
        }
    }
    );
    $('.inputSwit  input.waterMark').each(
    function() {
         if ($(this).val() > 0) {
             $(this).val("0");
        }
    }
    );
    $('.sizeContainer .item  input.waterMark').each(
    function() {
        if ($(this).val() > 0) {
            $(this).val("0");
        }
        else {
            $(this).val("0");
        }
    }
    );

    $('.inputSwit  input.waterMark').each(
    function() {
        if ($(this).val() > 0) {
            $(this).val("0");
        }
        else {
            $(this).val("0");
        }
    }
    );
 
    
    $('.inputSwit  input').focus(
    function() {
        if ($(this).hasClass('waterMark')) {
            $(this).val("");
        }
    }
    );
    $('.inputSwit  input.waterMark').blur(
    function() {
    if ($(this).val() == 0) {
        $(this).val("0").addClass('waterMark');
        }
        else {
            $(this).removeClass('waterMark');
        }
    }
    );
}

function calcAll() {
    var totalPrice = 0.0;
    $("span.sizeContainer input[id^='totalprice']").each(function() {
        totalPrice += parseFloat($(this).val().replace(",", "."));
    });
    $("span.priceValue").html(totalPrice.toFixed(2).toString().replace(".", ","));
}

function fixNews() {
    $('.nextNews:nth-child(-n+2)').addClass('fixFirst');
    $('.mainNews').next('.nextNews').addClass('fixFirst');
}


// SLIDER PRODUCT DETAIL


function hoverSlider() {
    $('.sliderDetail ul li a').hover(
    function() {
        $(this).addClass('fhoverImg');
    },
       function() {
        $(this).removeClass('fhoverImg');
    }
    );
}


$(function() {
    $('.cityChangeWrap').click(function() {
        return false;
    });
    $('.cityChangeWrap h2>.link').click(function() {
        return false;
    });

    function updateStylishSelect(select) {
        var selectedOption = select.find(':selected');
        $('.cityChangeWrap h2>.link').text(selectedOption.text())
                                     .css('color', selectedOption.val() == "0" ? "#3f3f3f" : "#8d8e88");
    }
    var stylishSelect = $('.cityChangeWrap select[id$="ddlCustomStylish"]');
    updateStylishSelect(stylishSelect);
    stylishSelect.change(function () {
        updateStylishSelect($(this));
    });

    $('.cityChangeWrap .select').css({ 'opacity': 0 });
});


// ORDERSTEPS FIX

function orderSteps() {
    $('.orderStepsHolder li.passed .orderStepText').each(function() {
        str = $(this).text();
        //str = str.slice(3);
        $(this).text(str);


    });
    $('div.orderStepsHolder li.passive:nth-child(-n+3)').after('<span class="nextStep"></span>');
    $('div.orderStepsHolder li.active:nth-child(-n+3)').after('<span class="nextStep active"></span>')
    $('.orderStepsHolder li.passed .orderStepText').before('<span class="orderDoneImg"></span>');
    $('.orderStepsHolder li.passed:nth-child(-n+3)').after('<span class="nextStep dubactive"></span>');




}

//  INPUT BLANK WATERMARK
function watermark() {
    $('input[watermark]').each(function() {
        $(this).watermark($(this).attr('watermark'), { className: 'waterMark' });
    });
}

function layoutFix() {
    $('.menuBlock li.active').next().addClass('afterAcrive');
    $('.productListContainer div.productListItem:nth-child(3n)').addClass('lastInLine').after('<div class="clear"></div>');
    $('.productListContainer .productListItem:gt(45)').addClass('lastInColumn');
    //$('.infoAccountContainer:nth-child(2n+2)').addClass('lastInfo');
}

function FooterImageClick() {
    footer = $('.hiddenPart');
    $('.siteMapBtn').click(function() {
        if (footer.is(":hidden")) {
            footer.slideDown();
            $(this).addClass('locked');
        }
        else {
            footer.slideUp();
            $(this).removeClass('locked');
        }
    });
}

function toggleButtons() {
    $("input.btnDecor").each(function() {
        if ($(this).hasClass('big')) {
            $(this).wrap('<span class="btnDecorated big"></span>');
        }
        else {
            $(this).wrap('<span class="btnDecorated"></span>');
        }
        $(this).before('<span class="btnLeft"></span>');
        $(this).after('<span class="btnRight"></span>');
        $(this).wrap('<span class="btnMid"></span>');
        $(this).removeClass('btnDecor')
    });
    $("a.btnDecor").each(function() {
        $(this).addClass('btnDecorated');
        $(this).wrapInner('<span class="btnMid"></span>');
        $(this).prepend('<span class="btnLeft"></span>');
        $(this).append('<span class="btnRight"></span>');
        $(this).removeClass('btnDecor')
    });
}

function toggleInputFields() {
    $("input.decorInput").each(function() {
        $(this).wrap('<span class="inputHolder"></span>');
        $(this).before('<span class="inputStart"></span>');
        $(this).after('<span class="closeBg"></span>');
        //        $(this).css("width", $(this).width() - 10);
        $(this).css("width", parseInt($(this).css("width")) - 10);
    });
}

function errorMes() {
    if ($(".errorMsg").length > 0) {
        $(".errorMsg").each(function () {
            if ($(this).hasClass('check')) return
            $(this).addClass('check');
            $(this).wrap('<div class="errorContainer"><span class="centerError"></span>');
            $(this).before('<span class="leftError"></span>');
            $(this).after('<span class="rightError"></span> </div>');
            $(".errorMsg").parent('.centerError').parent('.errorContainer').parent('.inputContainer').prev('.labelContainer').addClass('Error');

        });

    } else {
        $(".labelContainer label").addClass('none');
    };

    if ($(".errorMsgA").length > 0) {
        $(".errorMsgA").each(function() {
            $(this).wrap('<div class="errorContainer"><span class="centerErrorA"></span>');
            $(this).before('<span class="leftErrorA"></span>');
            $(this).after('<span class="rightErrorA"></span> </div>');


        });

    } else {
        $(".labelContainer label").addClass('none');
    };
}



function showPopUp() {
    $('.showPopUp').hover(
	function() {
	    $(this).find('.popUpContent:first').stop(true, true);
	    if ($.browser.msie == true) {
	        $(this).find('.popUpContent:first').show();
	    } else {
	        $(this).find('.popUpContent:first').fadeIn(200);
	    }
	},
		function() {
		    if ($.browser.msie == true) {
		        $(this).find('.popUpContent:first').hide();
		    } else {
		        $(this).find('.popUpContent:first').fadeOut(200);
		    }
		}
	);
}

function thumb() {
    $('div.thumb').append('<span class="tl"></span><span class="tr"></span><span class="bl"></span><span class="br"></span>');
}

function thumbt() {
    $('div.thumbt').append('<span class="bl"></span><span class="br"></span>');
}

function thumbsl() {
    $('div.sliderNews').append('<span class="tl"></span><span class="bl"></span>');
}

function thumbtop() {
    $('div.textTitle').append('<span class="tl"></span><span class="tr"></span>');
}

function makeHomeRotator() {
    var rotObj = new Object();
    var itemsCount = $('div.rotatorContainer div.item').length;
    var currentRotItem;
    var isFading = false;
    var homeRotatorTime = 3;
    var homeRotatorTimer;

    $('div.rotatorContainer').append('<div class="rotator-pager"></div>');
    for (var i = 0; i < itemsCount; i++) {
        $('div.rotator-pager').append('<div class="item"><!--  --></div>');
    }

    $('div.rotator-pager div.item').each(function(i) {
        $(this)
		.hover(
			function() {
			    $(this).addClass('hover');
			},
			function() {
			    $(this).removeClass('hover');
			}
		)
		.click(
			function() {
			    if ($(this).hasClass('active')) return;
			    if (isFading) return;
			    isFading = true;
			    $('div.rotator-pager div.item').removeClass('active');
			    $(this).addClass('active');
			    clearInterval(homeRotatorTimer);
			    $('div.rotatorContainer > div.item:nth-child(' + currentRotItem + ')').fadeOut(400);
			    currentRotItem = i + 1;
			    $('div.rotatorContainer > div.item:nth-child(' + currentRotItem + ')').fadeIn(400,
					function() {
					    isFading = false;
					    homeRotatorTimer = setInterval(rotate, homeRotatorTime * 1000);
					}
				);
			}
		)
    });

    currentRotItem = 1;
    $('div.rotator-pager div.item:nth-child(1)').addClass('active');
    $('div.rotatorContainer > div.item:nth-child(1)').show();

    if (itemsCount > 1)
        homeRotatorTimer = setInterval(rotate, homeRotatorTime * 1000);

    function rotate() {
        $('div.rotatorContainer > div.item:nth-child(' + currentRotItem + ')').stop(true, true);
        clearInterval(homeRotatorTimer);
        $('div.rotator-pager div.item').removeClass('active');
        $('div.rotatorContainer > div.item:nth-child(' + currentRotItem + ')').fadeOut(600);
        currentRotItem++;
        if (currentRotItem > itemsCount) currentRotItem = 1;
        $('div.rotator-pager div.item:nth-child(' + currentRotItem + ')').addClass('active');
        $('div.rotatorContainer > div.item:nth-child(' + currentRotItem + ')').fadeIn(600,
			function() {
			    isFading = false;
			    homeRotatorTimer = setInterval(rotate, homeRotatorTime * 1000);
			}
		);
    }
}

/*ShortNews*/
function cutLinks() {
    $(".newsItem ul li a").each(function() {
        elapseLongText(this, 20);
    });

    $(".newsItem ul li span").each(function () {
        elapseLongText(this, 20);
    });

    var breadCrumbs = $('div.breadCrumbs a');
    if (breadCrumbs.length > 3) {
        breadCrumbs.slice(2).each(function () {
            elapseLongText(this, 13);
        });
        elapseLongText('div.breadCrumbs em', 22);
    }
}

function elapseLongText(element, maxLength) {
    var text = $(element).text();
    if (text.length > maxLength + 2) {
        $(element).attr('title', text);
        text = text.slice(0, maxLength) + '...';
        $(element).text(text);
    }
}

$(function() {
    $(window).resize(function() {
        Resize();
    });
});
function Resize() {
    var item = $("#TB_window:visible");
    if (item.length == 0)
        return;
    $("#TB_ImageOff").click();
}
