/**
*public show object. 
*Not much more than a namespace.
*@author Bernie Witter  daybyday Media GmbH, Hamburg Germany
**/
var ftcp = {};
	ftcp.base = "/mediacenter/public_static/design_editor/";
	ftcp.title = "";
	//pages:
	ftcp.pages = null;
	//book props:
	ftcp.props = null;
	ftcp.text_props = null;
	ftcp.mode = "public";
	//string htmlElement where to throw template in:
	ftcp.container = "home";
	ftcp.stage = "stage";
	ftcp.theme = 1;
	ftcp.active = 1;
	ftcp.hint_preview = 1;
	
	/**
	*init the shit:
	**/
	ftcp.init = function(book_string)
	{
		
		try{
			eval(book_string);
		}catch(e){
			alert("Error reading data.");
			return;
		}
		
		//title:
		ftcp.title = pubShow.data.title;
		document.title = unescape(ftcp.title);
		//pages:
		ftcp.pages = pubShow.data.pages;
		//theme:
		ftcp.theme = pubShow.data.theme || 1;
		//book properties:
		ftcp.props = pubShow.data.props;
		ftcp.text_props = pubShow.data.text_props;
		//if in preview mode, set active page:
		if("preview"==ftcp.mode){
			 ftcp.active = pubShow.data.active;			 		
			}
		//get book theme:
		var url = ftcp.base+"themes/theme"+ftcp.theme+"/content.html";
		HTTP.get(url,ftcp._themeIn);
		
	}
	//==============================================
	ftcp._themeIn = function(result)
	{		
		UTIL.element(ftcp.container,result);
		UTIL.element("page_css").href = ftcp.base+"themes/theme"+ftcp.theme+"/styles.css";
		if("preview"==ftcp.mode) ftcp.build();
		else{
			//get navigation:
			var url = "show_js_objects/navi.html";
			HTTP.get(url,ftcp.build);
		}
	}
	//============================================
	ftcp.makeNavi = function(navi)
	{
		var s = "";
		for(var i in navi){
			s+= "<a onclick=\"window.location='"+navi[i].link+"'\">"+navi[i].label+"</a>";
		}
		return s;
	}
	//============================================
	ftcp.build = function(result)
	{
		var navi = "&nbsp;";
		try{
			eval(result);
			navi = ftcp.makeNavi(shows_navi);
		}catch(e){};
		
		var s = "";
		//in preview, let her go back:
		if("preview"==ftcp.mode){			
			s = "<a id=\"btn_back_to_edit\" onclick=\"ftcBook.showEditContent()\">"+txt.show_editor+"</a>";
			 UTIL.element("navigation",s);
		}else{
			//in public area we show navigation:
			/*
			s = "<a onclick=\"window.location.href='index.html'\" >";
			s+= txt.navi_welcome+"</a>";
			//guestbook:
			s+= "<a onclick=\"window.location.href='guestbook.html'\">";
			s+= txt.navi_guestbook+"</a>";
			//contact:
			s+= "<a onclick=\"window.location.href='kontakt.html'\" >";
			s+= txt.navi_contact+"</a>";
			*/
			s = navi;
			UTIL.element("navigation",s);
		}
		//pager:
			var pager = new ftcPaging(ftcp.pages.length,"pager",ftcp.navigate);
			pager.active = ftcp.active;
			
			pager.draw();						
			//book tilte:	
			var title = unescape(ftcp.title);
			title = UTIL.shorten(title,50);
		   UTIL.element("public_book_title",title);	
		  
		  //slideshow :
		  if("public"==ftcp.mode){
		 	//ftcp.slideShow();
		}
		//disable right click:
		UTIL.element(ftcp.container).oncontextmenu = function(){return false;};
			ftcp.generate();			
	}
	//======================================
	ftcp.alert_div = null;
	ftcp.generate = function()
	{
		var div = UTIL.newNode("DIV");
		document.body.appendChild(div);
		div.style.position = "absolute";
		div.style.width = "300px";
		div.style.height = "50px";
		div.style.textAlign = "center";
		div.style.left = "50%";
		div.style.marginLeft = "-150px";
		div.style.top = "200px";
		div.innerHTML = "generating...";
		ftcp.alert_div = div;
		window.setTimeout("ftcp.ready()",1000);
	}
	ftcp.ready = function(div)
	{
		document.body.removeChild(ftcp.alert_div);
		ftcp.navigate(ftcp.active);
	}
	//===================================================
	ftcp.navigate = function(no)
	{
		ftcp.drawPage(no);
	}
	//===============================================
	ftcp.drawPage = function(no)
	{
		var page = null ;
		for(var i=0;i<ftcp.pages.length;i++){
			if(no == ftcp.pages[i].no){
				page = ftcp.pages[i];
				break;
			}
		}
		if(null == page) return;
		//draw template:
		UTIL.element(ftcp.stage,page.tpl_htm);
		if(page.hasOwnProperty("bg_id")){
			UTIL.element("stage").innerHTML+=unescape(page.bg_htm);
			//set body props:
			if(null != page.bg_background)
				document.body.style.background = page.bg_background;
			if(null!=page.bg_color)
				document.body.style.color = page.bg_color;
				//pager:
				UTIL.element("td_ftcpmain").style.color = page.bg_color;
				//navigation:
				UTIL.element("navigation").style.color = page.bg_color;
		}else{
			//use book props:
			document.body.style.background = ftcp.props.background || "white";
			document.body.style.color = ftcp.props.color || "black";
			//pager:
			UTIL.element("td_ftcpmain").style.color = ftcp.props.color;
			//navigation:
			UTIL.element("navigation").style.color = ftcp.props.color || "black";
		}
		//get content:
		ftcp.fillTemplate(page);
	}
	//==========================================
	ftcp.fillTemplate = function(page)
	{
		var item,text;
		for(var i=0; i < page.items.length;i++){
			item = page.items[i];
			UTIL.element(item.parent_id,ftcp.drawItem(item));
			//zIndex:
			UTIL.element(item.parent_id).style.zIndex = item.zIndex;
			
			//attachment:
			if(item.obj.hasOwnProperty("attachment_object_id")){
				ftcp.drawAttachment(item);
			}
			//is video ? show open window link:
			var v_link = UTIL.element("video_link");
			if(v_link){
				if("video"==item.obj.media_type){
					v_link.style.display = "block";
					var link_src = ("preview"==ftcp.mode) ?  item.obj.src_prev : item.obj.src_public;
					var link_color = page.bg_color || ftcp.props.color ;
					v_link.innerHTML = "<a style=\"color:"+link_color+"\" href=\""+link_src+"\" target=\"_blank\">"+txt.open_window_for_video+"</a>";
				}else v_link.style.display = "none";
			}
		}
		//textboxes:
		for(var i=0;i<page.textboxes.length;i++){
			text = page.textboxes[i];
			ftcp.fillTextbox(text);
		}
	}
	//==========================================
	ftcp.drawAttachment = function(item)
	{
		var box = UTIL.element(item.parent_id);
		var icon = UTIL.newNode("IMG");
		var src = "/mediacenter/public_static/pix/";
		switch(item.obj.attachment_type){
			case "audio":
			src+= "icon_audio.jpg";
			break;
			default: src+="icon_pdf.jpg";break;
		}
		icon.src = src;
		//give it some infos:
		icon.media_src = ("preview"==ftcp.mode) ? item.obj.attachment_src_prev : item.obj.attachment_src;
		icon.media_type = item.obj.attachment_type;
		box.appendChild(icon);
		Handler.add(icon,"click",ftcp.openAttachment);
		//position the fucker:
		//icon dims:
		var icon_w = 59;
		var icon_h = 30;
		icon.style.width = icon_w+"px";
		icon.style.height = icon_h+"px";
		//image dims:
		var img = UTIL.element("ftcpimg"+item.id);
		var img_w = parseInt(img.offsetWidth);
		var img_h = parseInt(img.offsetHeight);
		icon.style.position = "absolute";
		icon.style.zIndex = "1000";
		icon.style.left = (img_w - icon_w)+"px";
		icon.style.top = (img_h - icon_h)+"px";
	}
	//=========================================
	ftcp.openAttachment = function(e)
	{
		e = e || window.event;
		//alert(e.target.media_type);
		switch(e.target.media_type){
			case "audio":
			UTIL.element("audio_container",UTIL.playAudio(e.target.media_src));
			break;
			default:
			window.open(e.target.media_src);
		}
	}
	//=========================================
	ftcp.drawItem = function(item)
	{
		//calc width:
		var box = UTIL.element(item.parent_id);
		var box_width = parseInt(box.offsetWidth);
		var box_height = parseInt(box.offsetHeight);	
		
		
		//image or viedeo ?
		switch(item.obj.media_type){
			default:
			//calc borders of images:
			box_width = box_width-(parseInt(ftcp.props.borderWidth)*2);
			box_height = box_height-(parseInt(ftcp.props.borderWidth)*2);
			var dims = UTIL.fit_image_to_box(box_width, box_height,item.obj.dim_x, item.obj.dim_y);
			var s = "<img src=\"";
			if("preview"==ftcp.mode || ! item.obj.src_public){
				s+= item.obj.src_medium;
			}else{
				s+= item.obj.src_public;
			}
			s+= "\" class=\"img-"+item.ratio+"\" ";
			s+= " id=\"ftcpimg"+item.id+"\" ";
			s+= " style=\"border:"+ftcp.props.borderWidth+"px solid "+ftcp.props.borderColor;
			//if(ftcp.props.borderWidth>0){
				s+= ";width:"+dims[0]+"px;height:"+dims[1]+"px;";
			//}
			s+= " \"/>";
			break;
			case "video":			
			var src = ("preview"==ftcp.mode) ? item.obj.src_prev : item.obj.src_public;
			var s = UTIL.quicktime(src,box_width, box_height);		
			break;
		}//end switch media type
		return s;
	}
	//===================================================
	ftcp.fillTextbox= function(textbox)
	{
		if(""==textbox.content) return "&nbsp;";
		var content = unescape(textbox.content);
		content = content.replace(/\n|\r/g,"<br/>");
		//convert links:
		content = UTIL.convert2Link(content, true);
		var box = UTIL.element(textbox.container);
		var htm = "<div class=\"t-box\" style=\"";
		htm+= textbox.css_style;
		htm+= "\">"+content+"</div>";
		box.innerHTML = htm;
		//if it has a tags set their color:
		var anchors = UTIL.tag("A", box);
		if(!anchors) return;
		var len = anchors.length;
		var a_color;
		try{
			a_color = textbox.props.color;
		}catch(e){
			a_color = ftcp.props.color;
		}
		for(var i=0; i<len; i++){
			anchors[i].style.color = a_color ;
		}
	}
	//slideshow ===========================================
	
	
	ftcp.slideShow = function()
	{
		try{
			//param: number pages, obj callback
			var slide = new dbdSlideShow(ftcp.pages.length,ftcPaging._select,ftcp.props.color);
			slide.setPage(ftcp.active);
			
		}catch(e){}
	}
	
