﻿//#region -- Slider
(function($) {
    $.fn.extend({
        productSlider: function() {
            //#region variables
            var container = this;
            container.slidetype = this.attr("slidetype");
            //#endregion
            //debugger;
            //#region -- command
            var command = function() {
                var pagenum = $(this).attr("pagenum");
                var direction = $(this).attr("direction");
                //var catID = $(this).attr("catID");
                //alert("id: " + $(this).attr("id") + ", pagenum: " + pagenum + ", slidetype: " + container.slidetype);

                Egghead.services.ajax.ucLoad({ controlPath: 'slider/slidetype'
					, data: { slidetype: container.slidetype, pagenum: pagenum }
					, success: function(response, result) {

					    if (jQuery.trim(direction) == 'right') {
					        container.find('#slider_body').append($(response.d).find('#slider_body_inner'));
					    }
					    else {
					        container.find('#slider_body').prepend($(response.d).find('#slider_body_inner'));

					        container.find('#slider_body_inner:first').css('margin-left', -container.find('#slider_body_inner').width());
					    }

					    if ($(response.d).find('.slider_left_arrow').length != 0) {
					        container.find('.slider_left_arrow, .slider_left_arrow_disabled').replaceWith($(response.d).find('.slider_left_arrow'));
					    }
					    if ($(response.d).find('.slider_left_arrow_disabled').length != 0) {
					        container.find('.slider_left_arrow, .slider_left_arrow_disabled').replaceWith($(response.d).find('.slider_left_arrow_disabled'));
					    }

					    if ($(response.d).find('.slider_right_arrow').length != 0) {
					        container.find('.slider_right_arrow, .slider_right_arrow_disabled').replaceWith($(response.d).find('.slider_right_arrow'));
					    }
					    if ($(response.d).find('.slider_right_arrow_disabled').length != 0) {
					        container.find('.slider_right_arrow, .slider_right_arrow_disabled').replaceWith($(response.d).find('.slider_right_arrow_disabled'));
					    }

					    container.find('.slider_left_arrow').unbind('click');
					    container.find('.slider_right_arrow').unbind('click');
		
		                $('.pr_snippet_category').each(function(index, element) {
			                try {
			                    var skuDiv = $('.sku', this);
			                    var sku = $(skuDiv[0]).text();
			                    GetReview(sku, this);
			                } catch (ex) {
			                    alert(ex);
			                }
			            });

					    if (jQuery.trim(direction) == 'right') {
					        container.find('#slider_wrapper').css("overflow", "hidden");
					        container.find('#slider_body').css("width", "1820px");
					        container.find('#slider_body_inner:first').animate({
					            marginLeft: -container.find('#slider_body_inner').width()
					        },
					            1500, function() {
					                $(this).remove();
					                container.find('#slider_wrapper').css("overflow", "visible");
					                container.find('#slider_body').css("width", "auto");
					                container.initialize();
					            });
					    }
					    else {
					        container.find('#slider_wrapper').css("overflow", "hidden");
					        container.find('#slider_wrapper').css("overflow", "hidden");
					        container.find('#slider_body').css("width", "1820px");
					        container.find('#slider_body_inner:first').animate({
					            marginLeft: 0
					        },
					            1500, function() {
					                container.find('#slider_body_inner:last').remove();
					                container.find('#slider_wrapper').css("overflow", "visible");
					                container.find('#slider_body').css("width", "auto");
					                container.initialize();
					            });
					    }

					}
					, error: function(response, result) {
					    //debugger;
					    alert("Error while loading slide items");
					}
                });
            }

            //#endregion

            //#region -- initialize
            this.initialize = function() {
                this.left = $(".slider_left_arrow", this);
                this.right = $(".slider_right_arrow", this);

                this.left.click(command);

                this.right.click(command);
            }

            //#endregion -- initialize
            this.initialize();

            return this;
        }
    })
})(jQuery);
//#endregion -- Slider
