// JavaScript Document

var homeCarouselAutoRotate = 4; // in seconds


Cufon.replace('ul.main-nav li a', { fontFamily: 'HelveticaBold', textShadow: '0px 1px rgba(0, 0, 0, 0.2)' });
Cufon.replace('div.page-content h2, ul.actueel span.text span.title, div.page-content .content-text-block h1 , h1.center-title', { fontFamily: 'HelveticaBold' });
Cufon.replace('div.solutions-holder div.popup h2', { fontFamily: 'HelveticaMedium' });
Cufon.replace('div.home-image-holder ul.solutions-featurette div.popup table p', { fontFamily: 'HelveticaRoman' });
Cufon.replace('ul.page-nav li a, ul.page-nav li span', { fontFamily: 'HelveticaBold', hover: true });
Cufon.replace('div.footer-holder div.column-slogan', { fontFamily: 'HelveticaRoman', textShadow: '0px 1px rgba(0, 0, 0, 0.2)' });
Cufon.replace('div.page-title h2', { fontFamily: 'HelveticaMedium' });

var isIE = ($.browser.msie) ? true : false;

$(window)
.load(function () {

    customSelects();

    initHomeFeaturette('div.home-image-holder div.featurette-holder');
    if (isIE) {
        carouselPopupIE();
        pageCarouselFx();
    } else {
        carouselPopup();
        pageCarouselFx();
    }

    initHomeFeaturette('div.page-image-holder div.featurette-holder');
})
.resize(function () {
    fixImage('div.home-image-holder div.wrapper img, div.page-image-holder div.wrapper img');
});

function onFocus(inputObj, defaultText) {
    if ($.trim($(inputObj).val()) == defaultText) {
        $(inputObj).val("").removeClass("inactive");
    }
}

function onBlur(inputObj, defaultText) {
    if (($.trim($(inputObj).val()) == "") || ($(inputObj).val().toLowerCase() == defaultText.toLowerCase())) {
        $(inputObj).val(defaultText).addClass("inactive");
    }
}


function initPageTitleImage() {
    $('div.home-image-holder div.wrapper img, div.page-image-holder div.wrapper img').each(function () {
        var imgURL = $(this).attr('src');
        $(this).css('opacity', 0).parent('div.wrapper').css('background-image', 'url("' + imgURL + '")');
        $(this).load(function () {
            $(this).css('opacity', 1).hide();
            fixImage(this);
        });
        fixImage(this);
    });

}

function fixImage(img) {
    var wWidth = $(window).width();
    $(img).each(function () {
        $(this).css('width', '');
        if ($(this).width() < wWidth) {
            $(this).width(wWidth).show();
            $(this).css('margin-left', -wWidth / 2).css('margin-top', -$(this).height() / 2);
        } else {
            $(this).hide();
        }
    });
}

function customSelects() {
    $('select.custom-select:not(.switched)').each(function () {
        var $originalSelect = $(this);
        var currentText = $('option:selected', $originalSelect).text();
        var $selectHolder = $originalSelect.wrap('<span class="select-holder"></span>').parent();

        $selectHolder.append('<span class="input"><input class="first-state" type="text" readonly="readonly" name="" value="' + currentText + '" /><span class="arrow"><!--  --></span></span>');
        $selectHolder.append('<span class="select-list"><span class="min-height">' + optionsList($originalSelect) + '</span><span class="bottom"><!--  --></span></span>');

        var $customInput = $('span.input input', $selectHolder);
        var $customInputArrow = $('span.input span.arrow', $selectHolder);
        var $listHolder = $('span.select-list', $selectHolder);
        var $scrollHolder = $('span.min-height', $selectHolder);
        var clickonscroll = false;
        var clickonlink = false;

        if ($originalSelect[0].selectedIndex != 0) {
            $customInput.removeClass('first-state');
        }

        if ($originalSelect.hasClass('disabled')) {
            $selectHolder.addClass('disabled');
            return;
        }


        $customInput
		.bind('focus', function () {
		    $selectHolder.css('position', 'relative');
		    $listHolder.stop(true, true);
		    $listHolder.slideDown(100);
		})
		.bind('blur', function () {
		    if (clickonscroll) {
		        if (clickonlink) {
		            $listHolder.slideUp(50, function () {
		                $selectHolder.css('position', 'static');
		            });
		        } else {
		            $(this).focus();
		        }
		        clickonscroll = clickonlink = false;
		        return;
		    }
		    clickonscroll = clickonlink = false;
		    $listHolder.slideUp(50, function () {
		        $selectHolder.css('position', 'static');
		    });
		});

        $customInputArrow
		.bind('mousedown', function () {
		    $(this).addClass('pressed');
		})
		.bind('mouseup', function () {
		    $(this).removeClass('pressed');
		    $customInput.focus();
		});

        $('a', $listHolder).each(function () {
            $(this).bind('mousedown', function () {
                clickonlink = true;
                $originalSelect.val($(this).attr('data-value'));
                $originalSelect.change();
                $customInput.val($(this).text()).removeClass('first-state');
            });
        });

        $scrollHolder.bind('mousedown', function () {
            clickonscroll = true;
        });

        $originalSelect.addClass('switched');

    });

    function optionsList(sel) {
        var list = '';
        $('option', sel).each(function () {
            list += '<a href="javascript:void(0)" data-value="' + $(this).val() + '">' + $(this).text() + '</a>';
        });
        return list;
    }
}

