﻿var animInterval = 4000;
var animSpeed = 900;
var delayInterval = 30000;

function computeMaxWidth() {
    var maxWidth = $('.pm_slider')[0].offsetWidth;

    var liN = $('.pm_slider ul li').length;
    var liWidth = parseInt((maxWidth) / liN);
    $('.pm_slider ul li').css('width', liWidth);
    $('.pm_slider ul li:not(:last)').css('border-right', 'solid 1px #dedede');

    $('.pm_slider ul li a').each(function() {
        var left = (liWidth / 2) - ($(this)[0].offsetWidth / 2);
        $(this).css('left', left + 'px');
    });
}

function bindClicks() {
    $('.pm_slider ul li a').click(function(e) {
        activateLink(this);
        return false;
    });
}

function activateLink(a) {

    if ($(a)[0].href) {
        moveIndicator($(a).parent(), $(a)[0].innerHTML);
    }
}

//function getObUpperLeft(obj) {
//    var curLeft = curTop = 0;
//    if (obj.offsetParent) {
//        curLeft = obj.offsetLeft
//        curTop = obj.offsetTop
////        alert("objName= " + obj.id + "  offsetLeft= " + obj.offsetLeft + "  offsetTop= " + obj.offsetTop + "  hasLayout= " + obj.currentStyle.hasLayout);
//        while (obj = obj.offsetParent) {
//            if (obj.id != "ProductsMenu") {
////                alert("objName= " + obj.id + "  offsetLeft= " + obj.offsetLeft + "  offsetTop= " + obj.offsetTop + "  hasLayout= " + obj.currentStyle.hasLayout);
//                curLeft += obj.offsetLeft
//                curTop += obj.offsetTop
//            }
//        }
//    }
//    return curLeft;
//}

function moveIndicator($li, text, skipAnimation) {
    var $activeLI = $('.pm_slider ul li.active')
    if ($activeLI.length > 0) $activeLI.removeClass('active');
    $li.addClass('active');

    //var baseLeft = getObUpperLeft($li[0]);
    var baseLeft = $li.offset().left - $li.parent().parent().parent().offset().left;

    var position = baseLeft + ($li[0].offsetWidth / 2) - 33;
    var $ind = $('.pm_indicator');
    var $next = $ind.children('.nextValue');
    var $old = $ind.children('.oldValue');

    var indLeft = $ind[0].offsetLeft;
    var startLeft = position - indLeft;
    var endLeft = indLeft - position;

    $next.html('<span>' + text + '</span>');
    if (!skipAnimation) {
        $ind.animate({ left: position }, animSpeed);
        $next.css({ left: startLeft }).animate({ left: 6 }, animSpeed);
        $old.css({ left: 6 }).animate({ left: endLeft }, animSpeed, function() {
            $old.html($next.html());
        });
    } else {
        $ind.css({ left: position });
        $old.html($next.html());
    }
}

function getFirstActiveLI() {
    var $allLI = $('.pm_slider ul li.active');
    for (var i = 0; i < $allLI.length; i++) {
        var $a = $($allLI[i]).children('a');
        if ($a.length > 0 && $a[0].href) {
            return $($allLI[i]);
            break;
        }
    }
}

$(document).ready(function() {
    computeMaxWidth();
    //bindClicks();
    $li = getFirstActiveLI();
    if ($li) {
        moveIndicator($li, $li.children('a').html(), true);
    } else {
        $('.pm_indicator').hide();
    }
});

function addCompareProduct(checkbox) {

    var $comparenow = jQuery('a', jQuery(checkbox).parent().parent());
    var cookie = $.cookie("CompareProducts");
    var value = "'" + checkbox.value + "'";
    if (checkbox.checked) {
        $comparenow.show();
        if (cookie == null) cookie = '';
        cookie += value;
    } else {
        $comparenow.hide();
        if (cookie != null && cookie != '') {
            var idx = cookie.indexOf(value, 0);
            if (idx == 0) {
                cookie = cookie.substring(value.length);
            } else if (idx > 0) {
                cookie = cookie.substring(0, idx) + cookie.substring(idx + value.length);
            }
        }
    }
    $.cookie("CompareProducts", cookie, { path: '/', expires: 7 });
}

