var layerHeight = 130;
var layerWidth = 200;

function cm_bwcheck()
{
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent.toLowerCase()
	this.dom=document.getElementById?1:0
	this.ns4=(!this.dom && document.layers)?1:0;
	this.op=window.opera 
	this.moz=(this.agent.indexOf("gecko")>-1 || window.sidebar)
	this.ie=this.agent.indexOf("msie")>-1 && !this.op
	this.safari=(this.agent.indexOf("safari")>-1)
	if(this.op){
		this.op5=(this.agent.indexOf("opera 5")>-1 || this.agent.indexOf("opera/5")>-1)
		this.op6=(this.agent.indexOf("opera 6")>-1 || this.agent.indexOf("opera/6")>-1)
		this.op7=this.dom&&!this.op5&&!this.op6
	}else if(this.moz) this.ns6 = 1
	else if(this.ie){
		this.ie4 = !this.dom && document.all
  	this.ie5 = (this.agent.indexOf("msie 5")>-1)
  	this.ie55 = (this.ie5 && this.agent.indexOf("msie 5.5")>-1)
  	this.ie6 = this.dom && !this.ie4 && !this.ie5 && ! this.ie55
  	this.ie7 = document.all && !document.XMLHTTPObject
  	
	}
	this.mac=(this.agent.indexOf("mac")>-1)
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.op5 || this.op6 || this.op7)
  this.usedom= this.ns6||this.op7
  this.reuse = this.ie||this.op7||this.usedom
  this.px=this.dom&&!this.op5?"px":""
	return this
}

var bw=new cm_bwcheck();


function hideLayer() {

	$("#login_dialog").fadeOut(500, function(){
		$('#login_dialog').css({'height': layerHeight+'px'});
	});;
	
	$("#lb-overlay").fadeOut(500);
	

	
	
	$("select").css("visibility", "visible");
	$("#login_dialog").fadeOut(500);
	$("body").css("position", "");
	$("body").css("height", "");
	$("body").css("overflow","auto");
	
	$('.error').text("");
	
	if (bw.ie && !document.documentElement.clientWidth) {
		
		setScroll(0,this.yPos);
		prepareIE("auto", "auto");
	}
}


function prepareIE(height, overflow){
	bod = document.getElementsByTagName('body')[0];
	bod.style.height = height;
	bod.style.overflow = overflow;

	htm = document.getElementsByTagName('html')[0];
	htm.style.height = height;
	htm.style.overflow = overflow; 
}


function showLayer(){
	
	if (window.innerWidth) {
		width = window.innerWidth;
	} else if (bw.ie && !document.documentElement.clientWidth) {
		width = document.body.offsetWidth;
	} else if (bw.ie7) {
		width = document.documentElement.clientWidth;
	}
	
	if (window.innerHeight) {
		height = window.innerHeight;
	} else if (bw.ie && !document.documentElement.clientHeight) {
		height = document.body.offsetHeight;
	} else if (bw.ie7) {
		height = document.documentElement.clientHeight;
	} 
	
	
	
	
		
	pos_left = (width-layerWidth)/2;
	pos_top	 = (height-layerHeight)/2;
	pos_left 	= Math.ceil(pos_left);
	pos_top 	= Math.ceil(pos_top);
	
	if (pos_top < 0) pos_top = 0;
		
	$("body").css("position", "relative");
	$("body").css("overflow","hidden");
	$("body").css("height", height);
	$("#login_dialog").fadeIn(500);
	$("#lb-overlay").css("display", "block");
	$("#lb-overlay").css("filter", "alpha(opacity=70)");
	$("#login_dialog").css("left", pos_left+"px");
	$("#login_dialog").css("top", pos_top+"px");
	
}

function login(){
	
	inputfields = $('#loginform :input').serialize();
	$('#error').text("");
	$('#ajax-loader').show();

	 $.ajax({
	   type: "POST",
	   url: "/user/login/",
	   data: inputfields,
	   dataType: "json",
	   success: function(data){
	   	
	   		$('#ajax-loader').hide();
	     	
	   		if (data == true){
	   			
	   			window.location.reload()
	   			
	   		}else{
	   			
	   			$('#error').text(data[0]);
	   		}
	   	
	   		var success = false;
			/*
			if (data[target]['success'] == true){

				success = true;
				
			}*/
 			
     	}
	   
	 });
}

function logout(){
	
	$.ajax({
	   type: "GET",
	   url: "/user/logout/",
	   success: function(data){
	   	
	   		window.location.reload()
 			
     	}
	   
	 });
	
}
