jQuery.noConflict();
jQuery(document).ready(function(){

        //boolean param to prevent extra clicks from injecting data into container
        overlayOpen = false;

        // apply style to hide containers before injecting data into them
        jQuery('#shoppingContainer').hide();
        jQuery('#favoritesContainer').hide();

        //Setup triggers on links
         //shopping cart link
        jQuery('#overlay_btn').click(function () {
            displayJQCartAnimation();
        });

        //add to favorites link
        jQuery('#favorite').click(function () {
            if (jQuery('#shoppingContainer').children().length > 0) {
                jQuery('#shoppingContainer').slideUp('fast',clearOverlay('#shoppingContainer'));
            }
            if (overlayOpen) return;

                jQuery('html, body').animate({scrollTop:0}, 'slow');

            wishlist_url = wishlist.add(skuSelect);
            if(wishlist_url){
                jQuery.ajax({
                         url:     wishlist_url,
                         success: function(result)
                         {
                            if (result == undefined) return;
                            if (typeof wait != 'undefined') wait.hide();

                            item_data = YAHOO.lang.JSON.parse(result);

                            if (!item_data.authenticated)
                            {
                                redirectTo('/wishlist');
                            } else {
                                overlayOpen = true;
                                jQuery('#favoritesContainer').empty();//empty content first
                                jQuery('#favoritesContainer').append(favoritesShoppingCartOverlay('favs', item_data));//adding content to the overlay
                                jQuery('#favoritesContainer').slideDown('fast'); //shows overlay
                                timer = setTimeout(function(){
                                    jQuery('#favoritesContainer').slideUp('fast',clearOverlay('#favoritesContainer'));//hide overlay 3 seconds after show
                                }, 5000);

                                //update wishlist header counter
                                document.getElementById('wishlist_counter').innerHTML = item_data.wishlist_count;

                                //set defaults back after adding to favorites
                                skuSelect.setDefaults();
                                skuSelect.selectQty();
                                skuSelect.selectSize();
                                showcase.switchColor(skuSelect.defaults['color']);
                            }
                         }
                    });
            }
            return false;
        });


});

function shoppingCartOverlayExceedsQty(qty_available) {
    return '<div style="overflow: hidden; width: 100%; height: 80px; background-color:#E6E0DA;">' +
        '<div>' +
            '<div class="bagDescription" style="font-weight: bold;">' +
                'We are sorry the quantity you selected exceeds the available quantity.' +
            '</div>' +
        '</div>' +
    '</div>';
}

function favoritesShoppingCartOverlay(type, item) {
    var constructOverlay = '';
        if(type=='cart'){
            utm = "";

        constructOverlay = '<div class="overlay_img"><img src="'+item_data.item_image+'" /></div>' +
            '<div class="prod_desc">'+item_data.item_name+'</div>' +
            '<div class="prod_cost">$'+item_data.item_price+'</div>' +
            '<div class="bag">' +
            '    <div class="bag_subtotal">Bag subtotal <span class="subtotal_cost">$'+item_data.subtotal+'</span></div>' +
            '    <div class="viewbag_btn"><a href="/shopping_bag"><img src="/images/redesign2010/overlays/view_my_bag_btn.gif" border="0"/></a></div>' +
            '    <div>'+utm+'</div>' +
            '</div>';
    }else if(type=='favs'){
    constructOverlay = '<div class="overlay_img"><img src="'+item_data.item_image+'" /></div>' +
        '<div class="prod_desc">'+item_data.item_name+'</div>' +
        '<div class="prod_cost">$'+item_data.item_price+'</div>' +
        '<div class="bag">' +
        '    <div class="bag_subtotal2"> added  to <span class="white"> Wish  List</span></div>' +
        '    <div class="viewbag_btn" style="padding-left: 13px"><a href="/wishlist"><img src="/images/redesign2010/overlays/wish_list_btn.jpg" border="0"/></a></div>' +
        '</div>';
    }

    return constructOverlay;
}

var user_wants_to_backorder = false;

