/*
 * Slightly Thickerbox 1.7
 * By Jason Levine (http://www.jasons-toolbox.com)
 * A modification of Thickbox by Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 * Thickbox is built on top of the very light weight jquery library.
 */

//on page load call TB_init
$(document).ready(TB_init);

var TB_NextObjToShow, TB_NextDirection, TB_WIDTH = 0, TB_HEIGHT = 0, TB_VisibleSelects, TB_WasOpen;

//add thickbox to href elements that have a class of .thickbox
function TB_init(){
	$("a.thickbox").click(function(){
		//var t = this.title || this.innerHTML || this.href;
		//TB_show(t,this.href);
		TB_ShowObj(this);
		this.blur();
		return false;
	});
	TB_WasOpen = true;
}

function TB_getPrevObj(ThickObj) {
	var PrevObj = null;
	var url = ThickObj.href;
	var thickgroup = ThickObj.rel;
	
	if (thickgroup != "") {	
		TB_ObjSet = $("a.thickbox[@rel=" + thickgroup + "]")
		TB_ObjSize = TB_ObjSet.size();
		for (var TB_Counter = 0; TB_Counter < TB_ObjSize; TB_Counter++) {
			if (TB_ObjSet.get(TB_Counter) == url) {
				TB_Counter = TB_ObjSize + 1;  // Exit out of the loop
			} else {
				PrevObj = TB_ObjSet.get(TB_Counter);
			}
		}
	}
	
	return PrevObj;
}

function TB_getNextObj(ThickObj) {
	var NextObj = null;
	var url = ThickObj.href;
	var thickgroup = ThickObj.rel;
	var FoundThickObj = 0;
	
	if (thickgroup != "") {	
		TB_ObjSet = $("a.thickbox[@rel=" + thickgroup + "]")
		TB_ObjSize = TB_ObjSet.size();
		for (var TB_Counter = 0; TB_Counter < TB_ObjSize; TB_Counter++) {
			if (TB_ObjSet.get(TB_Counter) == url) {
				FoundThickObj = 1;
			} else {
				if (FoundThickObj == 1) {
					NextObj = TB_ObjSet.get(TB_Counter);
					TB_Counter = TB_ObjSize + 1;  // Exit out of the loop
				}
			}
		}
	}
	
	return NextObj;
}


