﻿/*
Author 



*/

jQuery.fn.aPosition = function () {
    thisLeft = this.offset().left;
    thisTop = this.offset().top;
    thisParent = this.parent();
    parentLeft = thisParent.offset().left;
    parentTop = thisParent.offset().top;
    return {
        left: thisLeft - parentLeft,
        top: thisTop - parentTop
    }
}

jQuery.fn.center = function () {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}
 

function getBrowserWindowSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    return { width: myWidth, height: myHeight };
}

function num(a, b) { return (a - b); }

function showManager(obj, country) {
    var i, x = [], y = [];
    var mc = $('#image-map').position();
    //alert(mc.top);
    var c = $(obj).attr('coords').split(',');
    for (i = 0; i < c.length; i++) {
        x.push(c[i++]);
        y.push(c[i]);
    }
    var t = y.sort(num)[0];
    var l = x.sort(num)[0];
    //alert('top = ' + t + ', left = ' + l);

    t = Math.round(parseFloat( t) + parseFloat( mc.top))- 120;
    l = Math.round(parseFloat(l) + parseFloat(mc.left)) - 30;


    if (!$('.manager-tooltip').hasClass("manager-tooltip-visible")) {

        $.get('/data?m=5&r=' + country + '&l=' + locale, function (data) {
            $('.tooltip-body').html(data);
            $('.manager-tooltip').css("top", t + "px");
            $('.manager-tooltip').css("left", l + "px");
            $('.manager-tooltip').addClass("manager-tooltip-visible");


        });
    
    
    
    }

    





}


function hideManager(country) {
   // $('.manager-tooltip').css("display", "none");

    $('.manager-tooltip').removeClass("manager-tooltip-visible");

}


function fixBackgroundPosition() {

    var size = getBrowserWindowSize();

    var h = size.height-10;
    if(h<850)
    {
        h = 850;
    }


    $('body').css('min-height',h);
    $('.active-wallpaper').css('min-height', h + 20);
    
}


function wallpaperSlider() {
    var sl = '';
    var cs = 1;
    $('.wallpaper').each(function () {
        if (cs == 1) {
            $(this).css('display','block');
            sl = sl + '<li class="w-slider-item w-slider-item-active" onclick="switchWallpaper(' + cs + ')" ></li>'
        }
        else {
            sl = sl + '<li class="w-slider-item" onclick="switchWallpaper(' + cs + ')"></li>'
        }
        cs++;
    });
    $('.w-slider-item-list').css('width',cs * 17);
    $('.w-slider-item-list').html(sl);
    setInterval('switchWallpaper()', 20000);
}

function switchWallpaper(index) { 
    var total = 0;
    $('.wallpaper').each(function () { total++; });
    if (index == undefined) {
        if (wAuto) {

            var idx = 0;
            var cs = 1;
            var next = 0;
            $('.wallpaper').each(function () {

                if ($(this).hasClass('active-wallpaper')) {
                    idx = cs;
                }
                cs++;
            });

            if (idx == total) {
                next = 1;
            }
            else {
                next = idx + 1;
            }
            $('#w-' + idx).fadeOut(1000, function () {
                $('#w-' + idx).removeClass('active-wallpaper');
                $('#w-' + next).addClass('active-wallpaper');
                $('#w-' + next).fadeIn('slow');
                wIndex = next;
                $('.w-slider-item').removeClass('w-slider-item-active');
                var lp = 1;
                $('.w-slider-item').each(function () {

                    if (lp == next) {
                        $(this).addClass('w-slider-item-active');
                    }

                    lp++;
                });
            });
        }

    }
    else {
        wAuto = false;
        setTimeout('setWallpaperMode()', 20000);
        var midx = 0;
        var mc = 1;
        $('.w-slider-item').each(function () {

            if ($(this).hasClass('w-slider-item-active')) {
                midx = mc;
            }
            mc++;
        });

        if (index != midx) {
            $('#w-' + midx).fadeOut(1000, function () {
                $('#w-' + midx).removeClass('active-wallpaper');
                $('#w-' + index).addClass('active-wallpaper');
                $('#w-' + index).fadeIn('slow');
                wIndex = index;
                $('.w-slider-item').removeClass('w-slider-item-active');
                var lp = 1;
                $('.w-slider-item').each(function () {

                    if (lp == index) {
                        $(this).addClass('w-slider-item-active');
                    }

                    lp++;
                });
            });
        }
    }
}

