


var topelement_leserreporter_img = new Array ('<a href="http://leserreporter.20min.ch" target="_blank" class="leserreporter">Leserreporter</a>', '<div target="_blank" class="leserreporter" style="background-image:url(/2010/img/topelement/leserreporter_bubble_fr.png);">Lecteur reporter</div>');

var objectid = "topelement_"+ee_get_showid();
if ( typeof slideshowdata == "undefined" ) {
	var slideshowdata = new Array();
}

slideshowdata[objectid] = new Array();
slideshowdata[objectid]['id']					= objectid;
slideshowdata[objectid]['div']					= "#"+objectid;
slideshowdata[objectid]['currentcontent']		= 0;
slideshowdata[objectid]['currentcontentload']	= new Array();
slideshowdata[objectid]['currentimagepage']		= 1;
slideshowdata[objectid]['gallery_info']			= ee_get_json();
slideshowdata[objectid]['totalcontent'] 		= slideshowdata[objectid].gallery_info.pages.length;
slideshowdata[objectid]['totalimages']			= slideshowdata[objectid].gallery_info.pagecount;
slideshowdata[objectid]['lastmove']				= 0;

var popup = 0;
var captionlineheight=15;
var minheight = 44;

for (var counter=0;counter<slideshowdata[objectid].totalcontent;counter++) {
	$(slideshowdata[objectid].div+" .imageholder").append('<div class="imagewrap"></div>');
	slideshowdata[objectid].currentcontentload.push(false);
}

/* quickfix, sollte mal in der html-ausgabe angepasst werden */
$(slideshowdata[objectid].div+" .back .icon").removeClass("inactive");
$(slideshowdata[objectid].div+" .back .icon").addClass("active");

if ( typeof relatedshows == "undefined" ) {
	var relatedshows = new Array();
	$.getJSON("/diashow/relshows.tmpl?showid="+ee_get_showid(), function(json) { relatedshows[objectid]=json; initialize(); });
}

if ( typeof topshows == "undefined" ) {
	var topshows;
	$.getJSON("/diashow/topshows.jsx?nocache", function(json) { topshows=json; initialize(); });
}






/* all bindings */
$(slideshowdata[objectid].div+" .back").click(function () {
	var slideshow = slideshowdata[this.parentNode.parentNode.id];
	step_back (slideshow);
});

$(slideshowdata[objectid].div+" .forward").click(function () {
	var slideshow = slideshowdata[this.parentNode.parentNode.id];
	step_forward (slideshow);
});

$(slideshowdata[objectid].div+" .imageholder").click(function (e) {
	//normal image view
	if (e.target.nodeName == "IMG") {
		var grossAnsicht = $(".story_media a.grossAnsicht");
		//is within a popup..
		if (grossAnsicht.length === 0) {
			var slideshow = slideshowdata[this.parentNode.parentNode.id];
			step_forward(slideshow);
		}
		//is within topelem
		else {
			document.location = grossAnsicht.attr("href");
		}
	}
	//most probably endPage..
	/*else if (e.target.parentNode.nodeName=="A"){
		var showID=e.target.parentNode.onclick;
		//.match(/showid=(\d+)/);
		if (typeof console!=="undefined"){ console.log(showID); console.log(diaHtml5Load); }
		if (showID!==null && showID.length>=2 && typeof diaHtml5Load!=="undefined"){
			showID=showID[1];
			diaHtml5Load(showID[1],0);
		}
	}*/
});

$(slideshowdata[objectid].div+" .restart_link").click(function () {
	var slideshow = slideshowdata[this.parentNode.parentNode.parentNode.id];
	$(slideshow.div+" .restart_link").hide();
	slideshow.currentcontent = 0;
	slideshow.currentimagepage = 1;
	movetopelement (slideshow,0);
});

/* ==> keyboard.js */
keyboard.bindChar( 39, function () {
	var slideshow = slideshowdata[objectid];
	step_forward (slideshow);
});
keyboard.bindChar( 37, function () {
	var slideshow = slideshowdata[objectid];
	step_back (slideshow);
});