function initHomeFeaturette(featuretteObj) {
    var $featuretteHolder = $(featuretteObj);
    var $itemsList = $('ul.solutions-featurette', $featuretteHolder);

    var itemsNumber = $('li', $itemsList).length - 1;

    $('li > div > a, li a', $itemsList).addClass('first-state');

    $('li:gt(0) > div > a , li:gt(0) > a', $itemsList).each(function () {
        $(this).clone(true).insertAfter(this).removeClass('first-state').addClass('active-state');
    });

    $('div.home-image-holder div.solutions-holder ul li a, div.page-image-holder div.solutions-holder ul li a').css({ 'visibility': 'visible' });

    Cufon.replace('ul.solutions-featurette > li > a.first-state', { fontFamily: 'HelveticaBold', textShadow: '0px 1px #fff' });

    var tmpWidth = 0;
    $('li:gt(0)', $itemsList).each(function () {
        tmpWidth += $(this).width();
    });

    var itemsPadding = Math.floor((800 - tmpWidth) / (itemsNumber * 2));
    var lastPadding = 800 - (tmpWidth + itemsPadding * 2 * itemsNumber);

    $('li:gt(0) > div > a , li:gt(0) > a', $itemsList).css({ 'padding-left': itemsPadding, 'padding-right': itemsPadding });
    $('li:last-child > div	> a , li:last-child > a', $itemsList).css('padding-right', itemsPadding + lastPadding);

    Cufon.replace('ul.solutions-featurette > li > a.active-state, , ul.solutions-featurette > li > a.fixed-action', { fontFamily: 'HelveticaBold' });

    $('li:gt(0)', $itemsList).hover(
		function () {
		    if ($(this).hasClass('active')) { return; }
		    $('a.active-state', this).stop(true, true);
		    $('a.active-state', this).fadeIn(250);
		},
		function () {
		    if ($(this).hasClass('shown', 'shown')) { return; }
		    $('a.active-state', this).fadeOut(100);

		}
	);
}

function initPopularProducts() {
    var $galleryHolder = $('div.popular-products div.gallery-holder');
    var $itemsList = $('ul.popular-products', $galleryHolder);
    var $leftArrow = $('a.arrow-left', $galleryHolder);
    var $rightArrow = $('a.arrow-right', $galleryHolder);

    var itemsNumber = $('li', $itemsList).length;
    var currentPage = 0;
    var pagesNumber = Math.ceil(itemsNumber / 4);
    var isMoving = false;

    $itemsList.width(itemsNumber * 92);

    $rightArrow.click(function () {
        if ($(this).hasClass('disabled')) { return; }
        if (isMoving) { return; }
        isMoving = true;
        currentPage++;
        $itemsList.animate({ 'left': -currentPage * 368 }, 500, function () { isMoving = false; })
        checkArrows();
    })
	.focus(function () { $(this).blur() });

    $leftArrow.click(function () {
        if ($(this).hasClass('disabled')) { return; }
        if (isMoving) { return; }
        isMoving = true;
        currentPage--;
        $itemsList.animate({ 'left': -currentPage * 368 }, 500, function () { isMoving = false; })
        checkArrows();
    })
	.focus(function () { $(this).blur() });

    checkArrows();

    function checkArrows() {
        if (currentPage == 0) {
            $leftArrow.addClass('disabled');
        } else {
            $leftArrow.removeClass('disabled');
        }
        if (currentPage == pagesNumber - 1) {
            $rightArrow.addClass('disabled');
        } else {
            $rightArrow.removeClass('disabled');
        }
    }
}

