$(document).ready(function(){
	tb_init('a.thickbox, input.thickbox');

	$("#menu>li>a").mouseenter(function(){
		var t=$(this),p=t.parent().parent();
		if (!t.next("ul").hasClass("on")){
			t.next("ul").addClass("on").animate({'opacity':'1'},{queue:false,duration:300}).slideDown(300);
		}

		p.hover(function(){
		},function(){
			p.find("ul.on").animate({'opacity':'0'},{queue:false,duration:600}).slideUp(600).removeClass("on");
		});

	});
});

function navThrough(e){var l;if(!e)e=window.event;if((e.ctrlKey)&&((e.keyCode==0x25)||(e.keyCode==0x27))){switch(e.keyCode?e.keyCode:e.which?e.which:null){case 0x25:l=$("link[rel='prev']").attr("href");break;case 0x27:l=$("link[rel='next']").attr("href");break;}}if((l)&&(l.length>0)){document.location=l;}}
function returnVote(id){var n='/info.php?vote&vid='+id+'&vnum='+$("#voteselect").val()+'&height=70&width=300&keepThis=true&TB_iframe=true';$("#votesubmit").attr("alt",n);}

/*
* core Thickbox 3.1
*/

function tb_init(domChunk){
	$(domChunk).click(function(){
	var t=this.title||this.name||null,a=this.href||this.alt,g=this.rel||false;
	tb_show(t,a,g);
	this.blur();
	return false;
	});
}

function tb_show(caption,url,imageGroup){
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")===null) {
			$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
			$("#TB_overlay").click(tb_remove);
		}
	}else{//all others
		if(document.getElementById("TB_overlay") === null){
			$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
			$("#TB_overlay").click(tb_remove);
		}
	}
		
	if(tb_detectMacXFF()){
		$("#TB_overlay").addClass("TB_overlayMacFFBGHack");
	}else{
		$("#TB_overlay").addClass("TB_overlayBG").css('opacity','0').animate({opacity:"0.6"},{duration:500,queue:false});
	}
		
	if(caption===null){caption="";}
	$("body").append("<div id='TB_load'><img src='http://s.anime-dorama.ru/img/tpl/load.gif' /></div>");
	$('#TB_load').show();
		
	var baseURL;
	if(url.indexOf("?")!==-1){//query string
		baseURL = url.substr(0, url.indexOf("?"));
	}else{
		baseURL = url;
	}

	var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
	var urlType = baseURL.toLowerCase().match(urlString);
	   
	var queryString = url.replace(/^[^\?]+\??/,''),params = tb_parseQuery(queryString);

	TB_WIDTH = (params['width']*1) + 30 || 400;
	TB_HEIGHT = (params['height']*1) + 40 || 320;
	ajaxContentW = TB_WIDTH - 30;
	ajaxContentH = TB_HEIGHT - 45;

	if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){
		imgPreloader = new Image();
		imgPreloader.onload = function(){
			imgPreloader.onload = null;
				
			// Resizing large images
			var pagesize = tb_getPageSize();
			var x = pagesize[0] - 150;
			var y = pagesize[1] - 150;
			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;
			TB_HEIGHT = imageHeight;
			$("#TB_window").append("<a href='' id='TB_ImageOff'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>");
			
			$("#TB_closeWindowButton").click(tb_remove);

			tb_position();
			$("#TB_load").remove();
			$("#TB_ImageOff").click(tb_remove);
			$("#TB_window").css({display:"block"});
		};

		imgPreloader.src = url;

	} else {
		if(url.indexOf('TB_iframe') != -1){
			urlNoQuery = url.split('TB_');
			$("#TB_iframeContent").remove();
			if(params['modal'] != "true"){
				$("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
			}else{
				$("#TB_overlay").unbind();
				$("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'> </iframe>");
			}
		}else{
			if($("#TB_window").css("display") != "block"){
				if(params['modal'] != "true"){//ajax no modal
					if(params['notitle']=="true"){
						$("#TB_window").append("<div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
					}else{
						$("#TB_window").append("<div id='TB_title'><span id='#TB_ajaxTitle'>"+caption+"</span><a href='#' id='TB_closeButton'>закрыть</a></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
					}
				}else{//ajax modal
					$("#TB_overlay").unbind();
					$("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");	
				}
			}else{//window is already up, loading new content via ajax
				$("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
				$("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
				$("#TB_ajaxContent")[0].scrollTop = 0;
				$("#TB_ajaxTitle").html(caption);
			}
		}
					
		$("#TB_closeButton").click(tb_remove);
			
		if(url.indexOf('TB_inline') != -1){
			$("#TB_ajaxContent").append($('#' + params['inlineId']).children());
			$("#TB_window").unload(function () {
				$('#' + params['inlineId']).append( $("#TB_ajaxContent").children() );//move back
			});
			tb_position();
			$("#TB_load").remove();
			$("#TB_window").css({display:"block"}); 
		}else if(url.indexOf('TB_iframe') != -1){
			tb_position();
			if($.browser.safari){//safari needs help because it will not fire iframe onload
				$("#TB_load").remove();
				$("#TB_window").css({display:"block"});
			}
		}else{
			if(params['message']=="true"){
				$("#TB_ajaxContent").html('<p align="center">'+caption+'<p align="center"><a href="#" onClick="tb_remove()">Закрыть окно</a> <span style="color:#999">[Esc]</span>');
				tb_position();
				$("#TB_load").remove();
				tb_init("#TB_ajaxContent a.thickbox");
				$("#TB_window").css({display:"block"});
			} else {
				$("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
					tb_position();
					$("#TB_load").remove();
					tb_init("#TB_ajaxContent a.thickbox");
					$("#TB_window").css({display:"block"});
				});
			}
		}
	}
			
	if(!params['modal']){
		document.onkeyup = function(e){
			if(e==null){keycode = event.keyCode;}else{keycode=e.which;}
			if(keycode == 27){ // close
				tb_remove();
				if(typeof window['die']!=='undefined'){die=1;}
			}
		};
	}
} catch(e) {
	//nothing here
}
}

//functions

function tb_showIframe(){$("#TB_load").remove();$("#TB_window").css({display:"block"});}
function tb_remove(){$("#TB_closeButton").unbind("click");$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();$("#TB_load").remove();if(typeof document.body.style.maxHeight=="undefined"){$("body","html").css({height:"auto",width:"auto"});$("html").css("overflow","");}document.onkeydown="";document.onkeyup="";return false;}
function tb_position(){$("#TB_window").css({marginLeft:'-'+parseInt((TB_WIDTH/2),10)+'px', width:TB_WIDTH+'px'});if(!(jQuery.browser.msie&&jQuery.browser.version<7)){$("#TB_window").css({marginTop:'-'+parseInt((TB_HEIGHT/2),10)+'px'});}}
function tb_parseQuery(query){var P={};if (!query){return P;}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]),val=unescape(KeyVal[1]);val=val.replace(/\+/g,' ');P[key]=val;}return P;}
function tb_getPageSize(){var de=document.documentElement,w=window.innerWidth||self.innerWidth||(de&&de.clientWidth)||document.body.clientWidth,h=window.innerHeight||self.innerHeight||(de&&de.clientHeight)||document.body.clientHeight;aps=[w,h];return aps;}
function tb_detectMacXFF(){var ua=navigator.userAgent.toLowerCase();if (ua.indexOf('mac')!=-1&&ua.indexOf('firefox')!=-1){return true;}}
