/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

var tb_pathToImage = "/_img/loading_flash.gif";
var imgLoader;

var MINIWIDTH = 758;
var MINIHEIGHT = 776;

var MICROWIDTH = 758;
var MICROHEIGHT = 490;

// Custom mini player function
function openMini(videoId, customBigBoxZone, customPrerollAdTag) {
	var url = "/webtele/mini.jsp?videoId=" + videoId + "&TB_iframe=true&width=" + MINIWIDTH + "&height=" + MINIHEIGHT + "&modal=false"

	// Identify defined values (to allow blank or null values in parameters)
	var hasCustomBigBoxZone = false;
	var hasCustomPrerollAdTag = false;
	if (customBigBoxZone != null) {
		if (customBigBoxZone != "") {
			hasCustomBigBoxZone = true;
		}
	}
	if (customPrerollAdTag != null) {
		if (customPrerollAdTag != "") {
			hasCustomPrerollAdTag = true;
		}
	}

	// customBigBoxZone and customPrerollAdTag
	if (hasCustomBigBoxZone && hasCustomPrerollAdTag) {
		url = "/webtele/mini.jsp?videoId=" + videoId + "&customBigBoxZone=" + customBigBoxZone + "&customPrerollAdTag=" + customPrerollAdTag + "&TB_iframe=true&width=" + MINIWIDTH + "&height=" + MINIHEIGHT + "&modal=false";
	} else if (hasCustomBigBoxZone && !hasCustomPrerollAdTag) {
		// customBigBoxZone only
		url = "/webtele/mini.jsp?videoId=" + videoId + "&customBigBoxZone=" + customBigBoxZone + "&TB_iframe=true&width=" + MINIWIDTH + "&height=" + MINIHEIGHT + "&modal=false";
	} else if (!hasCustomBigBoxZone && hasCustomPrerollAdTag) {
		// customPrerollAdTag only
		url = "/webtele/mini.jsp?videoId=" + videoId + "&customPrerollAdTag=" + customPrerollAdTag + "&TB_iframe=true&width=" + MINIWIDTH + "&height=" + MINIHEIGHT + "&modal=false";
	}

	tb_show_webtele("", url, null);
}

// Custom micro player functions
function openMicroId(videoId) {
	var url = "/webtele/micro.jsp?videoId=" + videoId + "&TB_iframe=true&width=" + MICROWIDTH + "&height=" + MICROHEIGHT + "&modal=false";
	tb_show_webtele("", url, null);
}

function openMicroUrl(videoUrl) {
	var url = "/webtele/micro.jsp?videoUrl=" + videoUrl + "&TB_iframe=true&width=" + MICROWIDTH + "&height=" + MICROHEIGHT + "&modal=false";
	tb_show_webtele("", url, null);
}

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

