arr=[];
arr['email']=false;
arr['name']=false;
arr['theme']=false;
arr['text']=false;



old='';
w=0;
h=0;
urli=0;
idi=0;
obj_on=false;
target='';
view_on=false;

this.belfonold = function(mw,mh,dw,dh,callback)
{
		$('#fonIIold').css({display: 'block'}).animate({width: mw+20,height: mh+20,	top:((dh-mh+20)/2-20)+$(window).scrollTop(), left:((dw-mw+20)/2-20)+$(window).scrollLeft()},'slow',callback);

}

this.form = function(){
	$("#admcont").click(function () {
		$('#targ').val('adminfo');
			size = viewport();
		w = document.body.offsetWidth;
		h = document.body.offsetHeight;
		dw = $(window).width();
		dh = $(window).height();
		var selector1 = $("#"+this.id);

		$('#dispold').css({		top:0, 
						left:0,
						width: size[0],
						height: size[1]
				});
		$('#dispold').fadeIn();
		$('#fonnerold').css('opacity',0.6);

			$('#preI').css('display','none');
			w=$('#formJQ').width();
			h=$('#formJQ').height();
mt=(dh-h)/2;
ml=(dw-w)/2;
					mt += $(window).scrollTop();
					ml += $(window).scrollLeft();
		belfonold(w,h,dw,dh,function (){
			$('#formJQ').css({left:ml, top:mt}).fadeIn('slow');
});


});				
	$("#cont").click(function(){
			size = viewport();
		w = document.body.offsetWidth;
		h = document.body.offsetHeight;
		dw = $(window).width();
		dh = $(window).height();
		$('#targ').val('info');
		var selector1 = $("#"+this.id);

		$('#dispold').css({		top:0, 
						left:0,
						width: size[0],
						height: size[1]
				});
		$('#dispold').fadeIn();
		$('#fonnerold').css('opacity',0.6);

			$('#preI').css('display','none');
			w=$('#formJQ').width();
			h=$('#formJQ').height();
mt=(dh-h)/2;
ml=(dw-w)/2;
					mt += $(window).scrollTop();
					ml += $(window).scrollLeft();
		belfonold(w,h,dw,dh,function (){
			$('#formJQ').css({left:ml, top:mt}).fadeIn('slow');
});

		
    });



	$("#fonnerold,#fonIold,#closeForm").click(function(){
		$('#dispold').fadeOut('fast');
		$('#preIold').fadeIn('slow');
		$('#formJQ').css({display: 'none'});
//		$('#fonner').css('display','none');
//		$('#fonner').css('opacity',0.6);		
    });				
};
$(function () {	
	$('.dropdown').each(function () {
		$(this).parent().eq(0).hoverIntent({
			timeout: 100,
			over: function () {
				var current = $('.dropdown:eq(0)', this);
				current.slideDown(500);
			},
			out: function () {
				var current = $('.dropdown:eq(0)', this);
				current.fadeOut(300);
			}
		});
	});
	
	$('.menu_items').hover(function () {
		$(this).stop(true).animate(
{
paddingTop: '5',
paddingLeft: '10'
}, 300);
	}, function () {
		$(this).stop(true).animate(
{
paddingTop: '0',
paddingLeft: '0'
}, 300);
	});
});

