// JavaScript Document
var TemplatePath = '/templates/guest/';

$(document).ready(function (){

    $('#caption').each(function(){
        var spn = $(this);
        var spnText = spn.text();
        spn.html('');
        spn.html('<span class="over">' + spnText + '</span><span class="under">' + spnText + '</span>');
    });

    $('#introSlider').nivoSlider({
        effect:'sliceUpDownLeft',                   //Specify sets like: 'fold,fade,sliceDown'
        slices:8,
        animSpeed:700,                              //Slide transition speed
        pauseTime:8000,
        startSlide:0,                               //Set starting Slide (0 index)
        directionNav:false,                         //Next & Prev
        directionNavHide:false,                     //Only show on hover
        controlNav:true,                            //1,2,3...
        controlNavThumbs:true,                      //Use thumbnails for Control Nav
        controlNavThumbsSearch: '.jpg',             //Replace this with...
        controlNavThumbsReplace: '_thumb.jpg',      //...this in thumb Image src
        keyboardNav:false,                          //Use left & right arrows
        pauseOnHover:false,                         //Stop animation while hovering
        manualAdvance:false,                         //Force manual transitions
        captionOpacity:0                            //Universal caption opacity
    });

    $('#selectRegion ul li a').click(function () {
        var liObj = $(this).parent('li');
        var linkObj = $(this);
        if (liObj.hasClass('current')) {
            $('#selectRegion ul').click();
        } else {
            $('#selectRegion ul li.current').removeClass('current');
            liObj.addClass('current');
            $('#selectRegion ul').slideUp('slow', function () {
                window.location.href = linkObj.attr('href');
            });
        }
        return false;
    });

    $('#selectRegion ul').click(function() {
        var thisObj = $('#selectRegion ul');
        if (thisObj.hasClass('collapse')) {
            thisObj.slideUp('slow', function () {
                thisObj.show();
                thisObj.removeClass('collapse');
            });
        } else {
            thisObj.addClass('collapse');
            thisObj.hide();
            thisObj.slideDown('slow');
        }
    });

    $('#siteMap').colorbox({
        innerWidth:'90%',
        innerHeight:'90%',
        iframe:true,
        opacity: 0.80,
        overlayClose: false,
        scrolling: false,
        initialWidth: '150px',
        initialHeight: '100px',
        onComplete : function () { $('#cboxTitle').hide(); }
    });

    $('#request, #requestDisc, .requestDisc').colorbox({
        innerWidth:'800px',
        innerHeight:'600px',
        iframe:true,
        opacity: 0.80,
        overlayClose: false,
        scrolling: false,
        initialWidth: "150px",
        initialHeight: "100px",
        onComplete : function () { $('#cboxTitle').hide(); }
    });

    $('#colorpicker').colorbox({
        innerWidth:'710px',
        innerHeight:'770px',
        iframe:true,
        opacity: 0.80,
        overlayClose: false,
        scrolling: false,
        initialWidth: "150px",
        initialHeight: "100px",
        onComplete : function () { $('#cboxTitle').hide(); }
    });

    $('span.comments a, .addComment, #calculator, #basket, #checkOrderStatus').colorbox({
        innerWidth:'800px',
        innerHeight:'600px',
        iframe:true,
        opacity: 0.80,
        overlayClose: false,
        scrolling: false,
        initialWidth: "150px",
        initialHeight: "100px",
        onComplete : function () { $('#cboxTitle').hide(); }
    });
    
    $('a.projectMorePhoto, a.projectMorePhotoTitle').colorbox({
        innerWidth:'880px',
        innerHeight:'800px',
        iframe:true,
        opacity: 0.80,
        overlayClose: false,
        scrolling: false,
        initialWidth: "150px",
        initialHeight: "100px",
        onComplete : function () { $('#cboxTitle').hide(); }
    });

    $('a.projectMoreVideo, a.projectMoreVideoTitle').colorbox({
        innerWidth:'880px',
        innerHeight:'575px',
        iframe:true,
        opacity: 0.80,
        overlayClose: false,
        scrolling: false,
        initialWidth: "150px",
        initialHeight: "100px",
        onComplete : function () { $('#cboxTitle').hide(); }
    });


    $('#addQuestion').colorbox({
        innerWidth:'800px',
        innerHeight:'600px',
        iframe:true,
        opacity: 0.80,
        overlayClose: false,
        scrolling: false,
        initialWidth: "150px",
        initialHeight: "100px",
        onComplete : function () { $('#cboxTitle').hide(); }
    });

    $('a[rel="galleryPhoto"], .lonelyPhoto').colorbox({
        opacity: 0.80,
        overlayClose: false,
        scrolling: false,
        initialWidth: "150px",
        initialHeight: "100px"
    });

    $('.productPhoto').colorbox({
        opacity: 0.80,
        overlayClose: false,
        scrolling: false,
        initialWidth: "150px",
        initialHeight: "100px",
        onComplete : function () { $('#cboxTitle').hide(); }
    });


    $('table#catalogTbl td').hover(
        function () {
            $(this).addClass('highlight');
        },
        function () {
            $(this).removeClass('highlight');
        }
    );

    var addToBasketAnimation = new Image();
    addToBasketAnimation.src = TemplatePath + 'img/added-to-basket-animation.gif';

    $('.addToBasketBtn').click(function () {
        var myId = $(this).val();
        $.post(
            '/shopingcard/add_to_card.html',
            { product_id: myId },
            function (data) {
                alert('Товар добавлен в корзину');
                /*
                $('.item-' + myId).append('<div class="productAddedInCard"><img src="' + TemplatePath + 'img/added-to-basket-animation.gif" alt="Товар добавлен в корзину" /></div>');
                $('.item-' + myId + ' .productAddedInCard').fadeIn('1', function() {
                    $('#productsCount span').text(data.count);
                    $('#totalPrice span').text(data.price);
                });*/
                $('#productsCount span').text(data.count);
                $('#totalPrice span').text(data.price);
            },
            'json'
        );
    });

    $('.slideItem').click(function () {
        var id = $(this).attr('id');
        var number = id.substr(5);
    });
    
    $('#closeDisc').click(function () {
        $('#disc').remove();
        $.post(
            '/index.php',
            { closeDisc : 1 }
        );
        return false;
    });

    $('div.certs a').colorbox({
        opacity: 0.80,
        overlayClose: true,
        scrolling: false,
        initialWidth: "150px",
        initialHeight: "100px",
        onComplete : function () { $('#cboxTitle').hide(); }
    });

    $('#disc').animate({ marginRight: '0px' }, 1500);
});

var ratingCallback =
    function (value, link) {
        $.post(
            $(this.form).attr('action'),
            $(this.form).serialize(),
            function(data){
                if (data == '-1') {
                    alert('Вы уже голосовали!');
                } else if (data == '0') {
                    alert('Не удалось принять голос!');
                } else {
                    alert('Ваша оценка принята.\nСпасибо за участие!');
                    $('#rating-item-' + data + ' label').addClass('none');
                    $('.item-' + data).rating('readOnly',true);
                }
            }
        );
    };

function setCookie (name, value, expires, path, domain, secure)
{
    expires = 'Fri, 30 Nov 2035 00:00:00 +0300';
    path    = '/';
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires = " + expires : "") +
        ((path)    ? "; path    = " + path    : "") +
        ((domain)  ? "; domain  = " + domain  : "") +
        ((secure)  ? "; secure"               : "");
}
