$(document).ready(AddListen);

var GalleryArray;

function AddListen()
	{
	var popups = $("img.popup_image");
	popups.css({cursor:"pointer"}).click(LoadImg);
	GalleryArray = new Array();
	popups.each( function(i)
		{
		im = new Image();
		im.src = getLargeImage(this.src);
		GalleryArray[i] = im;
		});
	PreloadImages();
	$("#fade").click(CloseEnlarged);
	$("li.pup-li a").click(function(e){e.preventDefault();});
	$("a.div_prev").click(PrevGallery);
	$("a.div_next").click(NextGallery);
	$("a.div_close").click(CloseEnlarged);
	
	$("input[type=file].checkfile").change(CheckFile).each( function(i)
		{
		$(this).
			after('<input type="hidden" name="'+this.id+'_param" id="'+this.id+'_param" value="'+this.title+'"/>').
			after('<input type="hidden" name="'+this.id+'_result" id="'+this.id+'_result"/>').attr('title', '');
		});
	$("form.strictform").submit(CheckStrictForm);
	}
function getElement(event)
	{
	if (event.srcElement)
		return event.srcElement;
	else if (event.target)
		return event.target;
	}
function PreloadImages()
	{
	var image = new Array(
		'controls.png',
		's00.png',
		's01.png',
		's10.png',
		's11.png',
		'st.png',
		'sb.png',
		'sr.png',
		'sl.png'
		);
	var oImage = new Array();
	for(var i in image)
		{
		oImage[i] = new Image();
		oImage[i].src = '/images/'+image[i];
		}
	}
function getLargeImage(oUrl)
	{
	var what=oUrl.split("/");
	var wh="";
	for(i=0;i<what.length-1;i++)
		wh+=what[i]+'/';
	wh+='__large/'+what[i];
	return wh;
	}
function LoadImg(event)
	{
	el = getElement(event);
	
	oUrl=el.src;
	wh = getLargeImage(oUrl);
		
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i].src == wh)
			{
			Img = GalleryArray[i];
			setTimeout('_fill()',200);
			return;
			}
		}
	}
function NextGallery(event)
	{
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i] == Img)
			{
			if( i == GalleryArray.length - 1 )
				{
				i = -1;
				}
			Img = GalleryArray[i + 1];
			setTimeout('_fill()',10);
			return;
			}
		}
	}
function PrevGallery(event)
	{
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i] == Img)
			{
			if( i == 0 )
				{
				i = GalleryArray.length;
				}
			Img = GalleryArray[i - 1];
			setTimeout('_fill()',10);
			return;
			}
		}
	}
function isImageOk(img)
	{
	if(!img.complete)
		return false;
	if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
        	return false;
	return true;
	}
function _fill()
	{
		if(!isImageOk(Img))
			{
			setTimeout('_fill()',10);
			return;
			}
		
		shadow = $("#div_shadow");
		container = $("#div_enlarged");
		ofade = $("#fade");
		
		container.empty().append(Img);
		var w = Img.naturalWidth || Img.width;
		var h = Img.naturalHeight || Img.height;
		
		shadow.css({margin:"-"+Math.round(h/2)+"px 0 0 -"+Math.round(w/2)+"px"});
		if(!jQuery.browser.msie)
			{
			shadow.fadeIn(300);
			ofade.fadeIn(300);
			}
		else
			{
			shadow.show();
			ofade.show();
			}
	}
function CloseEnlarged()
	{
	if(!jQuery.browser.msie)
		{
		$("#div_shadow").fadeOut(300);
		$("#fade").fadeOut(300);
		}
	else
		{
		shadow.hide();
		ofade.hide();
		}
	}
function run_movie(arg, w, h) 
	{ 
	document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="'+w+'" HEIGHT="'+h+'" id="'+arg+'" ALIGN=""><PARAM NAME=movie VALUE="'+arg+'.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=wmode VALUE=transparent> <EMBED src="'+arg+'.swf"  quality=high wmode=transparent  WIDTH="'+w+'" HEIGHT="'+h+'" NAME="'+arg+'" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>'); 
	}