this.tooltip = function(){
//	$('body').append('<input type="text" id="left" value="LEFT" style="position:fixed;top:0px;left:0px;"><input type="text" id="top" value="TOP" style="position:fixed;top:25px;left:0px;">');		
		xOffset = -20;
		yOffset = -100;				
	$("div.itooltip,div.ptooltip").hover(function(event){
	$("#tooltip").remove();
	$("#pretip").remove();		
            if (!event) event = window.event;
            var target = (event.target) ?
                         event.target : event.srcElement;										  
		title = $('#'+this.id).attr('rel');
		var selector = $("#"+this.id);
		var selector1 = $("#"+target.id);
		var pos = selector.position();
//		$(e.target).css("border", "4px red solid");
//		$(e.target).css("position", "absolute");									  
		selector.prepend("<div style=\"display:block;\" id='pretip'><div id='tooltip'>"+ title +"</div></div>");
		$("#tooltip").css({top:'-100px',left:'0px'})
			.fadeIn("fast").css('display','block');


/*		var offset = $('#tooltip').offset();
		var tLeft = offset.left;
		var tTop = offset.top;
		$('#left').val('LEFT: ' + (selector1.position().left) + 'LEFT tt: ' + tLeft);
		$('#top').val('TOP: ' + ((selector1.position().top) -100) + 'TOP tt: ' + tTop);*/	
    },
	function(e){
//		$(e.target).css("border", "0px red solid");			
		$("#tooltip").fadeOut("fast");

    });				
};


this.view = function(obj,color)
{
if(!obj)
obj='div.itooltip img';

if(!color)
color='#62573a';

if(!view_on)
{

//Создаем базис(канонический) просмотрщика(
$('body').prepend('<div id="disp" style="display:none;position:absolute;width:100%;height:100%;top:0;left:0;z-index:9999;"></div>');
$('#disp').append('<div id="disp_wrap" style="display:block;position:absolute;top:0;left:0;z-index:2;"></div>');


$('#disp_wrap').append('<div id="disp_cont" style="display:block;position:absolute;top:0;left:0;z-index:2;"></div>');
$('#disp_wrap').append('<div id="disp_nav" style=""></div>');
$('#disp_wrap').append('<div id="close" style="display:block;background:url(disp/delete.png) right top no-repeat;position:absolute;right:0px;top:0px;cursor:pointer;height:24px;width:24px;z-index:6;"></div>');


$('#disp_nav').append('<div id="nav_left" style="display:block;background:url(disp/left_arrow.png) left bottom no-repeat;position:absolute;left:5px;bottom:10px;cursor:pointer;height:100%;width:35%;z-index:5;border:0px red solid;"></div>');
$('#disp_nav').append('<div id="nav_right" style="display:block;background:url(disp/right_arrow.png) right bottom no-repeat;position:absolute;right:5px;bottom:10px;cursor:pointer;height:100%;width:35%;z-index:5;border:0px green solid;"></div>');




$('#disp').append('<img style="top:50%;left:50%;display:none;position:absolute;z-index:4;" src="disp/loadingAnimation.gif" id="preI">');

$('#disp').append('<div id="fonII" style="display:none;position:absolute;left:0px;top:0px;width:1px;height:1px;background:white;z-index:1;"></div>');
$('#disp').append('<div id="fonner" style="display:block;position:absolute;left:0px;top:0px;width:100%;height:100%;background:'+color+';z-index:0;"></div>');

	$('#fonner').css('opacity',0.6);
/////////////////////////////////////////)


//Инициализируем закрытие просмотрщика по нажатию (
	$("#fonner,#close").expire().livequery('click',function(){
	obj_on=false;
		$('#disp').fadeOut('fast');
		$('#preI').css({display: 'none'});
		$(idi).css({display: 'none'});
		$('#fonI').remove();
//		alert('From remove: '+obj+'\n || '+urli+'\n || '+$('#fonI').attr('src')+'\n || '+w+'\n || '+h+'\n ||css w:'+$('#fonI').css('width')+'\n ||css h:'+$('#fonI').css('height'));
    });	
//////////////////////////////////////////////////)



	
//Меняем размер при скролле и ресайзе окна браузера(
       $(window).scroll(function () {
if(obj_on=='div_cont')
	size_maker(w,h,idi,false);
else if(obj_on=='img')
	size_maker(w,h,'#disp_wrap',true);
       });

	$(window).resize(function (){
if(obj_on=='div_cont')
	size_maker(w,h,idi,false);
else if(obj_on=='img')
	size_maker(w,h,'#disp_wrap',true);
        });
///////////////////////////////////////////////////)	

}
view_on=true;


	$(obj).expire().livequery('click',function(event)
	{
            if (!event) event = window.event;
            target = (event.target) ?
                         event.target : event.srcElement;


		
			size = viewport();
		dw = $(window).width();
		dh = $(window).height();
		urli = $(target).attr('rel');
		$("#tooltip").fadeOut("fast");
		$('#disp').css({		
						top:0, 
						left:0,
						width: size[0],
						height: size[1]
				});
		$('#preI').css({		top:(dh-13)/2+$(window).scrollTop(), 
						left:(dw-208)/2+$(window).scrollLeft(),
						display:'block'
				});
		$('#disp').fadeIn();


if(urli.indexOf('#')==-1)
  {
	 if($(obj).filter(':eq('+($(obj).index($(target))-1)+')').size()!=0)
		$('#nav_left').css({'display':'block'});
	 else
		$('#nav_left').css({'display':'none'});
		
	 if($(obj).filter(':eq('+($(obj).index($(target))+1)+')').size()!=0)
		$('#nav_right').css({'display':'block'});
	 else
		$('#nav_right').css({'display':'none'});

		$('#fonI').fadeOut('fast');
		$('#fonI').unbind('load').remove();
		$('#disp_cont').prepend("<img style='top:0px;left:0px;display:none;position:absolute;' src='' id='fonI'>");

	$('#fonI').load(function()
    {
			$('#preI').css('display','none');
			w=$('#fonI').width();
			h=$('#fonI').height();
			obj_on='img';
			
//			alert('From load: '+obj+'\n || '+urli+'\n || '+$('#fonI').attr('src')+'\n || '+w+'\n || '+h+'\n ||css w:'+$('#fonI').css('width')+'\n ||css h:'+$('#fonI').css('height'));
			
			size_maker(w,h,'#disp_wrap',true, function (){
//Инициализируем навигаю назад (
	$("#nav_left").expire().livequery('click',function(){
	 if($(obj).filter(':eq('+($(obj).index($(target))-1)+')').size()!=0)
	 {
	 $("#nav_left").expire();
			obj_on=false;
		$(obj).filter(':eq('+($(obj).index($(target))-1)+')').click();
	 }
    });	
///////////////////////////////)

//Инициализируем навигаю вперед (
	$("#nav_right").expire().livequery('click',function(){
	 if($(obj).filter(':eq('+($(obj).index($(target))+1)+')').size()!=0)
	 {
	 $("#nav_right").expire();
			obj_on=false;
		$(obj).filter(':eq('+($(obj).index($(target))+1)+')').click();
	 }
    });	
/////////////////////////////////)
			
			});
			

//alert('||'+w+'\n ||'+h+'\n ||'+$('#fonI').width()+'\n ||'+$('#fonI').height());

    });

		$('#fonI').attr('src',urli);
  }
 else
  {
obj_on='div_cont';
	$('#preI').css('display','none');
			w=$(urli).width();
			h=$(urli).height();
	$(urli).css({position:'absolute','z-index':10000});
idi = urli;
size_maker(w,h,urli,false);
  }

		
	});	
};


