$(document).ready(function(){

function getPageSize(){
	var yScrollleft;
    if (self.pageYOffset) {
      yScrolltop = self.pageYOffset;
    } else if(document.documentElement&& document.documentElement.scrollTop){ 
      yScrolltop = document.documentElement.scrollTop;
    } else if (document.body) {
      yScrolltop = document.body.scrollTop;
    }
	var maxWidth=Math.max($(window).width(),$(document).width());
	var maxHeight=Math.max($(window).height(),$(document).height());
	var arrayPageSize = new Array(maxWidth,maxHeight,yScrolltop,$(window).height());
	return arrayPageSize;
}

function hideall(){
	$('#ajax_loginBox').hide();	
	$('#simplebox').hide();
	$('#overlay').hide();	
}

//simplebox start
$("body").append("<div id='overlay'></div>");
$("body").append("<img id='simplebox' />");
$('#simplebox').click(function() {
	hideall();
	return false;
});

$("a[@rel=simplebox]").click(function() {showsimplebox(this);return false;});

function showsimplebox(objLink){
	var ps = getPageSize();
	$("#overlay").css({width:ps[0],height:ps[1]});
	$("#overlay").show();
	imgPreload = new Image();
	imgPreload.onload=function(){
		$("#simplebox").attr("src",objLink.href);
		return false;
	}
	imgPreload.src=objLink.href;
	tops=ps[2]+((ps[3]-imgPreload.height)*0.382)+"px";
	lefts=(ps[0]-imgPreload.width)*0.5+"px";
	$("#simplebox").css({top:tops,left:lefts});
	$("#simplebox").show();
}
//simplebox over



var loginOptions = { 
url:'login.asp?act=login',
target:'#ajax_result',
beforeSubmit:showLogLoading,
success:showResponse,
clearForm: true
};

var regOptions = { 
url:'register.asp?act=save',
target:'#ajax_result',
beforeSubmit:showRegLoading,
success:showResponse,
clearForm: true
};



$('#ajax_closeBtn').click(function() {
	hideall();
	return false;
});

$('#overlay').click(function() {
	hideall();
	return false;
});


$("#ajax_logout").click(function() {
	$('#topLogin').load('login.asp',{act:'logout'});
	$('#log_or_reg').html("///&nbsp;&nbsp;&nbsp;<a href='#'><font color='#AEEE00'>Login</font> <font color='#FF358B'>&</font> <font color='#01B0F0'>Register</font></a>");
	return false;
});

$("#log_or_reg").click(function() {
	$("#overlay").css({width:$(document).width(),height:$(document).height()});
	$("#overlay").show();
	var x=($(window).width()-320)/2;
	var y=($(window).height()-$("#ajax_loginBox").height())*0.382;
	$("#ajax_loginBox").css({top:y,left:x});
	$('#ajax_loginBox').show();	
	return false;
});

$('#ajax_loginForm').submit(function() {
	$(this).ajaxSubmit(loginOptions); 
	return false;
});

$('#ajax_regForm').submit(function() {
	$(this).ajaxSubmit(regOptions); 
	return false;
});


function showLogLoading(){
	$("#ajax_or").hide();
	$('#ajax_regPart').hide();
	$('#ajax_result').html("<img src='styles/v5/images/loading2.gif' />")
	$('#ajax_result').show();
}

function showRegLoading(){
	$("#ajax_or").hide();
	$('#ajax_loginPart').hide();
	$('#ajax_result').html("<img src='styles/v5/images/loading2.gif' />")
	$('#ajax_result').show();
}

function showResponse(responseText){
	if(responseText.indexOf("blog")>0||responseText.indexOf("success")>0){
			$('#ajax_loginBox').hide();
			$('#overlay').hide();
			$('#log_or_reg').empty();
			$("#topLogin").html(responseText);
	}
} 


//atanlabs
$("#labs").mouseout( function() { if(!this.contains(event.toElement))   setTimeout("$('#labsContent').hide()",500); } );
$("#labs").mouseover( function() { if(!this.contains(event.fromElement))   $("#labsContent").show(); } );

});
