// $Id: common.js 38355 2010-07-19 16:43:49Z kt $

/**
 * Tosho commented out this line
 * because of the thickbox image prev/next issue
 * 'Item page when you have multiple images and you click one to enlarge the prev and next do not work.'
 */
//jQuery.noConflict();

jQuery(document).ready(function () {

    /*
    * Switch List/Grid layout
    * 
    */
    
    jQuery(".nav-layout a").click(function() {
        if ( jQuery(this).is(".nav-layout-list") ) {
            jQuery.ajax({
                url: "list.html",
                success: function(html){
                    jQuery("#products-content").append(html);
                }
            });
        }
    });

    /**
    * CHECKOUT Step 1 of 3: Authentication; sets the radio button
    * next to the password field to 'checked'
    */
    jQuery("input[name='password']").focus(function () {
        jQuery("input[value='login']").attr("checked", "checked");
    });

    /**
    * Initialize jCarousel for RSS feeds
    */
    if (typeof jQuery.fn.jcarousel != "function") {
    	jQuery.fn.jcarousel = function() {}
    	}

    jQuery('#rss-carousel').jcarousel({
        "vertical": true,
        "scroll": 2
    });

    jQuery('.front-page-products').jcarousel({
        "vertical": false
    });

    /*
    * Show/Hide Tabs for category selection
    */
    /*
    jQuery(".feeds-button").click(function () {
      jQuery(this).next().slideToggle("slow");
    });

    jQuery(".feeds-content").hide();
    */

    /*
    * Dropdown menu for category selection
    */
    jQuery("#feeds-selector").change(function () {

        var selected = jQuery(this).val();
        var products_template = jQuery("#products_template").val();
        var url = smarty_vars['html_url']+"?page=rss&action=products_list&feed=" + selected + "&products_template=" + products_template;
        jQuery(".feeds-content *").remove();
        jQuery(".feeds-content").append('<ul class="jcarousel jcarousel-skin-strict"></ul>');

        jQuery.ajax({
            url:  url,
            cache: false,
            dataType: 'html',
            success: function(html){
                jQuery("ul.jcarousel").append(html);
                jQuery('ul.jcarousel').jcarousel({
                    "vertical": true,
                    "scroll": 2
                });
            }
        });
    });

});

/**
 * Vote for review
 *
 * @param {String} sku
 * @param {Integer} review_id
 * @param {String} vote (yes|no)
 */
function review_vote(sku, review_id, vote) {
	var url = smarty_vars['html_url']+"?page=review_vote&id="+sku+"&reviewid="+review_id+"&vote=" + vote;
	window.location.replace(url);
}

///////////////////////////////////////////////////////////////////////////////

/** Apply stock-level image overlays */
jQuery(document).ready(
	function() {
		var skus = [];

		jQuery('a > img').filter(
			function() {
				/* use only item images */
				return (this.src.indexOf('mas_assets/image_cache/') != -1);
				}
			).each(
			function() {
				/* extract the SKU */
				var href = this.parentNode.href, sku = href.match(/\/item\/([^\/]+)\//i);

				/* pretty URLs */
				if (sku != null && sku.length == 2) {
					jQuery(this.parentNode).addClass(sku[1]);
					skus.push(sku[1]);
					return true;
					}
				/* regular URLs */
				if (href.search(/page=item/i) != -1) {
					sku = href.match(/id=([^&]+)($|&)/i);
					jQuery(this.parentNode).addClass(sku[1]);
					skus.push(sku[1]);
					return true;
					}
				}
			);
		if (skus.length == 0) {
			return;
			}
	
		jQuery.getJSON(
			smarty_vars.rel_html_url 
				+ 'index.php?page=item_ajax&action=stock_levels&id=' 
				+ encodeURIComponent(JSON.stringify(skus)), 
			function(data) {
				jQuery.each(data, 
					function(sku, level) {
						jQuery('a.' + sku)
							.removeClass(sku)
							.addClass('stock-level-' + level);
					})
			});
		}
	);