////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
this.size_maker = function(ws,hs,obj,resize,callback)
{
//alert('From size_maker : '+obj+'\n || '+ws+'\n || '+hs+'\n ||css w:'+$('#fonI').css('width')+'\n ||css h:'+$('#fonI').css('height'));
/*$('#wid').val(w);
$('#hei').val(h);
$('#widI').val($(obj).width());
$('#heiI').val($(obj).height());
$('#widIn').val($(obj).innerWidth());
$('#heiIn').val($(obj).innerHeight());*/
		dw = $(window).width();
		dh = $(window).height();
if(hs>=dh)
{  
mh=dh-50; 
mw=(ws/(hs/(mh))); 
mt=(dh-mh)/2;
ml=(dw-mw)/2;
if(mw>=dw)
 {
mw=dw-50;
mh=(hs/(ws/mw));
mt=(dh-mh)/2;
ml=(dw-mw)/2;
 }
}
else
{
mh=hs;
mw=ws;
mt=(dh-mh)/2;
ml=(dw-mw)/2;
if(mw>=dw)
 {
mw=dw-50;
mh=(hs/(ws/mw));
mt=(dh-mh)/2;
ml=(dw-mw)/2;
 }
}
if(!resize)
{
mh=hs;
mw=ws;
mt=(dh-mh)/2;
ml=(dw-mw)/2;
}
					mt += $(window).scrollTop();
					ml += $(window).scrollLeft();
					
			
			$(obj).fadeOut('fast',function() {
		belfon(mw,mh,dw,dh,function (){
if(obj=='#disp_wrap')
{
	$('#disp_wrap').css({left:ml, top:mt, width: mw, height:mh}).fadeIn('fast');
	$('#fonI').css({width: mw, height:mh,display:'block'});
}
else if(obj_on=='div_cont')
	$(obj).css({left:ml, top:mt, width: mw, height:mh}).fadeIn();

//	alert('From belfon: '+obj+'\n || '+w+'\n || '+h+'\n ||css w:'+$('#fonI').css('width')+'\n ||css h:'+$('#fonI').css('height'));
		});
//alert('From size_maker after: '+obj+'\n || '+w+'\n || '+h+'\n ||css w:'+$('#fonI').css('width')+'\n ||css h:'+$('#fonI').css('height'));
setTimeout(callback,'1');
});

}