function setWallpaperMode() {
    wAuto = true;
}


function fixDelay() {

    fixSubMenuPosition('.m-lighting');
    fixSubMenuPosition('.m-electronics');
    fixSubMenuPosition('.m-sha');

}

function fixSubMenuPosition(path) {
    var x = $('#container').aPosition().left;
    var lx = $('.m-lighting').aPosition().left;
    var cx = $(path).aPosition().left;
    var shift = cx - x + 20;
    shift = cx - lx;
    var pos = '-' + shift + 'px'
    $('ul', path).css('margin-left', pos);
}

function recalculateSearchTotals() {
    $('.search-total-qty').html($('.search-results-entry').length);
}

function showImage(obj, path) {
    $('.gallery-image').removeClass("gallery-image-selected");
    $(obj).addClass("gallery-image-selected");
    $('#product-image-big').attr('src', path);
}

function hideLanguageSelector() {

    $('#language-list-box').css('display', 'none');

}

function hideLoginPanel() {

    $('#login-panel').css('display', 'none');

}

function switchLanguage(code) {

    var url = location.href;
    var pattern = /[a-z]{2}\-[a-z]{2}/;
    var cl = url.match(pattern)

    if (cl != null && cl.length == 1) {

        url = url.replace(cl[0], code);
        location.href = url;
    }

}

function clickHome(locale) {
    alert('http://localhost:35594/' + locale);


    window.location.href = 'http://localhost:35594/' + locale;

}


function hideLoginLanguageList() {

    $('#login-language').css('display', 'none');

}

function switchLoginLanguage(obj, locale) {
    var c = $('#language').attr('value');
    if (locale != c) {        
        $('language-title',obj).html();
        var cc = $(obj).clone();
        $('.language-title', cc).html($('.language-title', cc).html() + ' ↓');
        $('.current-login-language').html($(cc).html());
        $('#language').attr('value', locale);
        $('#login-language').css('display', 'none');
    }
}

