var selection = 1; //initial image selection

function actionthis(imagendx) {
	clearTimeout(tid);
	changeimage(imagendx);
}

function changeimage(imagendx) {
    var currentImage = $("#promoimgscontainer div[class='selected']");
    selection = imagendx;
	currentImage.fadeOut("fast", function() {
	    $('#promoimg' + imagendx + ' > a > img').attr('src', $("#promoimg" + imagendx + " div").text());
		$("#promoimg" + imagendx).fadeIn();
		currentImage.removeClass();
		$("#promoimg" + imagendx).addClass("selected");
		$("#numbers ul li a").removeClass("numberselected");
		$("#numbers ul li:nth-child(" + imagendx + ") a").addClass("numberselected");
	})
	}

function auto() {
    
    if($("#promoimgscontainer div:last-child").hasClass("selected"))
        changeimage(1);
	else
	    changeimage(selection + 1);
	 
	tid = setTimeout("auto()", 4000);
}
//********

function close_promotion() {
    $('.showcase_popup').hide();
    $('#showcase_shadow').hide();
    $('.video_carousel_item_active').removeClass('video_carousel_item_active');
    $('.showcase_video_player_container').html('');
}

function display_promotion(id){
    close_promotion();
    
    popup = $('#showcase_popup_' + id);
    
    if (popup.length == 0) return;
    
    popup.find('img.lazyload').each(function(){
        $(this).attr('src', $(this).next().text());
    })
    popup.show();
    $('#showcase_shadow').show();
    popup.find('.video_carousel_item').first().click();
}

var tid = setTimeout("auto()", 4000);

function jQueryInitialize(scope)
{    
    $(scope).find("#numbers ul li a").click(function() {
		$("#numbers ul li a").removeClass("numberselected");
		$(this).addClass("numberselected");		
		clearTimeout(tid);
		changeimage(parseInt($(this).attr('href').replace('#', '')));
		return false;
	});
	
	$(scope).find('.showcase_tabs a').click(function() {
	    display_promotion(parseInt($(this).attr('href').replace('#', '')));
	    return false;
	});
	
	$(scope).find('#showcase_shadow').click(function(){
	    close_promotion();
	});
	
	$(scope).find('.showcase_popup_close_button').click(function(){
	    close_promotion();
	});
	
	$(scope).find('.showcase_video_carousel').jcarousel({
        vertical: true,
        scroll: 4
    });
    $(scope).find('.showcase_video_carousel').css('height', '9000px');
    
    $(scope).find('.video_carousel_item').hover(
        function(){
            if (!$(this).hasClass('video_carousel_item_active'))
            {
                $(this).addClass('video_carousel_item_hover');
            }
        },
        function(){
            $(this).removeClass('video_carousel_item_hover');
        }
    );
    
    $(scope).find('.video_carousel_item').click(function(){
        video_player = $(this).parents('.showcase_popup_content').find('.showcase_video_player_container');
        video_id = $(this).find('a').attr('href').replace('#','');
        video_player.html('<iframe class="youtube-player" type="text/html" width="535" height="300" src="https://www.youtube.com/embed/' + video_id + '?rel=0&autoplay=1" frameborder="0"></iframe>');        
        $('.video_carousel_item_active').removeClass('video_carousel_item_active');
        $(this).removeClass('video_carousel_item_hover');
        $(this).addClass('video_carousel_item_active');
        return false;
    });
       
    if (location.pathname.toLowerCase() == "/productdetails.asp" || location.pathname.indexOf("-p/") != -1 || location.pathname.indexOf("_p/") != -1) {
        has_free_shipping = false;
        $('img').each(function(){
            if ($(this).attr('src') == '/v/vspfiles/templates/Orphan_v2/images/Icon_FreeShipping.gif') {
                has_free_shipping = true;
            }
        });
        if (has_free_shipping) {
            $('#showcasetabs').removeClass('free_shipping').removeClass('standard_shipping').addClass('free_shipping');
        }
        else {
            $('#showcasetabs').removeClass('free_shipping').removeClass('standard_shipping').addClass('standard_shipping');
        }
    }
    
}


