$(function(){
	$.ifixpng(webRoot+'extensions/globalstroke/scripts/images/pixel.gif');
	$('.pngfix').ifixpng();	
	$('#aMedting').css({opacity: 0.4}).hover(function(){
		$(this).fadeTo('normal',1);
	},function(){
		$(this).fadeTo(1000,0.4);
	});
	
	$('#btnLogin').click(function(){
		if(validateLoginForm()){
			var username = MD5($('#txtUsername').val().trim().toLowerCase());
			var password = MD5($('#txtPassword').val().trim());
			
			x_GlobalStrokeLogin(username, password, h_LogIn);						
		}else{
			
		}
		return false;			
	});	
	$('#txtUsername, #txtPassword').focus(function(){
		$(this).siblings('.pError').slideUp('fast');
	});
	$('#txtUsername,#txtPassword').keydown(function(evt){
		if(evt.keyCode == 13){
			if($('#txtPassword').val().trim().length == 0){
				$('#txtPassword').focus();
			}else{
				$('#btnLogin').click();
			}
		}
	});
	$('#btnSignUp,.btnSignUp').click(function(){		
		x_GetView('signup',null,function(data){			
			showOverlay(function(){
				showDialog(data,function(){					
					var imgConf = $('#divConfirmation img');
					var src = imgConf.attr('src');
					imgConf.attr('src',src.replace('med/ok','big/ok')).css({opacity:0.7,margin: '0 20px 0 0'});					
				});				
			});
		});
		return false;
	});
	$('#aInterestedGlobalStrokeNetwork').click(function(){
		var dWidth = $(document).width();
		var dHeight= $(document).height();
		var divOverlay = $('<div/>').attr('id','idDivOverlay').css({width: dWidth, height: dHeight, position: 'absolute',zIndex: 3, background: '#030810', left: 0, top: 0, opacity: 0, display: 'block'}).appendTo(document.body);
		divOverlay.fadeTo('normal',0.7,function(){
			var wWidth = $(document).width();
			var wHeight= $(document).height();
			
			$('#divContactGlobalstroke').css({left: (wWidth - 700)/2, top: (wHeight / 3.5)}).fadeIn('slow');
		});
		
		return false;
	});
	$('#aCloseContact').click(function(){
		$('#idDivOverlay, #divContactGlobalstroke').fadeOut('normal');
		return false;
	});
	$(window).keydown(function(ev){
		if(ev.keyCode == 27){
			$('#idDivOverlay, #divContactGlobalstroke').fadeOut('normal');
		}
	});
	$('#txtUsername').focus();
});

String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g,'')}

function validateLoginForm(){
	var user = $('#txtUsername').val().trim();
	var pass = $('#txtPassword').val().trim();
	
	var items = $('#liMessage span.visible');
	
	if(items.length == 0){
		if(user.length < 3){
			$('#lblUsernameError').fadeIn('normal').addClass('visible');	
		}
		if(pass.length < 5){
			$('#lblPasswordError').fadeIn('normal').addClass('visible');
		}
	}
	
	items.fadeOut('fast',function(){
		$(this).removeClass('visible');
		
		if(items.index(this) == (items.length - 1)){
			if(user.length < 3){
				$('#lblUsernameError').fadeIn('normal').addClass('visible');	
			}else if(pass.length < 5){
				$('#lblPasswordError').fadeIn('normal').addClass('visible');
			}
		}	
	});
		
	return user.length && pass.length >= 5;	
}
function h_LogIn(response){
	if(response == true){
		window.location.reload(true);
	}else if(response != false){
		window.location = response;
	}else{
		$('#lblLoginError').fadeIn('normal').addClass('visible');
	}
}
var dialog = null;
var infoDialog = null;

function showDialog(data,fnx){
	AjaxKey = data.key;	
	for(var index in data.styles){
		addCss(data.styles[index]);
	}	
	
	dialog = $(data.content);
	dialog.hide().appendTo(document.body);
	var dWidth = dialog.outerWidth();
	var dHeight = dialog.outerHeight();
	var posX = (($(document).width() - dWidth) /2) - 100;
	var posY = ($(window).height() - dHeight) / 4 + $(window).scrollTop();
	
	dialog.css({zIndex: 11, left: posX, top: posY, position: 'absolute'});
	
	dialog.fadeIn('slow',function(){
		if(fnx != undefined){			
			fnx();
		}
		for(var index in data.scripts){
			addJs(data.scripts[index]);
		}		
	});
}
function showOverlay(callback){
	var overlay = $('#divMedtingOverlay');
	if(overlay.length == 0){
		overlay = $('<div/>').attr('id','divMedtingOverlay');
		overlay.click(function(){removeOverlay();});
	}
	var docW = $(document).width();
	var docH = $(document).height();
	var winH = $(window).height();
	
	overlay.css({width: docW,height: docH, left: 0, top: 0, opacity: 0});
	overlay.appendTo(document.body);
	overlay.fadeTo(600,0.85,function(){
		if(callback != null && callback != undefined)
			callback();
	});
}
function removeOverlay(fnx){
	if(dialog != null){
		$(dialog).fadeOut('normal',function(){
			$(this).remove();
			dialog = null;
			
			if(fnx != undefined){
				fnx();
				fnx = undefined;
			}
		});	
	}
	var overlay = $('#divMedtingOverlay');
	if(overlay.length > 0){		
		overlay.fadeOut('normal',function(){
			$(this).remove();
			
			if(fnx != undefined){
				fnx();
				fnx = undefined;
			}
		});
	}
}
function addCss(url){
	$('<link/>').attr({type: 'text/css', rel: 'stylesheet',href: url}).appendTo($(document).find('head'));
}
function addJs(url){
	jQuery.getScript(url);
}
function createFloatingMessage(input,text,parent,itemClass){
	var errorBox = $('<div/>').addClass(itemClass);
	errorBox.html(text).appendTo(parent);
	$('<div/>').addClass('divPointer').appendTo(errorBox);
	
	var width =  $(input).outerWidth();
	var topMod = 0;
	if($(input).attr('type') == 'checkbox'){
		width = $(input).parent().outerWidth() + 5;
		topMod = -3;
	}else if($(input).get(0).tagName == 'IMG'){
            width += 10;
        }
	
	var loc = $(input).position();
	var posX = loc.left + width +2;
	var posY = loc.top - 4 + topMod;
		
	errorBox.css({left: posX, top: posY});
	errorBox.fadeIn('slow');
        return errorBox;
}