function TB_ShowObj(ThickObj) {
	var caption, url, thickgroup;
	try {
		caption = ThickObj.title || ThickObj.name || "";
		url = ThickObj.href;
		thickgroup = ThickObj.rel;
	
		if (document.getElementById("TB_overlay") == null) {
			$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
			$("#TB_overlay").click(TB_remove);
		}
		$(window).resize(TB_position);
		$(window).scroll(TB_position);
		
		TB_PrevObj = TB_getPrevObj(ThickObj);
		if (TB_PrevObj != null) {
			TB_PrevHTML = "<a href='#prev'  id='TB_prev'><img src=/images/tb_left.gif></a>";
		} else {
			TB_PrevHTML = " ";				
		}
		TB_NextObj = TB_getNextObj(ThickObj);
		if (TB_NextObj != null) {
			TB_NextHTML = "<a href='#next' id='TB_next'><img src=/images/tb_right.gif></a>";
			TB_NextHTMLstart = "<a href='#next' id='TB_next1'>";
			TB_NextHTMLend = "</a>";
		} else {
			TB_NextHTML = "<a href='#close' id='#TB_closeWindowButton1'>";	
			TB_NextHTMLstart = "";
			TB_NextHTMLend = "</a>";
		}
 		
		//$("#TB_overlay").show();
		$("body").append("<div id='TB_load'><div id='TB_loadContent'><img src='/images/circle_animation.gif' /></div></div>");
		var urlString = /\.jpg|\.jpeg|\.flv|\.html|\.htm|\.php/g;
		var urlType = url.toLowerCase().match(urlString) + '';
		switch (urlType) {
			case ".jpg":
			case ".jpeg":
				var imgPreloader = new Image();
				imgPreloader.onload = function(){
					// Resizing large images added by Christian Montoya
					var pagesize = getPageSize();
					var x = pagesize[0] - 30;
					var y = pagesize[1] - 50;
					var imageWidth = imgPreloader.width;
					var imageHeight = imgPreloader.height;
					if (imageWidth > x) {
						imageHeight = imageHeight * (x / imageWidth); 
						imageWidth = x; 
						if (imageHeight > y) { 
							imageWidth = imageWidth * (y / imageHeight); 
							imageHeight = y; 
						}
					} else {
						if (imageHeight > y) { 
							imageWidth = imageWidth * (y / imageHeight); 
							imageHeight = y; 
							if (imageWidth > x) { 
								imageHeight = imageHeight * (x / imageWidth); 
								imageWidth = x;
							}
						}
					}
					// End Resizing
					TB_WIDTH = imageWidth + 12;
					TB_HEIGHT = imageHeight + 37;
					$("#TB_window").append("<table id='TB_head'><tr><td>Фото "+caption+"</td><td width=14>" + TB_PrevHTML + "</td><td width=14>"+ TB_NextHTML + "</td><td align=right width=14><a href='#close' id='TB_closeWindowButton'><img src=/images/tb_close.gif></a></td></tr></table><div id='TB_ImageDIV'>" + TB_NextHTMLstart + "<img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"' class=tb_image onload='TB_ShowBox();'/>" + TB_NextHTMLend + "</div>");

					$("#TB_closeWindowButton").click(TB_remove);
					$("#TB_closeWindowButton1").click(TB_remove);
					if (!(TB_PrevHTML == "")) {
						$("#TB_prev").click(function () {
							TB_HideBox("r", TB_PrevObj);
						});
					}
					if (!(TB_NextHTML == "")) {
						$("#TB_next").click(function () {
							TB_HideBox("l", TB_NextObj);
						});
					}
					if (!(TB_NextHTML == "")) {
						$("#TB_next1").click(function () {
							TB_HideBox("l", TB_NextObj);
						});
					}
					TB_position();
					$("#TB_load").remove();
					$("#TB_ImageOff").click(TB_remove);
				}
		  
				imgPreloader.src = url;
				break;
			case ".flv":
				var queryString = url.replace(/^[^\?]+\??/,'');
				var params = parseQuery( queryString );
				TB_WIDTH = (320*1) + 13;
				TB_HEIGHT = (240*1) + 37;
				ajaxContentW = TB_WIDTH - 13;
				ajaxContentH = TB_HEIGHT - 37;
				paramss=params['file'];
				//paramss="dataimages/serv_corporative_1_video_18431822072008";
//				$("#TB_window").append("<div id='TB_caption'>"+caption+"</div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton'>close</a></div><div id='TB_SecondLine'>" + TB_PrevHTML + TB_NextHTML + "</div><div id='TB_ImageDIV' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'><a href='' id='TB_ImageOff' title='Close'><embed id='TB_Movie' src='" + url + "' autostart='true'></embed></div>");
	var o = {};
			o.aligh = 'middle';
			o.bgcolor = '#085d05';
			o.width = '320';
			o.height = '240';
			o.scale='showall';
			o.id = 'flash_stadium_def';
			o.name = '/mediaplayer.swf'
			o.flashvars='width=320&height=240&file=/'+paramss+'.flv&image=/'+paramss+'.jpg';
			o.quality = 'best';
			o.saligh = '';
			o.ver='9';
			video=showFlashforvideo(o);
				$("#TB_window").append("<table id='TB_head'><tr><td>Видео "+caption+"</td><td width=14>" + TB_PrevHTML + "</td><td width=14>"+ TB_NextHTML + "</td><td align=right width=14><a href='#close' id='TB_closeWindowButton'><img src=/images/tb_close.gif></a></td></tr></table><div id='TB_ImageDIV' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'><div class=tb_image>"+video+"</div></div>");
//					$("#TB_window").append("<table id='TB_head'><tr><td>"+caption+"</td><td width=14>" + TB_PrevHTML + "</td><td width=14>"+ TB_NextHTML + "</td><td align=right width=14><a href='#' id='TB_closeWindowButton'><img src=images/tb_close.gif></a></td></tr></table><div id='TB_ImageDIV'>" + TB_NextHTMLstart + "<img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"' class=tb_image onload='TB_ShowBox();'/>" + TB_NextHTMLend + "</div>");
				$("#TB_closeWindowButton").click(TB_remove);
				if (!(TB_PrevHTML == "")) {
					$("#TB_prev").click(function () {
						TB_HideBox("r", TB_PrevObj);
					});
				}
				if (!(TB_NextHTML == "")) {
					$("#TB_next").click(function () {
						TB_HideBox("l", TB_NextObj);
					});
				}
				TB_position();
				$("#TB_load").remove();
				$("#TB_ImageOff").click(TB_remove);
				TB_ShowBox();
				break;
			default:
				var queryString = url.replace(/^[^\?]+\??/,'');
				var params = parseQuery( queryString );
				width=params['width'];
				height=params['height'];
				if (width==undefined)
				{
					width=960-2;
					width=670-2;
				}
				if (height==undefined)
				{
					height=620-38;
					height=540-38;
				}

				TB_WIDTH = (width*1) + 2;
				TB_HEIGHT = (height*1) + 38;
				ajaxContentW = TB_WIDTH - 2-16;
				ajaxContentH = TB_HEIGHT - 38;
//				$("#TB_window").append("window.php?link=<div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a></div><div id='TB_SecondLine'>" + TB_PrevHTML + TB_NextHTML + "</div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
//				<div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>
				$("#TB_window").append("<table id='TB_head'><tr><td>"+caption+"</td><td width=14>" + TB_PrevHTML + "</td><td width=14>"+ TB_NextHTML + "</td><td align=right width=14><a href='#close' id='TB_closeWindowButton'><img src=/images/tb_close.gif></a></td></tr></table><div style='padding-top:5px; margin-left:10px' ><iframe src="+url+"?nodesign width="+ajaxContentW+" height="+ajaxContentH+" border=0 style='border: 0px; '></iframe></div>");
				
				$("#TB_closeWindowButton").click(TB_remove);
				TB_position();
					if (!(TB_PrevHTML == "")) {
					$("#TB_prev").click(function () {
						TB_HideBox("r", TB_PrevObj);
					});
				}
				if (!(TB_NextHTML == "")) {
					$("#TB_next").click(function () {
						TB_HideBox("l", TB_NextObj);
					});
				}

				$("#TB_load").remove();
				$("#TB_ImageOff").click(TB_remove);
				
				TB_ShowBox();
				

				break;
		}
	} catch(e) {
		alert( e );
	}
}

function TB_ShowBox(Direction) {
	$("#TB_overlay").show();
	TB_VisibleSelects = $("select:visible");
	TB_VisibleSelects.toggle();
	if ($().DropInLeft != undefined) {
		// Interface Elements for JQuery are included
		if (Direction == undefined) {
			if (TB_NextDirection == undefined) {
				Direction = "l";
			} else {
				Direction = TB_NextDirection;
			}
		}
		if (TB_WasOpen) {
			if (Direction.toLowerCase() == "r") {
				$("#TB_window").DropInRight(250);
			} else {
				$("#TB_window").DropInLeft(250);
			}
		} else {
			$("#TB_window").Grow(150);
			TB_WasOpen = true;
		}
	} else {
		// Interface Elements for JQuery are not included
		$("#TB_overlay").show();
		$("#TB_window").slideDown("normal");
		TB_WasOpen = true;
	}
}

function TB_AnimationComplete() {
//		$("#TB_overlay").show();
		$("#TB_window").fxReset();
}

function TB_HideBox(Direction, LocalNextObjToShow) {
	TB_NextObjToShow = LocalNextObjToShow;
	if ($().DropOutRight != undefined) {
		// Interface Elements for JQuery are included
		if (Direction == undefined) {
			Direction = "l";
		}
		//toggle  slideToggle fadeOut
		TB_NextDirection = Direction;
		if (Direction.toLowerCase() == "r") {
			$("#TB_window").toggle(300, function() {
				TB_HideBox_Part2();
			});

		} else {
			$("#TB_window").toggle(300, function() {
				TB_HideBox_Part2();
			});
		}
	} else {
		// Interface Elements for JQuery are not included
		$("#TB_window").slideUp("slow");
		TB_HideBox_Part2();
	}
}

function TB_HideBox_Part2() {
	$("#TB_load").remove();
	$("#TB_window").remove();
	$("body").append("<div id='TB_window'></div>");
	TB_ShowObj(TB_NextObjToShow);
}

//helper functions below

function TB_remove() {
	if ($().Shrink != undefined) {
		// Interface Elements for JQuery are included
		//$("#TB_window").Fold(500, 20, function() {
		//	$('#TB_window,#TB_overlay').remove();
		//	$("#TB_load").remove();
		//	TB_VisibleSelects.toggle();
		//	TB_WasOpen = false;
		//	return false;
		//});

		$("#TB_window,#TB_overlay").Shrink(500, function() {
			$('#TB_window,#TB_overlay').remove();
			$("#TB_load").remove();
			TB_VisibleSelects.toggle();
			//TB_WasOpen = false;
			TB_WasOpen = true;
			return false;
		});
	} else {
		// Interface Elements for JQuery are not included
		$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay').remove();});
		$("#TB_load").remove();
		TB_VisibleSelects.toggle();
		//TB_WasOpen = false;
		TB_WasOpen = true;
		return false;
	
	//TB_WasOpen = false;
	TB_WasOpen = true;
	alert(TB_WasOpen);
	}
}

function TB_position() {
	var pagesize = getPageSize();
  
  	if (window.innerHeight && window.scrollMaxY) {	
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
  	}
	
	var arrayPageScroll = getPageScrollTop();
	
	$("#TB_window").css({width:TB_WIDTH+"px",height:TB_HEIGHT+"px",
	left: ((pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-TB_HEIGHT)/2))+"px" });
	$("#TB_overlay").css("height",yScroll +"px");

}

function parseQuery ( query ) {
   var Params = new Object ();
   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 getPageScrollTop(){
	var yScrolltop;
	if (self.pageYOffset) {
		yScrolltop = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScrolltop) 
	return arrayPageScroll;
}

function getPageSize(){
	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 = new Array(w,h) 
	return arrayPageSize;
}