function initSlider() {
    $("#scroll-bar-wrapper").html('<div id="scroll-bar"></div>');
    var se = false;
    //scrollpane parts
    var scrollPane = $("#scroll-pane"), scrollContent = $("#scroll-content");
    it = $(".scroll-item", $("#scroll-content"));
    var scw = it.length * (parseInt(it.css("width")) + 60);
    var spw = parseInt(scrollPane.css("width"))
    scrollContent.css("width",scw );

    if (scw < spw) {

        scrollContent.css("margin-left", spw / 2 - scw / 2);
        se = true;
    }
    else {
        scrollContent.css("margin-left", 0);
    }
    var scrollbar = $("#scroll-bar").slider({
        disabled:se,
        slide: function (event, ui) {
            if (scrollContent.width() > scrollPane.width()) {
                scrollContent.css("margin-left", Math.round(
						ui.value / 100 * (scrollPane.width() - scrollContent.width())
					) + "px");
            } else {
                scrollContent.css("margin-left", 0);
            }
        }
    }); ;


    if (scrollContent.width() > scrollPane.width()) {

        scrollPane.mousewheel(function (event, delta) {
            var value = scrollbar.slider('option', 'value');

            if (delta < 0) {
                value += 2;

            }
            else if (delta > 0) {
                value -= 2;
            }

            if (value > 100) {
                value = 100;
            }
            if (value < 0) {
                value = 0;
            }

            if (scrollContent.width() > scrollPane.width()) {
                scrollContent.css("margin-left", Math.round(
		            value / 100 * (scrollPane.width() - scrollContent.width())
	            ) + "px");
            } else {
                //scrollContent.css("margin-left", 0);
            }
            value = Math.max(0, Math.min(100, value));
            scrollbar.slider('option', 'value', value);
            event.preventDefault();
        });
    }
   

    var handleHelper = scrollbar.find(".ui-slider-handle")
		.mousedown(function () {
		    $('.ui-slider-handle').removeClass('ui-state-focus');
		})
		.mouseup(function () {
		    $('.ui-slider-handle').removeClass('ui-state-focus');
		}).wrap("<div class='ui-handle-helper-parent'></div>").parent();  
    scrollPane.css("overflow", "hidden");

    function sizeScrollbar() {
        var remainder = scrollContent.width() - scrollPane.width();
        var proportion = remainder / scrollContent.width();
        var handleSize = scrollPane.width() - (proportion * scrollPane.width());

        if ((scrollContent.width() - scrollPane.width()) > 0) {
            handleSize = 67;

            scrollbar.find(".ui-slider-handle").css({
                "background-image": "url('/content/images/scroll-bar-handle.png')"
            });
                  

            scrollbar.find(".ui-slider-handle").hover(function() {
                    $(this).css({
                        "background-image": "url('/content/images/scroll-bar-handle-hover.png')"
                    });
                }, function() {
                    $(this).css({
                        "background-image": "url('/content/images/scroll-bar-handle.png')"
                    });
                });
        }
        else {
            handleSize = 423;

            scrollbar.find(".ui-slider-handle").css({
                "background-image": "url('/content/images/scroll-bar-long.png')"
            });
                        
            scrollbar.find(".ui-slider-handle").hover(function() {
                    $(this).css({
                        "background-image": "url('/content/images/scroll-bar-long.png')"
                    });
                }, function() {
                    $(this).css({
                        "background-image": "url('/content/images/scroll-bar-long.png')"
                    });
                });
        }







        scrollbar.find(".ui-slider-handle").css({
            width: handleSize,
            "margin-left": -handleSize / 2
        });
        handleHelper.width(scrollbar.width() - handleSize);
    }



    function resetValue() {
        var remainder = scrollPane.width() - scrollContent.width();
        var leftVal = scrollContent.css("margin-left") === "auto" ? 0 :
				parseInt(scrollContent.css("margin-left"));
        var percentage = Math.round(leftVal / remainder * 100);
        scrollbar.slider("value", percentage);
    }
    function reflowContent() {
        var showing = scrollContent.width() + parseInt(scrollContent.css("margin-left"), 10);
        var gap = scrollPane.width() - showing;
        if (gap > 0) {
            scrollContent.css("margin-left", parseInt(scrollContent.css("margin-left"), 10) + gap);
        }
    }
    $(window).resize(function () {
        resetValue();
        sizeScrollbar();
        reflowContent();
    });
    setTimeout(sizeScrollbar, 10);

}


function toggleSectionFilter(obj, category) {

    if (!$(obj).hasClass('section-filter-active')) {
        $('.section-filter-value').removeClass('section-filter-active');
        $(obj).addClass('section-filter-active');
        if (category == 'new') {
            newOnly = 1;
        } else {
            newOnly = 0;
        }
        var fl = category;
        var mode = 1;
        if (newOnly == 0) {
            mode = 2;
        }
        else {
            mode = 1;
        }
        $.get('/data?m=' + mode + '&n=' + newOnly + '&c=' + fl + '&l=' + locale + '&s=' + section, function (data) {
            $('#scroll-content').html(data);
            initSlider();
        });
    }
}


function submitSearch(locale) {

    var query = $('#searchbar').attr('value');
    query = $.base64Encode(query);

    query = query.replace(/=/g, '_');

    location.href = '/' + locale + '/search/' + query;

}

function submitOnEnter(locale, e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
        submitSearch(locale);
        return false;
    }
    else
        return true;
}


function switchContactTab(obj,tab) {


    if (!$(obj).hasClass('contact-tab-header-active')) {

        $('.contact-tab-header').removeClass('contact-tab-header-active');
        $(obj).addClass('contact-tab-header-active');
        $('.contact-tab').removeClass('contact-tab-active');
        var target = "";
        switch (tab) {

            case "general":
                target = "contact-tab-general";
                break;

            case "export":
                target = "contact-tab-export";
                break;



            case "baltic":
                target = "contact-tab-baltic";
                break;

            case "development":
                target = "contact-tab-development";
                break;

            case "marketing":
                target = "contact-tab-marketing";
                break;
        

            case "support":
                target = "contact-tab-support";
                break;



        }
        $('#' + target).addClass('contact-tab-active');
    
    
     }

}