function ChangeSelectSpan(event)
	{
	el = getElement(event);
	for (var i = 0, end=SelectSpans.length; i<end; i++)
		{
		if( SelectSpans[i] != el )
			{
			cmnRemove_class(SelectSpans[i].parentNode, 'chosen');
			cmnRemove_class($('div-'+SelectSpans[i].id), 'face-chosen');
			cmnSet_class($('div-'+SelectSpans[i].id), 'face-unchosen', '');
			}
		}
	cmnSet_class(el.parentNode, 'chosen', '');
	cmnRemove_class($('div-'+el.id), 'face-unchosen');
	cmnSet_class($('div-'+el.id), 'face-chosen', '');
	$('input-face').value = el.id;
	}

function ChangeShopperSpan(event)
	{
	el = getElement(event);
	for (var i = 0, end=ShopperSpans.length; i<end; i++)
		{
		if( ShopperSpans[i] != el )
			{
			cmnRemove_class(ShopperSpans[i].parentNode, 'chosen');
			cmnRemove_class($('div-'+ShopperSpans[i].id), 'face-chosen');
			cmnSet_class($('div-'+ShopperSpans[i].id), 'face-unchosen', '');
			}
		}
	cmnSet_class(el.parentNode, 'chosen', '');
	cmnRemove_class($('div-'+el.id), 'face-unchosen');
	cmnSet_class($('div-'+el.id), 'face-chosen', '');
	}

function CheckFile(event)
	{
	el = getElement(event);
	JsHttpRequest.query( 
		'/dk/checkfile.php', 
		{'photo': el, 'params' : $("#"+el.id+"_param").val()},
		function(result, errors)
			{
			response = result["params"]+'';
			if( response == '1' )
				{
				el.style.background = '#F4FFF2';
				}
			else if( (response != '1') && el.value )
				{
				el.style.background = '#FFF2F2';
				}
			$("#"+el.id+"_result").val(response);
			},
		false  // do not disable caching
		);
	}
function CreateCalendar(element, trigger)
	{
	Zapatec.Calendar.setup({
	firstDay          : 1,
	weekNumbers       : false,
	showOthers        : true,
	electric          : false,
	inputField        : element,
	button            : trigger,
	ifFormat          : "%d.%m.%Y",
	daFormat          : "%Y/%m/%d"
	});
	}

function CheckStrictForm(event)
	{
	oForm = getElement(event);
	Prevent(event);
	SubmitStrictForm(oForm);
	return false;
	}

function SubmitStrictForm(oForm)
	{
	var isAbleToSubmit = true;
	$(".strict,.checkfile", oForm).each( function(i)
		{
		if( isAbleToSubmit && (state = check_(this)) !== true)
			{
			alert(state+' '+this.title);
			this.focus();
			isAbleToSubmit = false;
			}
		});
	if(isAbleToSubmit)
		oForm.submit();
	return false;
	}

function CheckStrict(event)
	{
	oInput = getElement(event);
	return check_(oInput);
	}

function check_(oInput)
	{
	if( $(oInput).hasClass("checkfile"))
		{
		
		var result = $("#"+oInput.id+"_result");
		if(oInput.value && result.val() != '1')
			return "Выберите файл, соответствующий требованиям.";
		else
			return true;
		}
	else if( oInput.value=='' )
		{
		$(oInput).removeClass('partlyincorrect').addClass('incorrect');
		return "Введите";
		}
	else if( $(oInput).hasClass("email") && !CorrectEmailAddress(oInput.value) )
		{
		$(oInput).removeClass('incorrect').addClass('partlyincorrect');
		return "Введите правильный";
		}
	else
		{
		$(oInput).removeClass('incorrect').removeClass('partlyincorrect');
		return true;
		}
	}

function CorrectEmailAddress(email)
	{
	if(email.length > 0 && ( email.indexOf("@",0) == -1 || email.indexOf(".",0) == -1 ) )
		return false;
	return true;
	}
function Prevent(event)
	{
	if (event.cancelable)
		{
		event.preventDefault();
		}
	else
		{
		event.returnValue=false;
		}
	}