if ( window.console == undefined || window.console.log == undefined ) {	window.console = { log:function(msg){ /* msg */ } } }
if ( typeof(window.billboard)=="undefined"){ billboard={}; }

billboard.slideshow = new ( function($) {

    var me = this;
	 me.init = function(){
		billboard.broadcaster.addListener( "pageLoaded", function() { me.onPageLoaded(); } );
    };
    me.onPageLoaded = function(){

		$("#slideshow-wrapper .previous").hide();
		me.sizeSlideshowImages();

		$("#slideshow-wrapper .next").click(function(){
			var $active = $('#image-list IMG.active');
			var $next = $active.next();
			if($next.get(0)){
				$("#slideshow-wrapper #curr-index").text($next.attr("id"));
				me.slideEl(["#image-list IMG",".slideshow-info .caption p",".slideshow-image .credits p"],"next");
				//billboard.ads.refreshAd( $("iframe.ad-iframe.slideshow")[0] );
				billboard.ads.refreshAllAds();
				billboard.metrics.trackPageView();
			}
			if($next.next().get(0) == undefined) $(this).hide();
			if($next.prev().get(0)) if($("#slideshow-wrapper .previous").is(":hidden")) $("#slideshow-wrapper .previous").show();
		});
		$("#slideshow-wrapper .previous").click(function(){
			var $active = $('#image-list IMG.active');
			var $prev = $active.prev();
			if($prev.get(0)){
				$("#slideshow-wrapper #curr-index").text($prev.attr("id"));
				me.slideEl(["#image-list IMG",".slideshow-info .caption p",".slideshow-image .credits p"],"prev");
				billboard.ads.refreshAllAds();
				//billboard.ads.refreshAd( $("iframe.ad-iframe.slideshow")[0] );
				billboard.metrics.trackPageView();
			}
			if($prev.prev().get(0) == undefined) $(this).hide();
			if($prev.next().get(0)) if($("#slideshow-wrapper .next").is(":hidden")) $("#slideshow-wrapper .next").show();

		});
		$('.artist-info-slideshow, .article-slideshow').mouseover(function(){
			if($(this).find(".slideshow-url").length!=0) $(this).find('img').addClass('hvr-bdr');
			$(this).find('.amount-btn').hide();
			$(this).find('.slideshow-btn').show();
			return false;
		});
		$('.artist-info-slideshow, .article-slideshow').mouseout(function(){
			if($(this).find(".slideshow-url").length!=0) $(this).find('img').removeClass('hvr-bdr');
			$(this).find('.amount-btn').show();
			$(this).find('.slideshow-btn').hide();
			return false;
		});
		$('.article-slideshow, .artist-info-slideshow').click(function(){
			var iframeSrc = $(this).parent().find(".slideshow-url").text();
			if(iframeSrc && iframeSrc != "")
				billboard.modal.show( "<iframe src='"+ iframeSrc +"?decorator=slideshow&confirm=true' scrolling='no' frameborder=0 width='961' height='582' id='slideshow-display'/>" );
		});
	
		$("#close-slideshow").click( function(){
			top.billboard.modal.hide();

		});
		

    };
	me.slideEl = function(arr,dir){
		$(arr).each(function() {
			$(this +".active").addClass("last-active");
			if(dir == 'next') $(this +".active").next().addClass("active");
			else $(this+".active").prev().addClass("active");
			$(this+".last-active").removeClass("active last-active");
		});
		
	};
	me.sizeSlideshowImages = function(){
		lHeight = $('#slideshow-wrapper .slideshow-image').height();
		lWidth = $('#slideshow-wrapper .slideshow-image').width();
		 $("table#image-list img").each( function(o,img){
			 iHeight = $(img).height();
			 iWidth = $(img).width();
			 if(iHeight > iWidth){
				if(iHeight > lHeight) $(img).attr("height", lHeight); 
			 }
			 else{
				if(iWidth > lWidth) $(img).attr("width", lWidth);
			 }
		 });
	 };

	//On Load
    $( function(){		
        me.init();
	});//End On Load
})(jQuery);