var updateHtml5DiaLink=function(slideshow){
	var link=$(".story_media a.grossAnsicht");
	if (link.length !== 0) {
		split = link.attr("href").split(/(\d+)$/);
		if (typeof split !== "undefined" && split.length >= 3) {
			link.attr("href", split[0] + (slideshow.currentimagepage - 1));
		}
	}
}



function initialize () {
	update_pagenumbers(slideshowdata[objectid]);
	update_text_and_height(slideshowdata[objectid]);
	handle_leserreporter(slideshowdata[objectid]);
	fill_in_content(slideshowdata[objectid], 0);
	fill_in_content(slideshowdata[objectid], 1);
	$(slideshowdata[objectid].div+" .cooltent").html(slideshowdata[objectid].gallery_info.txt_cooltip);
	if ( $(slideshowdata[objectid].div+" .leserreporter").size() == 0 ) $(slideshowdata[objectid].div+" .caption").after(topelement_leserreporter_img[topelement_get_language()]);
}





function topelement_get_language () {
	if (self.location.href.indexOf("/ro/") < 0) return 0; /* de */
	else return 1; /* fr */
}





function step_back (slideshow) {
	if (slideshow.currentcontent>0) {
		fill_in_content(slideshow,slideshow.currentcontent-2);
		movetopelement(slideshow,-1);
		$(slideshow.div+" .restart_link").hide();
	}
	else {
		slideshow.currentimagepage = 0;
		fill_in_content(slideshow,slideshow.totalcontent-2);
		fill_in_content(slideshow,slideshow.totalcontent-1);
		fill_in_content(slideshow,slideshow.totalcontent-3);
		if (slideshow.totalcontent-slideshow.totalimages==1) slideshow.currentimagepage = 1; // if there's no add correct the pagenumber
		movetopelement(slideshow,slideshow.totalcontent-2);
	}
	refresh_wemfngoogle(slideshow);
	updateHtml5DiaLink(slideshow);
}






function step_forward (slideshow) {
	if (slideshow.currentcontent<slideshow.totalcontent-1) {
		if (slideshow.currentcontent<slideshow.totalcontent-2) {
			fill_in_content(slideshow,slideshow.currentcontent+2);
		}
		refresh_wemfngoogle(slideshow);
		movetopelement(slideshow,1);
	}
	else {
		$(slideshowdata[objectid].div+" .restart_link").click();
	}
	updateHtml5DiaLink(slideshow);
}






function handle_leserreporter (slideshow) {
	if ( slideshow.gallery_info.pages[slideshow.currentcontent].is_lr == "1" ) {
		$(slideshow.div+" .leserreporter").show();
	}
	else {
		$(slideshow.div+" .leserreporter").hide();
	}
}







function movetopelement (slideshow,direction) {
	var current_op = slideshow.gallery_info.pages[slideshow.currentcontent].op;
	if (current_op == "lastpage") {
		slideshow.currentimagepage--;
	}
	else if (current_op == "page") {
		slideshow.currentimagepage = slideshow.currentimagepage + direction;
	}
	else if ( (current_op == "ad") && (direction!=slideshow.gallery_info.lastmove) )
	{
		slideshow.currentimagepage = slideshow.currentimagepage + direction;
	}
	slideshow.gallery_info.lastmove = direction;
	slideshow.currentcontent = slideshow.currentcontent + direction;
	
	var currentposition = -597 * (slideshow.currentcontent);
	
	$(slideshow.div+" .pagelink").fadeOut("slow");
	$(slideshow.div+" .imageholder").css( "left", currentposition+"px" );
	
	if (slideshow.gallery_info.pages[slideshow.currentcontent].op=="page") {
		update_pagenumbers(slideshow);
	}
	update_text_and_height(slideshow);
	handle_leserreporter(slideshow);
}