function tb_show_webtele(caption, url, imageGroup) {//function called when the user clicks on a thickbox link

	// Go to top of page (seb)
	// This is actually done for users that have a resolution height less than that of the mini player
	// Combined with placing the thickbox at the top of page, having it's position fixed.
	window.scrollTo(0, 0);

	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect_webtele") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect_webtele'></iframe><div id='TB_overlay_webtele' onClick='tb_remove_webtele();'></div><div id='TB_window_webtele'></div>");
			}
		}else{//all others
			if(document.getElementById("TB_overlay_webtele") === null){
				$("body").append("<div id='TB_overlay_webtele' onClick='tb_remove_webtele();'></div><div id='TB_window_webtele'></div>");
			}
		}

		if(tb_detectMacXFF_webtele()){
			$("#TB_overlay_webtele").addClass("TB_overlayMacFFBGHack_webtele");//use png overlay so hide flash
		}else{
			$("#TB_overlay_webtele").addClass("TB_overlayBG_webtele");//use background and opacity
		}

		if(caption===null){caption="";}
		// Apparently we don't need the loader...
		
		// New one
		$("body").append("<div id='TB_load_webtele'><img src='"+imgLoader.src+"' width='18' height='18' border='0' alt='0'/></div>");//add loader to the page

		var baseURL;
		if(url.indexOf("?")!== -1){ //if there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
		} else {
	 		baseURL = url;
		}

		var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
		var urlType = baseURL.toLowerCase().match(urlString);

		if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images

			// We don't use mini to look at pictures.
			alert("We don't use mini to look at pictures.");

		}else{//code to show html

			var queryString = url.replace(/^[^\?]+\??/,'');
			var params = tb_parseQuery_webtele(queryString);

			TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
			TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
			ajaxContentW = TB_WIDTH - 30;
			ajaxContentH = TB_HEIGHT - 45;

			if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window
				urlNoQuery = url.split('TB_');
				$("#TB_iframeContent_webtele").remove();
				if(params['modal'] != "true"){//iframe no modal

					// WEBTELE MINI
					$("#TB_window_webtele").append("<div style='background: url(/_img/slug.gif); height: 25px;'><div style='padding: 4px 0 4px 0; margin-bottom: 1px; text-align:right; float:right;'><a href='#' title='' onClick='tb_remove_webtele();return false;'><img src='/webtele/_img/btn_fermer_deluxe.png' width='78' height='20' border='0' alt='' style='padding-right: 12px;'></a></div><br clear='all'/></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent_webtele' name='TB_iframeContent_webtele"+Math.round(Math.random()*1000)+"' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");

				}else{//iframe modal
					$("#TB_overlay_webtele").unbind();
					$("#TB_window_webtele").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent_webtele' name='TB_iframeContent_webtele"+Math.round(Math.random()*1000)+"' onload='tb_showIframe_webtele()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'> </iframe>");
				}
			}else{// not an iframe, ajax
				if($("#TB_window_webtele").css("display") != "block"){
					if(params['modal'] != "true"){//ajax no modal
						$("#TB_window_webtele").append("<div id='TB_title_webtele'><div id='TB_ajaxWindowTitle_webtele'>"+caption+"</div><div id='TB_closeAjaxWindow_webtele'><a href='#' id='TB_closeWindowButton_webtele'>fermer</a></div></div><div id='TB_ajaxContent_webtele' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
					}else{//ajax modal
						$("#TB_overlay_webtele").unbind();
						$("#TB_window_webtele").append("<div id='TB_ajaxContent_webtele' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
					}
				}else{//this means the window is already up, we are just loading new content via ajax
					$("#TB_ajaxContent_webtele")[0].style.width = ajaxContentW +"px";
					$("#TB_ajaxContent_webtele")[0].style.height = ajaxContentH +"px";
					$("#TB_ajaxContent_webtele")[0].scrollTop = 0;
					$("#TB_ajaxWindowTitle_webtele").html(caption);
				}
			}

			if(url.indexOf('TB_inline') != -1){
				$("#TB_ajaxContent_webtele").append($('#' + params['inlineId']).children());
				$("#TB_window_webtele").unload(function () {
					$('#' + params['inlineId']).append( $("#TB_ajaxContent_webtele").children() ); // move elements back when you're finished
				});
				tb_position_webtele();
				$("#TB_load_webtele").remove();
				$("#TB_window_webtele").css({display:"block"});
			}else if(url.indexOf('TB_iframe') != -1){

				// WEBTELE MINI
				tb_position_webtele();
				$("#TB_load_webtele").remove();
				$("#TB_window_webtele").css({display:"block"});

			}else{
				$("#TB_ajaxContent_webtele").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
					tb_position_webtele();
					$("#TB_load_webtele").remove();
					//tb_init("#TB_ajaxContent_webtele a.thickbox");
					$("#TB_window_webtele").css({display:"block"});
				});
			}

		}

		if(!params['modal']){
			document.onkeyup = function(e){
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove_webtele();
				}
			};
		}

	} catch(e) {
		//nothing here
	}
}

//helper functions below
function tb_showIframe_webtele(){
	// This should'nt be used
	$("#TB_load_webtele").remove();
	$("#TB_window_webtele").css({display:"block"});
}

function tb_remove_webtele() {
	$("#TB_window_webtele").css({display:"none"});
	$("#TB_load_webtele").css({display:"none"});
	$("#TB_overlay_webtele").css({display:"none"});
	$("#TB_window_webtele").css({display:"none"});
	$("#TB_HideSelect_webtele").css({display:"none"});
	$("#TB_window_webtele").remove();
	$("#TB_load_webtele").remove();
	$("#TB_overlay_webtele").remove();
	$("#TB_window_webtele").remove();
	$("#TB_HideSelect_webtele").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}

	return false;
}

function tb_position_webtele() {
	$("#TB_window_webtele").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#TB_window_webtele").css({marginTop: '-300px'});
	}
}

function tb_parseQuery_webtele(query) {
	var Params = {};
	if (!query) {
		return Params;
	}// return empty object
	var Pairs = query.split(/[;&]/);
	for ( var i = 0; i < Pairs.length; i++ ) {
		var KeyVal = Pairs[i].split('=');
		if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
			var key = unescape( KeyVal[0] );
			var val = unescape( KeyVal[1] );
			val = val.replace(/\+/g, ' ');
			Params[key] = val;
	}
	return Params;
}

function tb_getPageSize_webtele() {
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function tb_detectMacXFF_webtele() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}