this.belfon = function(mw,mh,dw,dh,callback)
{
		$('#fonII').stop(true).animate({width: mw+20,height: mh+20,top:((dh-mh+20)/2-20)+$(window).scrollTop(), left:((dw-mw+20)/2-20)+$(window).scrollLeft()},'slow',callback);

}


function resizew(select)
{
			size = viewport();
		$(select).css({			top:0, 
						left:0,
						width: size[0],
						height: size[1]
				});
}

/////Экран затемнения, неактивный
var on=false;
function smoke_screen(switcher,color)
{
if(color=='')
 color = 'black';
if(!on)
{
$('body').prepend('<div id="smoke_p" style="display:none;position:absolute;width:100%;height:100%;top:0;left:0;z-index:9999;"><div id="smoke" style="display:block;position:absolute;left:0px;top:0px;width:100%;height:100%;background:'+color+';"></div></div>');
$('#smoke_p').css({opacity:0.6});
	$(window).resize(function (){
resizew('#smoke');
});

on=true;
}
resizew('#smoke_p');
if(switcher=='on')
$('#smoke_p').fadeIn('fast');
else
$('#smoke_p').fadeOut('fast');

}


this.mvalid = function(email)
{
dd = "email="+email;
arr['email']=false;
filledOut(arr);
$.ajax({
   type: "POST",
   url: "email_valid.php",
   data: dd,
   success: function(msg){
if(msg=='true')
arr['email']=true;
else
arr['email']=false;
filledOut(arr);
   }
 });
}

this.filledOut = function(arrs){
no=false;
    for (val in arrs) { 
        if (arrs[val]==false) {
$('#SS').attr('disabled',true);
no=true;
$('#'+val).css({border: '2px outset red'});
}
else
{
if(!no)
$('#SS').attr('disabled',false);
$('#'+val).css({border: '2px outset green'});
}
}
no=false;
}



	function viewport() {
				
		// the horror case
		if ($.browser.msie) {
			
			// if there are no scrollbars then use window.height
			var d = $(document).height(), w = $(window).height();
			var d1 = $(document).width(), w1 = $(window).width();
			return [
				d1 - w1 < 30 ? w1 : d1, 					// ie6 quirks mode
				d
			];
		} 
		
		// other well behaving browsers
		return [$(document).width(), $(document).height()];
		
	} 
