var _showLogin = 0;
$(window).load(function() {
    if($('#slider') != undefined){
    $('#slider').nivoSlider({
        effect: 'boxRain',
        animSpeed: 1000, // Slide transition speed
        pauseTime: 6000, // How long each slide will show
        directionNav: false, // Next & Prev navigation
        captionOpacity: 1 // Universal caption opacity
    })
    };

    if($('#carousel') != undefined){
    $('#carousel').jcarousel({
        wrap: 'circular',
        scroll: 1,
        auto: 4
    })
    };

    if($('#mini-cart-open') != undefined){
    $('#mini-cart-open').click(function() {
        showCart();
        if($('#cart') != undefined)
        $('#cart').fadeIn();
    })
    };
    
    if($('#mini-cart-close') != undefined){
    $('#mini-cart-close').click(function() {

        closeCart();

    })
    };
    
    if($('#loginOpen') != undefined){
    $('#loginOpen').click(function() {
        $(document).bind('click', function(e) {
            if (_showLogin == 1) {
                var $clicked = $(e.target);
                if (!($clicked.is('#login') || $clicked.parents().is('#login'))) {
                    closeLogin();
                }
            }
            else {
                _showLogin = 1;
            }
        }
        );
        $('#login').fadeIn();
    })
    };
    
    if($('#login-close') != undefined){
    $('#login-close').click(function() {
        closeLogin();
    })
    };

    // This adds onclick to every a.add-to-cart element on page.
    // In case the product is not added to cart after clicking buy button,
    // we want the tooltip to NOT SHOW - hence the _doNotShowToolTipAddedToCart
    if($('a.add-to-cart') != undefined){
    $('a.add-to-cart').click(function() {
        if (_doNotShowToolTipAddedToCart == true) return false;
        addedToCartTooltip($(this), _itemAddedToCartMessage);
    })
    };
});

function closeLogin() 
{
    $(document).unbind('click');
    $('#login').fadeOut();
    _showLogin = 0;
}


//General tooltip function.
//Tooltip is displayed at "object" (which is constructed as $(elementID))
function tooltip(object, title, text) {
    object.qtip({
        content: {
            text: text,
            title: {
                text: title
            }
        },
        show: {
            when: 'click',
            ready: true
        },
        hide: { when: { event: 'unfocus'} },
        position: {
            corner: {
                target: 'bottomMiddle',
                tooltip: 'topMiddle'
            },
            adjust: {
                screen: true, // Keep the tooltip on-screen at all times
                y: -3
            }
        },
        style: {
            tip: {
                corner: 'topMiddle',
                size: { x: 18, y: 10 }
            },
            background: '#eeede1 url(img/bubble.png) top repeat-x',
            fontSize: '11px',
            padding: '5px 8px',
            border: {
                width: 1,
                color: '#e5e5db'
            },
            title: {
                color: '#c3112b',
                background: '#fff',
                padding: '5px 8px 0px'
            },
            name: 'light' // Use the default light style
            //width: 200 width not specified => fit content
        }
    });
}


//General tooltip function with red background and big font size.
//Tooltip is displayed at "object" (which is constructed from objectId parameter (element attribute "id")
function tooltipRed(objectId, title, text) {

    var object = $(getElementBySubstringId(objectId));

    object.qtip({
        content: {
            text: text
        },
        show: {
            when: { event: '' }, //event is not used here - we just use ready=true, which shows qtip one time right when it's created
            ready: true
        },
        hide: {     //using this code to automaticaly hide after certain time passed didn't work in 
            //any of the VERY MANY combinations I've tried. So i call qtip("hide") through setTimeout()
            //and this "hide: {}" section is used for additional functionality of tooltip disappearing when clicking the mouse
            when: { event: 'unfocus' }
        },
        position: {
            corner: {
                target: 'topMiddle',
                tooltip: 'bottomMiddle'
            },
            adjust: {
                screen: true, // Keep the tooltip on-screen at all times
                y: -3
            }
        },
        style: {
            tip: {
                corner: 'topMiddle',
                size: { x: 18, y: 10 }
            },
            background: '#c3112b', 
            color: '#fff',
            fontSize: '24px',
            fontWeight: 'bold',
            textAlign: 'center',
            padding: '12px',
            border: {
                width: 1,
                color: '#c3112b'
            },
            title: {
                color: '#333',
                background: '#fff',
                padding: '10px 10px 0px 10px'
            },
            name: 'light', // Use the default light style
            width: 250
        }
    });

    setTimeout('hideTooltipRed("' + objectId +'")', 2000);
}

function hideTooltipRed(objectId) {
    var object = $(getElementBySubstringId(objectId));
    object.qtip("hide");
}


function addedToCartTooltip(object, text) {

    //var object = $(document.getElementById("mini-cart-open"));
    var bodyObject = $(document.body);

    object.qtip({
        content: {
            text: text
        },
        show: {
            when: { event: '' }, //event is not used here - we just use ready=true, which shows qtip one time right when it's created
            ready: true
        },
        hide: {     //using this code to automaticaly hide after certain time passed didn't work in 
            //any of the VERY MANY combinations I've tried. So i call qtip("hide") through setTimeout()
            //and this "hide: {}" section is used for additional functionality of tooltip disappearing when clicking the mouse
            when: { event: 'unfocus' }
        },
        position: {
            corner: {
                target: 'topMiddle',
                tooltip: 'bottomMiddle'
            },
            adjust: {
                screen: true, // Keep the tooltip on-screen at all times
                y: -3
            }
        },
        style: {
            tip: {
                corner: 'topMiddle',
                size: { x: 18, y: 10 }
            },
            background: '#94C352',
            color: '#fff',
            fontSize: '24px',
            fontWeight: 'bold',
            textAlign: 'center',
            padding: '40px',
            border: {
                width: 1,
                radius: 5,
                color: '#94C352'
            },
            title: {
                color: '#333',
                background: '#fff',
                padding: '10px 10px 0px 10px'
            },
            name: 'light', // Use the default light style
            width: 300
        }
    });

    addToCartTooltipHelper = object;
    setTimeout('hideAddedToCartTooltip()', 3000);
}

//used to fix case of qtip sometimes not disappearing when using $('a.add-to-cart').qtip("hide");
//this variable is therefore meant for use with addedToCartTooltip() and hideAddedToCartTooltip() functions
var addToCartTooltipHelper;
function hideAddedToCartTooltip() {
    //var object = $(document.getElementById("mini-cart-open"));
    //object.qtip("hide");

    //$('a.add-to-cart').qtip("hide");

    addToCartTooltipHelper.qtip("hide");
}



//gets element with id containing string passed as param "id"
//second param "elementType" can be used to narrow down the number
//of searched elements if we know what tag we are seraching for
function getElementBySubstringId(id, elementType) {

    if (elementType == null) elementType = "*"; //if not specified, we search all elements

    var allElements = document.getElementsByTagName(elementType);

    for (var i = 0, il = allElements.length; i < il; i++) {

        element = allElements[i];
        var test = element.getAttribute("id");

        if (test == null) continue;

        if (test.indexOf(id) != -1) {
            return element;

        }
    }
}

function productFilterKeyPress(e) {
    e = e || window.event;
    if (e.keyCode == 13) {
        e.cancelBubble = true;
        e.returnValue = false;
        if (e.preventDefault) e.preventDefault();
        if (e.stopPropagation) e.stopPropagation();
        try{
            var element = getElementBySubstringId("SelectProductsOK", "input");
            if(element == null) return;
            document.all(element.Id).click();
        } catch(exception){}
    }
}