function initProductsList() {
    var $listHolder = $('div.products-list-holder ul.products-list');

    $('li.item > a', $listHolder)
	.each(function () {
	    if (isIE) {
	        $('span.text', this).css({ 'visibility': 'visible', 'bottom': 4 }).hide();
	    } else {
	        $('span.text', this).css({ 'visibility': 'visible', 'bottom': 10, 'opacity': 0 });
	    }
	})
	.hover(
		function () {
		    $('span.image-holder', this).stop(true, true);
		    $('span.image-holder', this).animate({ 'opacity': 0.7 }, 150);
		    $('span.text', this).stop(true, true);
		    if (isIE) {
		        $('span.text', this).show(0);
		    } else {
		        $('span.text', this).animate({ 'bottom': 4, 'opacity': 1 }, 250);
		    }
		},
		function () {
		    $('span.image-holder', this).animate({ 'opacity': 1 }, 100);
		    if (isIE) {
		        $('span.text', this).hide(0);
		    } else {
		        $('span.text', this).animate({ 'bottom': 10, 'opacity': 0 }, 100);
		    }
		}
	);

    if (isIE && jQuery.browser.version.substr(0, 1) == "7") {
        $('ul.products-list li').each(function () {
            $(this).click(function () {
                document.location.href = $('a', this).attr('href');
            })
        });
    }
}

function initGallery() {
    $('#productImage').load(function () {
        if (isIE && parseInt($.browser.version) < 8) {
            $(this).css('margin-top', (460 - $(this).height()) / 2);
        }
        $(this).fadeIn(400);
    });

    var $leftArrow = $('.gallery-list .arrow-left-product-details');
    var $rightArrow = $('.gallery-list .arrow-right-product-details');
    var $galleryItem = $('.wrapper-list .product-details');
    /*
    $('li:not(li.active-img)', $galleryItem).animate({ 'opacity': 0.6}, 50);
    if (isIE) {
    $('li:not(li.active-img) img',$galleryItem).addClass('opacity');
    }*/
    var $gallery = $('.product-details');
    /*
    $('li', $galleryItem).hover(
    function(){
    $(this).stop(true, true);
    $(this).animate({ 'opacity': 1}, 50);
    if (isIE) {
    $('img',this).addClass('opacity-none');
    $('img',this).removeClass('opacity');
    } 	
    },
    function(){
    if ($(this).hasClass('active-img')) { return; }
    if (isIE) {
    $('img',this).addClass('opacity');
    $('img',this).removeClass('opacity-none');
    } 			
    $(this).animate({ 'opacity': 0.6}, 50);			
    });
    */


    var counter = 0;
    var isMoving = false;

    if ($('li', $galleryItem).length < 4) {
        $('div.center-align-arrows').hide();
        return;
    }

    $leftArrow.click(function () {
        if ($(this).hasClass('disabled')) { return; }
        if (isMoving) { return; }
        counter--;
        isMoving = true;
        $gallery.animate({ 'top': -counter * 150 }, 500, function () { isMoving = false; });
        checkArrows();

    });
    $rightArrow.click(function () {
        if ($(this).hasClass('disabled')) { return; }
        if (isMoving) { return; }
        isMoving = true;
        counter++;
        $gallery.animate({ 'top': -counter * 150 }, 500, function () { isMoving = false; });
        checkArrows();
    });

    function checkArrows() {
        if (counter == 0) {
            $leftArrow.addClass('disabled');
        } else {
            $leftArrow.removeClass('disabled');
        }
        if (counter == $('li', $galleryItem).length - 3) {
            $rightArrow.addClass('disabled');
        } else {
            $rightArrow.removeClass('disabled');
        }
    }
    checkArrows();
}

function changeImage(url, obj) {
    $galleryItem = $('.wrapper-list .product-details ');
    if ($(obj).parent().hasClass('active-img')) { return; }
    //$('li.active-img', $galleryItem).animate({ 'opacity': 0.6}, 50);
    $('li', $galleryItem).removeClass('active-img');
    $(obj).parent().addClass('active-img');
    var $img = $('#productImage');
    $img.fadeOut(400, function () {
        $(this).attr('src', url);
    });
}

function popupBlock() {
    $('div.hidden-block').slideDown(500);
    $('div.content-text-block p.popout').hide();
}

function pageCarouselFx() {
    var $carouselHolderPage = $('div.page-image-holder ul.solutions-featurette ');
    var lastLiWidth = $('li:last-child', $carouselHolderPage).addClass('last').width();
    $('li.last span.popup-block-arrow', $carouselHolderPage).css({ 'left': 'auto', 'right': lastLiWidth / 2 + 8 });

    var pageItem = $('li', $carouselHolderPage).index($('li.active-ref')) + 1;

    $('li:nth-child(' + pageItem + ')', $carouselHolderPage).addClass('active-block');
    $('li:not(.active-block) div.popup', $carouselHolderPage).hide();

    $('li.active-block a', $carouselHolderPage).addClass('fixed-action').fadeIn(250);
    $('li.active-block a', $carouselHolderPage).parent().addClass('shown');
}