function toggleFilter(obj,category) {
    if (category == 'new') {
        if ($(obj).hasClass('category-filter-active')) {
            $(obj).removeClass('category-filter-active');
            newOnly = 0;
        }
        else {
            $(obj).addClass('category-filter-active');
            newOnly = 1;
        }
    }
    else {
        if ($(obj).hasClass('category-filter-active')) {
           $(obj).removeClass('category-filter-active');
            var idx = -1;
            for (var i = 0; i < filters.length; i++) {
                if (filters[i] == category) {
                    idx = i;
                }
            }
            if (idx != -1) {
                filters.splice(idx, 1);
            }
        }
        else {
            $(obj).addClass('category-filter-active');
            filters.push(category);
        }
    }
    
    var fl = '';
    for (var i = 0; i < filters.length; i++) {

        if (i != 0) {
            fl = fl + ';';
        }
        fl = fl + filters[i];
     }
     $.get('/data?m=3&n=' + newOnly + '&c=' + fl + '&l=' + locale + '&s=' + section +'&sc='+sc, function (data) {
         $('#scroll-content').html(data);
         initSlider();
     });
}

function toggleSearchFilter(obj, category) {


    if ($(obj).hasClass('search-filter-active')) {
        $(obj).removeClass('search-filter-active');

        if (category == 'new') {
            newOnly = 0;
        }
        else {

            var idx = -1;
            for (var i = 0; i < filters.length; i++) {
                if (filters[i] == category) {
                    idx = i;
                }
            }
            if (idx != -1) {
                filters.splice(idx, 1);
            } 
        }
    }
    else {
        $(obj).addClass('search-filter-active');

        if (category == 'new') {
            newOnly = 1;
        }
        else {

            filters.push(category);       
        }
    }
    var fl = '';
    for (var i = 0; i < filters.length; i++) {

        if (i != 0) {
            fl = fl + ';';
        }

        fl = fl + filters[i];
    }



    $.get('/data?m=4&n='+newOnly+'&c=' + fl + '&l=' + locale + '&s=&q=' + query, function (data) {
        $('.search-results').html(data);
        recalculateSearchTotals();
    });


}

function rightClick(d) {
    var scrollPane = $("#scroll-pane"), scrollContent = $("#scroll-content");
    var s = $('#scroll-bar').slider();
    var cp = s.slider('value');

    if (parseInt(scrollPane.css("width")) < parseInt(scrollContent.css("width"))) {

        if ((cp + d) < 100) {
            cp = cp + d;
        }
        else {
            cp = 100;
        }
        s.slider('value', cp);
        s.slider('option', 'change');
        if (scrollContent.width() > scrollPane.width()) {
            scrollContent.css("margin-left", Math.round(
		    cp / 100 * (scrollPane.width() - scrollContent.width())
	    ) + "px");
        } else {
            scrollContent.css("margin-left", 0);
        }
    }

    if (scroll) {
        setTimeout("rightClick(1)", 200);
    }

}

function leftClick(d) {
    var scrollPane = $("#scroll-pane"), scrollContent = $("#scroll-content");
    var s = $('#scroll-bar').slider();
    var cp = s.slider('value');


    if (parseInt(scrollPane.css("width")) < parseInt(scrollContent.css("width"))) {

        if ((cp - d) > 0) {
            cp = cp - d;
        }
        else {
            cp = 0;

        }
        s.slider('value', cp);
        s.slider('option', 'change');

        if (scrollContent.width() > scrollPane.width()) {
            scrollContent.css("margin-left", Math.round(
		    cp / 100 * (scrollPane.width() - scrollContent.width())
	    ) + "px");
        } else {
            scrollContent.css("margin-left", 0);
        }
    }

    if (scroll) {
        setTimeout("leftClick(1)", 200);
    }

}




function leftStartScroll(e) {
    if (!scroll) {
        scroll = true;
        if (scroll) {
            setTimeout("leftClick(1)", 200);
        } 
    }

}

function rightStartScroll(e) {

    if (!scroll) {
        scroll = true;
        if (scroll) {
            setTimeout("rightClick(1)", 200);
        } 
    }
}


function leftEndScroll() {
    scroll = false;
}

function rightEndScroll() {
    scroll = false;
}
