$(document).ready(JI_init);
function JI_init(){
	JI=$('#JI');
	JC=$('#JI_copy');
	CC=$('#cc');
	CA='0';
	var m=$("#menu0 a.here");
	var width=250;
    var options = {
    	target: CC,
		beforeSubmit: fade1,
		success: function(){CC.fadeIn('slow')}
		}; 
			$("body").append("<div id='JI' style='width:"+width+"px'><div class='JI_close'>.</div><div id='JI_copy'><div class='JI_loader'><div></div></div>");$('#JI').hide();
	  		m.click(function(){
	  			JI_menuClick(this.href,m.index(this));
				$(this).parent().addClass('active');
				return false
			});
        $('form').ajaxForm(options);
			$("#picdiv")
			.click(function(e){
					JI_click(e.pageX,e.pageY);
					return false;
			})
			.mouseout(function()
				{$('#theid').attr('value','');
				$('#JI').hide()
			})
			.mousemove(function(e){
					JI_move(e.pageX,e.pageY)
			});
}
function fade1(){
	$('#mainbody div').fadeOut('slow');
}
function showResp(){
   	CC.fadeIn('slow');
}
function JI_menuClick(url,ind){
	$("#menu0 li").removeClass('active');
	$('#mainbody div').fadeOut('slow',function(){
		if (ind){CC.load(url+'&a=1',function(){CC.fadeIn('slow')})}else{$('#picdiv').fadeIn('slow')}});	
}

function JI_submit(url,pinco,theid){
	$("#menu0 li").removeClass('active');
	$('#mainbody div').fadeOut('slow',function(){CC.load(url,{id: theid,pin: pinco},function(data){CC.fadeIn('slow');if (data.length<4){document.location='g.php?d='+theid}})});
}

function JI_move(x,y){
	var params = parseCoords(x,y);
	var JI=$('#JI');
	var JC=$('#JI_copy');
	JI.css({left: params['x']+"px", top: params['top']+"px"});
	JI.show();
	if(params['id']!=CA){
		CA=params['id'];
		$('.JI_close').html(CA);
		JC.html("<div class='JI_loader'><div>");
	setTimeout(function(){if(CA==params['id']){JC.load(params['url']+'&v=1')}},400);
	};
}
function JI_click(x,y){
	var params = parseCoords(x,y);
	var pinco = $('#pininput').attr('value');
	var theid = params['id'];
	JI.css({left: params['x']+"px", top: params['top']+"px"});
	$('#JI_copy').html("<div class='JI_loader'><div>");
	JI.show();
	JI_submit('pincode.php',pinco,theid);
}

function getAbsLeft(objectId) {
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oLeft += oParent.offsetLeft
		o = oParent
	}
	return oLeft
}

function getAbsTop(objectId) {
	o = document.getElementById(objectId)
	oTop = o.offsetTop
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oTop += oParent.offsetTop
		o = oParent
	}
	return oTop
}

function parseCoords (x,y) {
   var Params = new Object ();
   var dx=getAbsLeft('bigpic');
   var dy=getAbsTop('bigpic');
   var sx=Math.floor((x-dx)/10)*10+dx;
   var sy=Math.floor((y-dy)/10)*10+dy;
   Params['width']=250;
   Params['lft']=sx;
   Params['top']=sy;
   Params['rgt']=sx+10;
   Params['btm']=sy+10;
   Params['x']=Params['rgt']+5;
   Params['id']=1+Math.floor((x-dx)/10)+Math.floor((y-dy)/10)*100;
   Params['url']='g.php?d='+Params['id'];
   if (x-dx>=500) Params['x']=Params['lft']-Params['width']-5;
   return Params;
}
function modal(){
	var overlay = $("<div id='modal-overlay'></div>");
	if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
						$("body","html").css({height: "100%", width: "100%"});
						$("html").css("overflow","hidden");
					}
					
					$("body").append(overlay)
					$("body").append("<div id='modal-load'></div>");
					overlay.css("opacity", 0.8);
					overlay.fadeIn(150);
					e.preventDefault();
}
function modalHide() {
				var r = $('#modal-overlay');
				r.fadeOut('slow',function(){r.remove()});
}

function blockEvents(evt) {
              if(evt.target){
              evt.preventDefault();
              }else{
              evt.returnValue = false;
              }
}