function buildimagepage (slideshow, loadingslot) {
	var maxwidth=600;
	var maxheight=400;
	var image_width = slideshow.gallery_info.pages[loadingslot].w;
	var image_height = slideshow.gallery_info.pages[loadingslot].h;
	var attribute="";
	var padding_top=0;
	var prop_pic=image_width/image_height;
	var prop_max=maxwidth/maxheight;
	if ( (image_height>maxheight) || (image_width>maxwidth) ) {
		if ( (image_width/image_height) > (maxwidth/maxheight) ) {
			var scaled_height = maxwidth * image_height / image_width;				
			padding_top = parseInt( (maxheight - scaled_height) / 2 );
			attribute='width="'+maxwidth+'"';			
		}
		else {
			attribute='height="'+maxheight+'"';
		}
	}
	else {
		padding_top = parseInt( (maxheight - image_height) / 2 );
		attribute='width="'+image_width+'" height="'+image_height+'"';		
	}
	var preload_image = new Image();
	preload_image.src=slideshow.gallery_info.pages[loadingslot].file;
	$(slideshow.div+" .imageholder div:eq("+loadingslot+")").replaceWith('<div class="imagewrap" style="padding-top:'+padding_top+'px;"><img src="'+slideshow.gallery_info.pages[loadingslot].file+'" '+attribute+' /></div>');
}






function build_lastpage (slideshow, loadingslot) {
	if ( ( slideshow.gallery_info.tilllate == null ) || ( slideshow.gallery_info.tilllate == "" ) ) {
		build_defaultlastpage (slideshow, loadingslot);
	}
	else {
		build_tillatelastpage (slideshow, loadingslot);
	}
}






function build_defaultlastpage (slideshow, loadingslot) {
	if ( slideshow.popup) {
		$(slideshow.div+" .imageholder div:eq("+loadingslot+")").replaceWith('<div class="lastwrap"><h6>'+topshows.desc+'</h6><ul class="topshows"></ul></div>');
		for (var counter=0;counter<18;counter=counter+1) {
			var icon = ' style="background-image:url(http://www.20min.ch/diashow/images/kein_bild_panel.png);" ';
			if (topshows.items[counter].hasicon=="1") icon = ' style="background-image:url(http://www.20min.ch/diashow/'+topshows.items[counter].showid+'/navpicture_panel.jpg);" ';
			$(slideshow.div+" .imageholder .lastwrap ul.topshows").append('<li><a href="http://www.20min.ch/diashow/diashow.tmpl?showid='+topshows.items[counter].showid+'"><div class="thumb" '+icon+' ></div><span>'+topshows.items[counter].top+'</span></a></li>');
		}
	}
	else if (slideshow.gallery_info.catid == 810) {
		$(slideshow.div+" .imageholder div:eq("+loadingslot+")").replaceWith('<div class="lastwrap"><h6>'+relatedshows[slideshow.id].desc+'</h6><ul class="relatedshows"></ul></div>');
		var number_related_shows = relatedshows[slideshow.id].items.length;
		if ($.browser.msie) number_related_shows--; /* IE measures json-arrays badly, if they end with an empty "," as in this case */
		if (number_related_shows>18) number_related_shows = 18;
		for (var counter=0;counter<number_related_shows;counter=counter+1) {
			var icon = ' style="background-image:url(http://www.20min.ch/diashow/images/kein_bild_panel.png);" ';
			if (topshows.items[counter].hasicon=="1") icon = ' style="background-image:url(http://www.20min.ch/diashow/'+relatedshows[slideshow.id].items[counter].showid+'/navpicture_panel.jpg);" ';
			$(slideshow.div+" .imageholder .lastwrap ul.relatedshows").append('<li><a href="javascript:;" onclick="javascript:window.open(\'http://www.20min.ch/ro/diashows/diashow.tmpl?showid='+relatedshows[slideshow.id].items[counter].showid+'\', \'popup\', \'width=875,height=770\');"><div class="thumb" '+icon+' ></div><span>'+relatedshows[slideshow.id].items[counter].top+'</span></a></li>');
		}
	}
	else {
		$(slideshow.div+" .imageholder div:eq("+loadingslot+")").replaceWith('<div class="lastwrap"><h6>'+relatedshows[slideshow.id].desc+'</h6><ul class="relatedshows"></ul></div>');
		var number_related_shows = relatedshows[slideshow.id].items.length;
		if ($.browser.msie) number_related_shows--; /* IE measures json-arrays badly, if they end with an empty "," as in this case */
		if (number_related_shows>9) number_related_shows = 9;
		for (var counter=0;counter<number_related_shows;counter=counter+1) {
			var icon = ' style="background-image:url(http://www.20min.ch/diashow/images/kein_bild_panel.png);" ';
			if (topshows.items[counter].hasicon=="1") icon = ' style="background-image:url(http://www.20min.ch/diashow/'+relatedshows[slideshow.id].items[counter].showid+'/navpicture_panel.jpg);" ';
			$(slideshow.div+" .imageholder .lastwrap ul.relatedshows").append('<li><a href="javascript:;" onclick="javascript:window.open(\'http://www.20min.ch/diashow/diashow.tmpl?showid='+relatedshows[slideshow.id].items[counter].showid+'\', \'popup\', \'width=875,height=770\');"><div class="thumb" '+icon+' ></div><span>'+relatedshows[slideshow.id].items[counter].top+'</span></a></li>');
		}
		$(slideshow.div+" .imageholder .lastwrap").append('</ul><h6>'+topshows.desc+'</h6><ul class="topshows">');
		for (var counter=0;counter<9;counter=counter+1) {
			var icon = ' style="background-image:url(http://www.20min.ch/diashow/images/kein_bild_panel.png);" ';
			if (topshows.items[counter].hasicon=="1") icon = ' style="background-image:url(http://www.20min.ch/diashow/'+topshows.items[counter].showid+'/navpicture_panel.jpg);" ';
			$(slideshow.div+" .imageholder .lastwrap ul.topshows").append('<li><a href="javascript:;" onclick="javascript:window.open(\'http://www.20min.ch/diashow/diashow.tmpl?showid='+topshows.items[counter].showid+'\', \'popup\', \'width=875,height=770\');"><div class="thumb" '+icon+' ></div><span>'+topshows.items[counter].top+'</span></a></li>');
		}
	}
	$(slideshow.div+" .imageholder .lastwrap ul").append('</ul>');
}