this.pageLoad = function(obj)
{
if(!obj) obj = 'b.pages';
$(obj).livequery('click',function (event){
            if (!event) event = window.event;
            var target = (event.target) ?
                         event.target : event.srcElement;
		url = $(target).attr('href');
		targetid = $(target).parents('div:eq(1)').attr('id');

	smoke_screen('on','green');
$("#"+targetid).load(url,function (){
        smoke_screen('off');

});
});

}

this.delPhoto = function(obj)
{
$(obj).livequery('click',function (event){
return false;
});
$(obj).livequery('click',function (event){
            if (!event) event = window.event;
            var target = (event.target) ?
                         event.target : event.srcElement;
		del_url = $(target).attr('href');
		refresh_url = $(target).attr('rel');
		targetid = $(target).parents('div:eq(0)').attr('id');
	smoke_screen('on','green');

$.ajax({
   type: "GET",
   url: del_url,
   success: function(msg){
//alert(msg);
   }
 });

$("#"+targetid).load(refresh_url,function (){
        smoke_screen('off');

});

});
}

///////////////////////////////////////Плавающее меню///////////////////////////////////////////
this.flow_menu = function(options)
{
	options = options || {};
	var name =  options['name'] || "#menu";
	var pb =  options['pb'] || 0;
	var ptarget =  options['ptarget'] || "";

if(!isNaN(parseInt($(name).css("top"))))
 {

	menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
	ptstart = $(name).offset().top;

           var offset = menuYloc+$(document).scrollTop()-ptstart+"px";
	   var calc = $(document).scrollTop()+parseInt($(name).height());

           if(calc<pb&&($(document).scrollTop()>ptstart))
           $(name).animate({top:offset},{duration:500,queue:false});

           if($(document).scrollTop()<ptstart)
           $(name).animate({top:menuYloc},{duration:500,queue:false});

       $(window).scroll(function () {

			if(ptarget=='')
			 pbr = viewport()[1]-pb;
			else
			 pbr =  $(ptarget).offset().top+$(ptarget).height()-pb;

           var offset = menuYloc+$(document).scrollTop()-ptstart+"px";
	   var calc = $(document).scrollTop()+parseInt($(name).height());

           if(calc<pbr&&($(document).scrollTop()>ptstart))
           $(name).animate({top:offset},{duration:500,queue:false});

           if($(document).scrollTop()<ptstart)
           $(name).animate({top:menuYloc},{duration:500,queue:false});
/*		$('#wid').val($(name).width());
		$('#hei').val($(name).height());
		$('#heiMT').val($(ptarget).height());
		$('#heiD').val(pbr);
		$('#offY').val(offset);
		$('#calc').val(calc);*/

       });
 }
}
/////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
	$(window).resize(function (){
resizew('#disp');
});
        $(document).ready(function() {

$("#sel").change(function() {
val = $('#sel').val();
if(val=='yes')
$('#trsel').css({display:'table-row'});
else
$('#trsel').css({display:'none'});

if(val=='text')
$('#trtext').css({display:'table-row'});
else
$('#trtext').css({display:'none'});
});

val = $('#sel').val();
if(val=='yes')
$('#trsel').css({display:'table-row'});
else if(val=='text')
$('#trtext').css({display:'table-row'});


		

$(".menu_td a img").hover(
  function () {
    $(this).attr('src',$(this).attr('rel'));
  },
  function () {
    $(this).attr('src',$(this).attr('rel1'));
  }
);


			size = viewport();
		$('#disp').css({		top:0, 
						left:0,
						width: size[0],
						height: size[1]
				});
 delPhoto('a.del_href');
 pageLoad();
 view('div.itooltip img','green');
 tooltip();
 form();

flow_menu({'name':'#menu','pb':10});

$('#fonMenu').css({opacity:0.5});


var options = { 
    target:     '#mc', 
    success:    function() {
 type = $('#storeForm').attr('title');
smoke_screen('off');
//	$('#disp').fadeOut(function (){	$('#fonII').css({display:'block'});});

////////////////////Переобозначение функций для корректной работы с новым DOM////////////////////
/*	$("div.itooltip,#fonner,#fonI,#close,.pages,#addMore").unbind('click');
	$("#close,#fonI").unbind('hover');
	$('#fonI').unbind('load');
 	view();
	pageLoad(); */
/////////////////////////////////////////////////////////////////////////////////////////////////
switch(type)
 {
case 'viewPhotos':
 {
$('#SS').attr('disabled',false);
			$('#storeForm').animate({opacity:1},'normal');
			$('#mc').fadeIn('normal');
break;
 }
case 'gb':
 {
             $('#storeForm').ajaxForm(options); 
$('#SS').attr('disabled',false);
			$('#storeForm').animate({opacity:1},'normal');
			$('#mc').fadeIn('normal');
break;
 }
default:
 {


$('#storeForm').fadeOut('normal',function (){
			$('#mc').fadeIn('normal');
});
	$('#addMore').click(function (){
$('#mc').fadeOut('normal',function (){
	$('#SS').attr('disabled',false);
			$('#storeForm').clearForm();
			$('#storeForm').fadeIn('normal').animate({opacity:1},'normal');
});
});

break;
 }
}
    },
    beforeSubmit: function (formData, jqForm, options)
{
type = $('#storeForm').attr('title');
switch(type)
 {
case 'gb':
{
	if(formData[0].value!=''&&formData[1].value!=''&&formData[3].value!='')
	{
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(formData[2].value))&&formData[2].value!='')
	{
	$('#storeForm').animate({opacity:1},'normal');
	$('#SS').attr('disabled',false);
	alert('Неправильный e-mail! '+formData[2].value);
	return false;
	}
	}
	else
	{
	alert('Не все поля формы заполнены');
	return false;
	}
break;
}
default:
  {
  }

 }

/*		$("div.itooltip,#fonner,#fonI,#close,.pages,#addMore").unbind('click');
		$('#disp').children().not('#fonner').css({display:'none'});
		$('#disp').fadeIn();
		$('#fonner').css('opacity',0.6);*/
		
		
		smoke_screen('on','green');
		$('#storeForm').animate({opacity:0.5},'normal');
		$('#SS').attr('disabled',true);



/*

    for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
$('#SS').attr('disabled',false);
            alert('Не все поля формы заполены корректно!'); 

            return false; 
        } 
    } 
*/
}
}; 


            $('#storeForm').ajaxForm(options); 
			
			
			
			
