/* Multi Windows Thickbox by Vadim Derkach
 * Based on 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 = "/images/loadingAnimation.gif";
var tb_no=-1;
var tb_no1=-1;
var tb_stack=Array();
var tb_render=0;

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
$(document).ready(function(){

	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;

	imgBorderL = new Image();
	imgBorderL.src='/images/mod-l-t.gif';

	imgBorderR = new Image();
	imgBorderR.src='/images/mod-r-t.gif';

});

function showTbWindow(caption, url){
//	if (tb_render) return false;

	tb_iframe=1;
	if(tb_no<7){
		tb_render=1;
		tb_no++;
		tb_show(caption, url)
	} else {

		alert((lang!='ru')?'Cannot proceed. Too many interlnal windows. Please close some of them first.':'Невозможно выполнить операцию. Слишком много окон открыто. Пожалуйста, закройте некоторые из них сначала.')
	}

}
function tb_show(caption, url, imageGroup) {

	try {

		if(tb_no>0) {
		//	if (tb_render) return false;
			tb_no1=tb_no-1
			$("body").append("<div id='TB_window_over"+tb_no+"' class='TB_window_over' style='z-index:" +(302000+tb_no*50-10)+"'><img src='"+imgLoader.src+"' style='margin-top:40px'/><div class='tb_load_mes'>"+((lang!='ru')?'Press Esc to cancel this...':'Если вы хотите отменить загрузку, нажмите Esc...')+"</div></div>");
			$('#TB_window_over'+tb_no).css("left", $('#TB_window'+tb_no1).css("left"));
			$('#TB_window_over'+tb_no).css("top", (parseInt($('#TB_window'+tb_no1).css("top"))+27)+'px');
			$('#TB_window_over'+tb_no).css("height", parseInt($('#TB_iframeContent'+tb_no1).css("height"))+'px');
			$('#TB_window_over'+tb_no).css("width", $('#TB_iframeContent'+tb_no1).css("width"));
			$("#TB_window_over"+tb_no).css({display:"block"});
			$('.TB_b'+tb_no1).css("background-color","#8d8d8d");

		}

		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6

//			if(tb_no==0) {
//				$("body","html").css({height: "100%", width: "100%"});
//				$("html").css("overflow","hidden");
//			}
//			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
//				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window"+tb_no+"' class='TB_window' style='z-index:" +(302000+tb_no*50)+"'></div>");
//			} else {
//				$("body").append("<div id='TB_window"+tb_no+"' class='TB_window' style='z-index:" +(302000+tb_no*50)+"'></div>");
//			}


			if(document.getElementById("TB_overlay") === null){

				$("body").append("<div id='TB_overlay'></div><div id='TB_window"+tb_no+"' class='TB_window' style='z-index:" +(302000+tb_no*50)+"'></div>");
			} else {

				$("body").append("<div id='TB_window"+tb_no+"' class='TB_window' style='z-index:" +(302000+tb_no*50)+"'></div>");
			}

		}else{//all others

			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window"+tb_no+"' class='TB_window' style='z-index:" +(302000+tb_no*50)+"'></div>");
			} else {
				$("body").append("<div id='TB_window"+tb_no+"' class='TB_window' style='z-index:" +(302000+tb_no*50)+"'></div>");
			}
		}

		if(tb_no==0) {
			if(tb_detectMacXFF()){
				$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
			}else{
				$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
			}

			$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /><div class='tb_load_mes'>"+((lang!='ru')?'Press Esc to cancel this...':'Если вы хотите отменить загрузку, нажмите Esc...')+"</div></div>");//add loader to the page
			$('#TB_load').show();//show loader
		}

	   var baseURL;
	   if(url.indexOf("?")!==-1){ //ff there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
	   }else{
	   		baseURL = url;
	   }



		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = tb_parseQuery( queryString );

		TB_WIDTH = (params['width']*1-tb_no*10)  || 630; //defaults to 630 if no paramaters were added to URL
		TB_HEIGHT = (params['height']*1-tb_no*35 ) || 440; //defaults to 440 if no paramaters were added to URL

		var wwidth=TB_WIDTH;
		var wheight=TB_HEIGHT;

		urlNoQuery = url.split('TB_');
		$("#TB_iframeContent"+tb_no).remove();

		if(tb_no>0){

			$('#TB_mod_title_'+tb_no1).css('background-color','#4C5870');
			$('#TB_mod_title_'+tb_no1).css('background-image','url(/images/mod-title1.gif)');
			$('#TB_mod_title_'+tb_no1).css('color','#A0A0A0');

			$('#TB_mod1_'+tb_no1).css('background','url(images/mod-l-t1.gif) no-repeat');
			$('#TB_mod2_'+tb_no1).css('background','url(images/mod-r-t1.gif) no-repeat');
			$("#TB_closeWindowButtonS"+tb_no1).css('color','#A0A0A0');




		}
		if(lang=='ru'){
			var close_caption="Закрыть окно. Нажмите на эту иконку или нажмите на кнопку Esc."
			var close_button="закрыть"
		} else {
			var close_caption="Close. Press this icon or press Esc button";
			var close_button="close it"
		}
//48

		var s_content='<div style="width:'+wwidth+'px" id="TB_title'+tb_no+'"><div class="mod1" id="TB_mod1_'+tb_no+'"></div><div class="mod-title" id="TB_mod_title_'+tb_no+'" style="width:'+(wwidth-103)+'px" id="TB_ajaxWindowTitle'+tb_no+'">'+caption+'</div><div class="mod2" id="TB_mod2_'+tb_no+'"><a href="#" id="TB_closeWindowButton'+tb_no+'" title="Close"><img src="/images/spacer.gif" border=0 width=25 height=25 alt="'+close_caption+'"></a><a href="#" id="TB_closeWindowButtonS'+tb_no+'"  style="padding:7px 0 0 3px" title="Close">'+close_button+'</a></div></div><div class=mod style="height:'+(wheight-27+15)+'px"><iframe style="width:'+wwidth+'px;height:'+(wheight-27+15)+'px" frameBorder=0 hspace="0" src="'+urlNoQuery[0]+'&tbno='+tb_no+'" id="TB_iframeContent'+tb_no+'" name="TB_iframeContent'+Math.round(Math.random()*1000)+'" onload="tb_showIframe()"></iframe></div><div style="width:'+wwidth+'px" class="TB_footer"><div class="TB_b'+tb_no+'"  style="width:'+(wwidth-2)+'px;margin-left:1px;height:2px"></div><div class="TB_b'+tb_no+'"  style="width:'+(wwidth-4)+'px;margin-left:2px;height:2px"></div><div class="TB_b'+tb_no+'" style="width:'+(wwidth-6)+'px;margin-left:3px;height:1px"></div><div class="TB_b'+tb_no+'" style="width:'+(wwidth-8)+'px;margin-left:4px;height:1px"></div><div class="TB_b'+tb_no+'" style="width:'+(wwidth-10)+'px;margin-left:5px;height:1px"></div><div class="TB_b'+tb_no+'" style="width:'+(wwidth-12)+'px;margin-left:6px;height:1px"></div><div class="TB_b'+tb_no+'" style="width:'+(wwidth-14)+'px;margin-left:7px;height:1px"></div><div class="TB_b'+tb_no+'" style="width:'+(wwidth-18)+'px;margin-left:9px;height:1px"></div><div class="TB_b'+tb_no+'" style="width:'+(wwidth-22)+'px;margin-left:11px;height:1px"></div></div>';




		$("#TB_window"+tb_no).append(s_content);


		$("#TB_closeWindowButton"+tb_no).click(tb_remove);
		$("#TB_closeWindowButtonS"+tb_no).click(tb_remove);


		tb_position();

		if($.browser.safari){//safari needs help because it will not fire iframe onload
			if(tb_no==0) $("#TB_load").remove();
			$("#TB_window"+tb_no).css({display:"block"});
		}




		if(tb_no==0) {
			document.onkeyup = function(e){
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove();
				}
			};
		}



		if (typeof imgBorderL == "undefined"){

			imgBorderL1 = new Image();
			imgBorderL1.src='/images/mod-l-t1.gif';

			imgBorderR1 = new Image();
			imgBorderR1.src='/images/mod-r-t1.gif';

		}



	} catch(e) {
		//nothing here

	}
	tb_render=0;
}

//helper functions below
function tb_showIframe(){

	if(tb_no==0) $("#TB_load").remove();
	$('.TB_b'+tb_no).css("background-color","#fff");
	$("#TB_window"+tb_no).css({display:"block"});
	if(tb_no>0) {
		$('#TB_window_over'+tb_no).css('background-color','#4C5870');
		$('.TB_b'+tb_no1).css("background-color","#4C5870");

	}
}

function tb_remove() {

	$("#TB_closeWindowButton"+tb_no).unbind("click");
	$("#TB_closeWindowButtonS"+tb_no).unbind("click");
	if(tb_no==0){
		$('#TB_window'+tb_no+',#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
		$("#TB_load").remove();
		if (typeof document.body.style.maxHeight == "undefined" ) {//if IE 6
			$("body","html").css({height: "auto", width: "auto"});
			$("html").css("overflow","");
		}
		document.onkeydown = "";
		document.onkeyup = "";

	} else {
		$('#TB_window'+tb_no).remove();
		$('#TB_window_over'+tb_no).remove();
	}

	tb_no--;

	if(tb_no>-1){
		$('#TB_mod_title_'+tb_no).css('background-color','#627EBF');
		$('#TB_mod_title_'+tb_no).css('color','#fff');

		$('#TB_mod1_'+tb_no).css('background','url(images/mod-l-t.gif) no-repeat');
		$('#TB_mod2_'+tb_no).css('background','url(images/mod-r-t.gif) no-repeat');
		$('#TB_mod_title_'+tb_no).css('background-image','');
		$('.TB_b'+tb_no).css("background-color","#fff");
		$('#TB_closeWindowButtonS'+tb_no).css("color","#fff");

	}
	return false;
}

function tb_position() {

	var h=$(window).height();
	var w=$(window).width();

	$('#TB_window'+tb_no).css("left", (w-TB_WIDTH)/2+"px");
	$('#TB_window'+tb_no).css("top", ((h-TB_HEIGHT)/2+tb_no*14)+"px");

}

function tb_parseQuery ( 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_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}