function carouselPopup() {

    var $carouselHolder = $('div.home-image-holder ul.solutions-featurette');

    var itemsNumber = $('li', $carouselHolder).length;
    var lastLiW = $('li:last-child', $carouselHolder).addClass('last').width();
    $('li.last span.popup-block-arrow', $carouselHolder).css({ 'left': 'auto', 'right': lastLiW / 2 - 5 });

    var currentItem = 1;

    var timeoutRun = false;
    var timeoutID = 0;

    timeoutID = setTimeout(nextItem, 500);

    $('li:not(.first)', $carouselHolder).hover(
		function () {
		    clearTimeout(timeoutID);
		    var $popupItem = $('div.popup', this);
		    if ($('a.active-state', this).parent().hasClass('shown')) {
		        $popupItem.stop(true, true).fadeIn(0);
		    } else {
		        $('li:nth-child(' + currentItem + ') div.popup', $carouselHolder).stop(true, true).fadeOut(0);
		        $('li:nth-child(' + currentItem + ') a.active-state', $carouselHolder).stop(true, true).fadeOut(0); //.parent().removeClass('shown');
		        $popupItem.stop(true, true).fadeIn(500);
		        $('a.active-state', this).stop(true, true).fadeIn(500);
		    }
		},
		function () {
		    var $popupItem = $('div.popup', this);
		    $popupItem.fadeOut(0);
		    $('a.active-state', this).fadeOut(0);
		    timeoutID = setTimeout(nextItem, homeCarouselAutoRotate * 500);
		});

    function nextItem() {

        $('li:nth-child(' + currentItem + ') div.popup', $carouselHolder).fadeOut(100);
        $('li:nth-child(' + currentItem + ') a.active-state', $carouselHolder).fadeOut(100).parent().removeClass('shown');
        currentItem++;
        if (currentItem == itemsNumber + 1) { currentItem = 2; }

        $('li:nth-child(' + currentItem + ') div.popup', $carouselHolder).fadeIn(150);
        $('li:nth-child(' + currentItem + ') a.active-state', $carouselHolder).fadeIn(150).parent().addClass('shown');

        timeoutID = setTimeout(nextItem, homeCarouselAutoRotate * 1000);
    }
}

function carouselPopupIE() {

    var $carouselHolder = $('div.home-image-holder ul.solutions-featurette');

    var itemsNumber = $('li', $carouselHolder).length;
    var lastLiW = $('li:last-child', $carouselHolder).addClass('last').width();
    $('li.last span.popup-block-arrow', $carouselHolder).css({ 'left': 'auto', 'right': lastLiW / 2 - 5 });

    var currentItem = 1;

    var timeoutRun = false;
    var timeoutID = 0;

    timeoutID = setTimeout(nextItem, 500);

    $('li:not(.first)', $carouselHolder).hover(
		function () {
		    clearTimeout(timeoutID);
		    var $popupItem = $('div.popup', this);
		    if ($('a.active-state', this).parent().hasClass('shown')) {
		        $popupItem.stop(true, true).show();
		    } else {
		        $('li:nth-child(' + currentItem + ') div.popup', $carouselHolder).stop(true, true).hide();
		        $('li:nth-child(' + currentItem + ') a.active-state', $carouselHolder).stop(true, true).hide(); //.parent().removeClass('shown');
		        $popupItem.stop(true, true).show();
		        $('a.active-state', this).stop(true, true).show();
		    }
		},
		function () {
		    var $popupItem = $('div.popup', this);
		    $popupItem.hide();
		    $('a.active-state', this).hide();
		    timeoutID = setTimeout(nextItem, homeCarouselAutoRotate * 500);
		});

    function nextItem() {

        $('li:nth-child(' + currentItem + ') div.popup', $carouselHolder).hide();
        $('li:nth-child(' + currentItem + ') a.active-state', $carouselHolder).hide().parent().removeClass('shown');
        currentItem++;
        if (currentItem == itemsNumber + 1) { currentItem = 2; }

        $('li:nth-child(' + currentItem + ') div.popup', $carouselHolder).show();
        $('li:nth-child(' + currentItem + ') a.active-state', $carouselHolder).show().parent().addClass('shown');

        timeoutID = setTimeout(nextItem, homeCarouselAutoRotate * 1000);
    }
}


function siteMapFix(colAmount) {
    var colAmount = colAmount ? colAmount : 6;
    $('div.site-map ul.level1-nav li.level1-nav-li:nth-child(' + String(colAmount) + 'n)').after('<li class="clear"></li>');
}

function indexSuccess() {
    document.location.href = "index-success.html";
}
function indexFail() {
    document.location.href = "index-fail.html";
}
function dropUpNav() {
    $('ul.page-nav li ul.sublevel').slideDown();
}
