function bannerRotator(selector, options){
    options = options || {};
    var initCallback = function(carousel){
        if (options.stopOnClick){
            // Disable autoscrolling if the user clicks the prev or next button.
            carousel.buttonNext.bind('click', function(){
                carousel.startAuto(0);
            });
            carousel.buttonPrev.bind('click', function(){
                carousel.startAuto(0);
            });
        }

        // Pause autoscrolling if the user moves with the cursor over the clip.
        carousel.clip.hover(function(){
            carousel.stopAuto();
        }, function(){
            carousel.startAuto();
        });

        if (options.initCallback)
            options.initCallback.apply(this, arguments);
    };

    if (jQuery(selector + ' > li').size() > 1){
        return jQuery(selector).jcarousel({
            auto: options.auto || 5,
            scroll: options.scroll || 1,
            offset: 1,
            start: 1,
            wrap: options.wrap || 'circular',
            buttonNextHTML: options.buttonNextHTML !== undefined ? options.buttonNextHTML : '<a href="#" onclick="return false;">Next</a>',
            buttonPrevHTML: options.buttonPrevHTML !== undefined ? options.buttonPrevHTML : '<a href="#" onclick="return false;">Previous</a>',
            initCallback: initCallback,
            itemVisibleInCallback:options.itemVisibleInCallback || null,
            animation:options.animation || 'fast'
        });
    }
    return null;
}

var modalWindow = {
    parent:'body',
    windowId:null,
    content:null,
    width:null,
    height:null,
    css_update:null,
    direction:'modal-row-right',
    close:function(){
        jQuery(".modal-overlay").remove();
        jQuery(".modal-window").stop().animate({'opacity':0}, 'fast', function(){
            jQuery(this).remove();
        });
    },
    open:function(callback){
        var modal = "";
        modal += "<div class=\"modal-overlay\"></div>";
        modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px;\"><div class=\"modal-top\">&nbsp;</div><div class=\"modal-inner\">";
        modal += this.content;
        modal += '</div><div class="modal-bottom">&nbsp;</div><div class="modal-row '+this.direction+'">&nbsp;</div>';
        modal += "<div class=\"modal-loading\"></div></div>";

        modal = jQuery(modal);
        jQuery(this.parent).append(modal.hide());

        jQuery('.modal-window').css(this.css_update).append("<a class=\"close-window\"></a>");
        jQuery('.close-window').click(function(){modalWindow.close();});
        jQuery('.modal-overlay').click(function(){modalWindow.close();});

        jQuery(modal).fadeIn();
        if (callback)
            callback(jQuery(this.parent));
    }
};

var Econoco = {
    text: {},
    openQuickview: function(source, css_update, direction){
        modalWindow.windowId = "modalOverlay";
        modalWindow.width = 460;
        modalWindow.height = 290;
        modalWindow.content = "<div class='iframe-wrapper' style='width:436px;height:300px;'><iframe id='iframe_quickview' onload=\"Econoco.onQuickviewLoad(this)\" width='436' height='300' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'></iframe></div>";
        modalWindow.css_update = css_update;
        modalWindow.direction = direction;
        modalWindow.open();
    },
    closeQuickview: function($){
        modalWindow.close();
        if (animate)
            jQuery('html, body').animate({scrollTop:0}, 'fast');
    },
    onQuickviewLoad: function(frame){
        jQuery(frame).parents('.modal-window').children('.modal-loading').hide();
    }
};


jQuery(function($){
    $('body').addClass('js');
    $('.image-rotator li').preloadImages();
    $('#main-banners li.no-display').removeClass('no-display');

    $("a.extrn, a[rel*=external]").attr({'target': "_blank"});

    // main banners
    var main_banners = $('#main-banners'),
        pager = main_banners.parent().find('.rotator-pagination'),
        count = main_banners.children().size();
    pager.children().first().addClass('current');
    bannerRotator('#main-banners', {
        wrap: 'both',
        itemVisibleInCallback: {
            onBeforeAnimation: function(carousel, element, idx){
                pager.children('.current').removeClass('current');
                pager.find('a[rel='+(Math.abs((idx-1)%count)+1)+']').parent().addClass('current');
            }
        }
    });
    pager.delegate('a', 'click', function(){
        jQuery(main_banners).data('jcarousel').scroll(jQuery.jcarousel.intval($(this).attr('rel')));
        return false;
    });
});

/* main menu */
jQuery(function($){
    $('#nav .dropdown').each(function(){
        var $this = $(this),
            width = 0,
            positionRight = 0,
            props = {'position': 'absolute', 'visibility': 'hidden', 'display': 'block'};
        var position = $this.parent().position();
        var parentWidth = $this.parent().width();
        var positionLeft = position.left + 8;
        jQuery.swap(this, props, function(){
            $this.find('.col').each(function(){
                width = width + $(this).outerWidth();
            });
        });
        width += 16;
        if (positionLeft < 480 ){
            if((positionLeft + width) > 970){
                $this.css('right',13);
            } else{
                $this.css('left',positionLeft);
            }
        } else{
            if((980 - parentWidth - positionLeft + 16 + width) > 970){
                $this.css('left',13);
            } else{
                $this.css('right',(980 - parentWidth - positionLeft + 16));
            }
        }
        $this.css('width',width);
    });
});


/*Quick look*/
jQuery(function($){
    $('.category-products .products-grid').delegate('.item .quick-look', 'click', function(){
        var $this = $(this),
            href = $this.parent().find('a.product-image').attr('href'),
            position = $this.parent().position();
        if (href.indexOf('?') != -1){
            if (href[href.length - 1] != '?')
                href += '&';
        } else {
            href += '?';
        }
        href += 'isAjax=1';
        var css_update, direction;
        if ($this.parent().hasClass('first')){
            css_update = {left:(position.left + 200), top:(position.top - 30)};
            direction = 'modal-row-left';
        } else {
            css_update = {left:(position.left - 420), top:(position.top - 30)};
            direction = 'modal-row-right';
        }
        Econoco.openQuickview(href, css_update, direction);
        var temp_var = 0;
        jQuery('#iframe_quickview').load(function() {
        	if(temp_var==1)
        		Econoco.closeQuickview();
        	temp_var++;
        });
        return false;
    });
});

/*No Zip*/
jQuery(function($){
    $('.shipping-estimate .change-zip').click(function(e){
        var $this = $(this);
            $this.parent().hide();
            $('.nozip').show();
        });
});


/*Select*/
jQuery(function($){
	$('.catalog-category-view .sort-by select').sSelect();
	$('.catalogsearch-result-index .sort-by select').sSelect();
});


/* Fancybox IE speed issue fix */
function fancyboxOnStart(css_url){
    return (jQuery('.fancybox-ie').length != 0) ? function(){
        if (jQuery('#fbiecssfix').length == 0){
            jQuery('head').append('<link id="fbiecssfix" rel="stylesheet" type="text/css" media="all" href="'+css_url+'" />');
        }
    } : function(){};
}