function build_tillatelastpage (slideshow, loadingslot) {
	var tillatecode = '<a href="'+slideshow.gallery_info.tilllate+'" target="_blank" class="tillatewrap"><h6>Alle weiteren Fotos zu dieser Story findest du auf www.tilllate.com</h6><div class="tillatethumbs">';
	if( topelement_get_language () ) {
		tillatecode = '<a href="'+slideshow.gallery_info.tilllate+'" target="_blank" class="tillatewrap"><h6>D\'autres images sur www.tillate.com</h6><div class="tillatethumbs">';
	}
	var counter = 0;
	var hits = 0;
	while ( ( hits < 4 ) && ( hits < slideshow.totalimages ) ) {
		if ( slideshow.gallery_info.pages[counter].file != undefined ) {
			var image_width = slideshow.gallery_info.pages[counter].w;
			var image_height = slideshow.gallery_info.pages[counter].h;
			var shift_top = parseInt( 0.5 * ( 120 - ( 120 * image_height / image_width ) ) );
			var shift_left = parseInt( 0.5 * ( 120 - ( 120 * image_width / image_height ) ) );
			if (image_height / image_width > 1) tillatecode += '<div class="thumb"><img class="portrait" src="'+slideshow.gallery_info.pages[counter].file+'" style="position:relative;margin-top:'+shift_top+'px;" /></div>';
			else tillatecode += '<div class="thumb"><img class="landscape" src="'+slideshow.gallery_info.pages[counter].file+'" style="display:block;;margin-left:'+shift_left+'px;" /></div>';
			hits++;
		}
		counter++;
	}
	tillatecode += '</div><img class="tillatelogo" src="/2010/img/topelement/tilllate_logo.png" /></a>';
	$(slideshow.div+" .imagewrap:last").html(tillatecode);
}






function fill_in_content (slideshow, loadingslot) {
	if (slideshow.currentcontentload[loadingslot] == false) {
		/* --- IMAGE PAGE --- */
		if (slideshow.gallery_info.pages[loadingslot].op=="page") {
			buildimagepage (slideshow, loadingslot);
		}	
		/* --- ADVERTISEMENT PAGE --- */
		else if (slideshow.gallery_info.pages[loadingslot].op=="ad") {
			$(slideshow.div+" .imageholder div:eq("+loadingslot+")").replaceWith('<div class="reklamadowrap"><iframe src="'+slideshow.gallery_info.pages[loadingslot].adurl+'" width="597" height="400" frameborder="0" scrolling="no" style="float:left;"></iframe></div>');
		}	
		/* --- LAST PAGE --- */
		else if (slideshow.gallery_info.pages[loadingslot].op=="lastpage") {
			build_lastpage (slideshow, loadingslot);
		}
		slideshow.currentcontentload[loadingslot] = true;
	}
}






