$(document).ready(function(){
	$('.bg_l,.bg_top').css('background-position',-346+$('.bg_l').width()+'px 0px');
	$(window).resize(function(){$('.bg_l,.bg_top').css('background-position',-346+$('.bg_l').width()+'px 0px');});
	$('#menu_1').hover(function(){$('#menu_1a').css('display','');},function(){$('#menu_1a').css('display','none');});
	$('#menu_2').hover(function(){$('#menu_2a').css('display','');},function(){$('#menu_2a').css('display','none');});
	$('#menu_3').hover(function(){$('#menu_3a').css('display','');},function(){$('#menu_3a').css('display','none');});
	$('#menu_4').hover(function(){$('#menu_4a').css('display','');},function(){$('#menu_4a').css('display','none');});
	$('#menu_5').hover(function(){$('#menu_5a').css('display','');},function(){$('#menu_5a').css('display','none');});
	$('#menu_6').hover(function(){$('#menu_6a').css('display','');},function(){$('#menu_6a').css('display','none');});
	$('#menu_7').hover(function(){$('#menu_7a').css('display','');},function(){$('#menu_7a').css('display','none');});
});

var _li_flag=false,_li_info=null,_li_timer=0;
function load_image(src,onload,onabort){if(_li_timer) clearTimeout(_li_timer);if(_li_flag){try{if(_li_info._i.complete)_li_info._onload();else _li_info._onabort();}catch(e){}}else{}var i=new Image();i.src=src;_li_flag=true;_li_info={_onload: onload,_onabort: onabort,_i:i};_li_timer=setTimeout("_load_image_timer()",100);}
function _load_image_timer(){if(_li_info._i.complete){_li_info._onload();_li_flag=false;_li_timer=0;}else _li_timer=setTimeout("_load_image_timer()",100);}

function img_size(w,h,new_w,new_h){
	if(new_w==0){
		ratioHeight=h/new_h;
		destWidth=w/ratioHeight;
		destHeight=new_h;
	}
	else if(new_h==0){
		ratioWidth=w/new_w;
		destHeight=h/ratioWidth;
		destWidth=new_w;
	}
	else{
		ratioWidth=w/new_w;
		ratioHeight=h/new_h;
		if(ratioWidth<ratioHeight){
			destWidth=w/ratioHeight;
			destHeight=new_h;
		}
		else{
			destHeight=h/ratioWidth;
			destWidth=new_w;
		}
	}
	return [destWidth,destHeight];
}

function pop_up(id,w,h,folder){
	if(typeof(folder)=='undefined') folder='work_gal';
	var t_id=id;
	var t_w=w;
	var t_h=h;
	$(window).resize(function(){pop_up(t_id,t_w,t_h,folder);});
	var w_height=$(window).height();
	var w_width=$(window).width();
	var height;
	var txt=$('#sm_'+id).html();
	$('#popup_txt').html(txt);
	$('#popup_tb').css('top','0');
	$('#popup_bg').css({display:'',opacity:0.5});
	$('#popup').css({display:''});
	if($('#popup_txt').height()==0){
		$('#popup_tb_txt').css('display','none');
		var txt_h=0;
	}
	else{
		txt_h=$('#popup_txt').height()+7*2;
	}
	
	if(w+46>w_width||h+46>w_height){
		size=img_size(w,h,w_width-46,w_height-46);
		w=size[0];h=size[1];
	}
	
	$('#popup_img').css({width:w,height:h});
	$('#popup_img').html('Подождите... Идет загрузка');
	load_image('/u/'+folder+'/big/'+id+'.jpg',function(){
		$('#popup_img').html('<img src="/u/'+folder+'/big/'+id+'.jpg" width="'+w+'" height="'+h+'">');
	},function(){
		$('#popup_img').html('Произошла ошибка загрузки!');
	});
	
	temp=h+txt_h+17*2+12;
	if(w_height<temp){
		size=img_size(w,h,0,w_height-txt_h-17*2-12);
		w=size[0];h=size[1];
		$('#popup_img, #popup_pod').width(w);
		$('#popup_img').height(h);
		txt_h=$('#popup_txt').height()+7*2;
	}
	else{
		$('#popup_pod').width(w);
	}
	
	temp=h+txt_h+17*2+70*2+12;
	if(w_height<temp){
		$('#popup_tb').css('top',-(temp-w_height)/2);
	}
	$('#popup_lt,#popup_rt').height(h+12+17*2+txt_h);
	$(document).keydown(function(e){
		if(!e)e=window.event;
		var code=e.keyCode;
		if(code==27) pop_up_close()
	});
}

function pop_up_close(){
	$('#popup_bg, #popup').css({display:'none'});
	$('#popup_img')[0].src='/file/x.gif';
	$(document).unbind('keydown');
	$(window).unbind('resize');
	$(window).resize(function(){$('.bg_l,.bg_top').css('background-position',-346+$('.bg_l').width()+'px 0px');});
}

function fixPNG(element,crop){
	if(navigator.appName=="Microsoft Internet Explorer"){
		var src;
		if (element.tagName=='IMG'){
			if (/\.png$/.test(element.src)){
				src = element.src;
				element.src = "/file/x.gif"; 
			}
			if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='crop')";
		}
		else{
			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
			if (src){
				src = src[1]; 
				element.runtimeStyle.backgroundImage="none"; 
			}
			if(typeof(crop)!='undefined'&&crop){if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='crop')";}
			else{if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";}
		}
	}
}