function displayJQCartAnimation(){
    if (jQuery('#favoritesContainer').children().length > 0) {
        jQuery('#favoritesContainer').slideUp('fast',clearOverlay('#favoritesContainer'));
    }
    if (overlayOpen) return;

        jQuery('html, body').animate({scrollTop:0}, 'slow');

    validAddtoBag = validateQvSizeQty (skuSelect);
    if(validAddtoBag){
        if (!skuSelect.checkSizeQty()) return;
        sku_id = skuSelect.color_size[skuSelect.swatch_pad][skuSelect.color_id][skuSelect.size_id]['sku_id'];
        if ((skuSelect.backorderable[sku_id]) && !skuSelect.checkAvailable()) return;

        postData = '?i=' + skuSelect.item_id + '&c=' + skuSelect.color_id + '&s=' + skuSelect.size_id + '&q=' + skuSelect.qty;
        postData += skuSelect.addAnotherPost();


        jQuery.ajax({
                url:    '/shopping_bag/ajaxadd'
                          + postData,
                 success: function(result) {
                            if (typeof weAreInCart != "undefined" && weAreInCart == true) {
                                window.location.reload();
                                return;
                            }

                            if (result == undefined) return;
                            if (typeof wait != 'undefined') wait.hide();

                            item_data = YAHOO.lang.JSON.parse(result);

                            // Sitecatalyst stuff
                            if(typeof(item_data.sitecatalyst)!='undefined')
                                eval(item_data.sitecatalyst);
                            
                            // Certona stuff (see catalog.js)
                            updateCertonaTags(skuSelect.item_id,skuSelect.color_id,'add');
                            
                            overlayOpen = true;
                            jQuery('#shoppingContainer').empty();//empty content first

                            //define the content
                            var content = null;
                            if(typeof(item_data.exceeds_stock) != 'undefined'){
                                //content = shoppingCartOverlayExceedsQty(item_data.qty_available);
                                var alertQvDiv = document.getElementById('qv_errors');
                                var customError = document.getElementById('custom-alert');
                               customError.innerHTML = "We are sorry - your selection exceeds the quantity available.";
                               alertQvDiv.style.display = 'block';
                               content = "";
                               //return;
                            }
                            else{
                                content = favoritesShoppingCartOverlay('cart', item_data);
                            }
                            jQuery('#shoppingContainer').append(content);//adding content to the overlay
                            jQuery('#shoppingContainer').slideDown('fast'); //shows overlay
                            timer = setTimeout(function(){
                                jQuery('#shoppingContainer').slideUp('fast',clearOverlay('#shoppingContainer'));//hide overlay 3 seconds after show
                            }, 5000);

                            //update header counter
                            document.getElementById('shopping_bag_counter').innerHTML = item_data.item_count;
                            //update subtotal
                            var hdr_subtotal = document.getElementById('hdr_subtotal');
                            if(typeof(hdr_subtotal)!='undefined' && hdr_subtotal!=null && typeof(item_data.subtotal) != 'undefined')
                                hdr_subtotal.innerHTML = '$' + item_data.subtotal;

                            //set defaults back after adding to to bag
                            skuSelect.setDefaults();
                            skuSelect.selectQty();
                            skuSelect.selectSize();
                            showcase.switchColor(skuSelect.defaults['color']);


                          }

            });
    }
    return false;
}

function clearOverlay(container){
    clearTimeout(timer); //clear all setTimeouts
    overlayOpen = false;
}

function displayJQCartAnimationforGC(item_data){
    overlayOpen = true;
    jQuery('#shoppingContainer').empty();//empty content first

    //define the content
    var content = null;
    if(typeof(item_data.exceeds_stock) != 'undefined')
        content = shoppingCartOverlayExceedsQty(item_data.qty_available);
    else
        content = favoritesShoppingCartOverlay('cart', item_data);

    jQuery('#shoppingContainer').append(content);//adding content to the overlay
    jQuery('#shoppingContainer').slideDown('fast'); //shows overlay
    timer = setTimeout(function(){
        jQuery('#shoppingContainer').slideUp('fast',clearOverlay('#shoppingContainer'));//hide overlay 3 seconds after show
    }, 5000);
}

function fav_add_from_cart(id_line)
{
    var callback = {
            success: function(o)
                     {
                            item_data = YAHOO.lang.JSON.parse(o.responseText);

                            if (!item_data.authenticated)
                            {
                                redirectTo('/wishlist');
                            }
                            else
                            {
                                redirectTo('/shopping_bag');
                            }
                            //YAHOO.util.Connect.asyncRequest('POST', '/favorites/remove/i/'+o.argument[0], {success: function(r) {r.argument.parentNode.removeChild(r.argument);}, argument: o.argument[1]});
                     }
    };
    YAHOO.util.Connect.asyncRequest('POST', '/cart/ajaxFavorites', callback, 'i='+id_line);
}