function update_pagenumbers (slideshow) {
	$(slideshow.div+" .pagenumber").text(slideshow.currentimagepage+"|"+slideshow.totalimages);
}






function update_text_and_height (slideshow) {
	var new_height =-1;
	if (slideshow.gallery_info.pages[slideshow.currentcontent].op=="page") {
		$(slideshow.div+" .texttester").html(slideshow.gallery_info.pages[slideshow.currentcontent].txt);
		var number_of_lines = $(slideshow.div+" .texttester").height() / captionlineheight;
		var prepend_if_short_text = "";
		if (number_of_lines<=1) {
			prepend_if_short_text = "<div style='height:14px;font-size:1px;line-height:1px;'></div>";
		}
		else if (number_of_lines<=2) {
			prepend_if_short_text = "<div style='height:7px;font-size:1px;line-height:1px;'></div>";
		}
		new_height = (captionlineheight*number_of_lines)+2;
		if (new_height<minheight) new_height=minheight;
		$(slideshow.div+" .caption span").html(prepend_if_short_text+slideshow.gallery_info.pages[slideshow.currentcontent].txt);
		$(slideshow.div+" .copyright").html(slideshow.gallery_info.pages[slideshow.currentcontent].copyright);
		
		//Link generieren
		var sLink=makePageLink(slideshow.gallery_info.pages[slideshow.currentcontent].link_text,slideshow.gallery_info.pages[slideshow.currentcontent].link_url,slideshow.gallery_info.pages[slideshow.currentcontent].link_targetblank)
		if (sLink!="") {
			$(slideshow.div+" .pagelink").html(sLink);
			$(slideshow.div+" .pagelink").fadeIn("slow");
		}
	}
	else if (slideshow.gallery_info.pages[slideshow.currentcontent].op=="ad") {
		$(slideshow.div+" .caption span").html("<div style='height:14px;'></div>"+slideshow.gallery_info.txt_ad);
		$(slideshow.div+" .copyright").html("");
		new_height = minheight;
	}
	else if (slideshow.gallery_info.pages[slideshow.currentcontent].op=="lastpage") {
		$(slideshow.div+" .restart_link").fadeIn("slow");
		$(slideshow.div+" .caption span").html("");
		$(slideshow.div+" .copyright").html("");
		new_height = minheight;
	}
	$(slideshow.div+" .caption").animate( { height:new_height+"px" }, 200 );
	$(slideshow.div+" .back").animate( { height:new_height+15+"px" }, 200 );
	$(slideshow.div+" .forward").animate( { height:new_height+15+"px" }, 200 );
}






function refresh_wemfngoogle (slideshow) {
	var google = '<iframe src="'+slideshow.gallery_info.google+'?showid='+slideshow.gallery_info.showid+'&nocache='+(slideshow.currentimagepage+1)+'" style="border: 0pt none ; width: 0px; height: 0px;" scrolling="no"></iframe>';
	var wemf=slideshow.gallery_info.wemf.replace( /src="(.+?)"/g ,"src=\"$1?r="+escape(document.referrer)+"&d="+(new Date()).getTime()+"\"" );
	$(slideshow.div+" .wemfngoogle").html(wemf+google);
}






function makePageLink (sLinkText,sLinkUrl,sLinkTargetBlank) {
	var sGeneratedHtml='';
	if(sLinkUrl != '') {
		var sTargetBlank='';
		if(sLinkTargetBlank != '') {
			sTargetBlank='target="_blank"';
		}
		if(sLinkText == '') {
			sLinkText='Link';
		}
		sGeneratedHtml='<a href="' + sLinkUrl + '" ' + sTargetBlank + '>' + sLinkText + '</a>';
	}
	return sGeneratedHtml;
}