$('#email').blur(function (){
mm = $('#email').val();
mvalid(mm);
});

$('#text,#name,#theme').blur(function (){
if($('#text').val()!='')
arr['text']=true;
else
arr['text']=false;

if($('#name').val()!='')
arr['name']=true;
else
arr['name']=false;

if($('#theme').val()!='')
arr['theme']=true;
else
arr['theme']=false;
filledOut(arr);
});

var options1 = { 
    target:     '#formOtv', 
    success:    function() { 
$('#formJQ').fadeOut('normal',function (){
$('#formCont').css({display: 'none'});
$('#formOtv').css({display: 'block'});
		dw = $(window).width();
		dh = $(window).height();
			w=$('#formJQ').width();
			h=$('#formJQ').height();
belfon(w,h,dw,dh,function (){
mt=(dh-h)/2;
ml=(dw-w)/2;
mt += $(window).scrollTop();
ml += $(window).scrollLeft();
			$('#formJQ').css({left:ml, top:mt}).fadeIn('slow');
});
});


    },
    beforeSubmit: function (formData, jqForm, options)
{
$('#SS').attr('disabled',true);
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(formData[0].value))||arr['email']==false)
{
$('#SS').attr('disabled',false);
alert('Неправильный e-mail!');
return false;
}
    for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
$('#SS').attr('disabled',false);
            alert('Не все поля формы заполены корректно!'); 

            return false; 
        } 
    } 
}
}; 


            $('#formJF').ajaxForm(options1); 

        });


