function openWindow(url,width,height) {
	if(!width) var width = 550;
	if(!height) var height = 350;
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top = parseInt((screen.availHeight/2) - (height/2));
	var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
	var myWindow = window.open(url, "subWind", windowFeatures);
}
$('#relaodCaptcha').click(function(){
	var d = new Date();
	$('#captchaImg').attr('src','/img/loading.gif');
	$('#captchaImg').attr('src','/users/captcha?'+d.getTime());
});
	
/*	
function showPanel(panel) {
	if(!$('#event div.tabs a[rel="'+panel+'"]').hasClass('active')) {
		$('#event div.tabs a').removeClass('active');
		$('#event div.tabs a[rel="'+panel+'"]').addClass('active');
		$('.panel').hide().removeClass('active');
		$('.panel[rel="'+panel+'"]').fadeIn(800, function() {
			$(this).addClass('active');
		});
		document.location.hash = panel;
	}
}
*/
function showPanel(panel) {
	if(!$('div.tabs a[rel="'+panel+'"]').hasClass('active')) {
		$('div.tabs a').removeClass('active');
		$('div.tabs a[rel="'+panel+'"]').addClass('active');
		$('.panel').hide().removeClass('active');
		$('.panel[rel="'+panel+'"]').fadeIn(800, function() {
			$(this).addClass('active');
		});
		document.location.hash = panel;
	}
}
	
$(document).ready(function(){
	
	$('.dropdown').dropdown();
	
	/*
	$('#event div.tabs a').click(function() {
		panel = $(this).attr('rel');
		showPanel(panel);
	});
	*/
	$('div.tabs a').click(function() {
		panel = $(this).attr('rel');
		showPanel(panel);
	});
	
	if(window.location.hash){
		var type = window.location.hash;				
		type = type.substring(1);
		if( $('div.tabs').find('a[rel='+type+']').length >0){
			showPanel(type);
		}
	}
	
	
	// SIGNIN
	$('a.linkedin').live('click',function(){ 
		openWindow('/linkedinapp',450,245);
	});
	
	//SIGNIN ' Load Signin Popup's content on startup )
	$("#dialog-signin").load('/users/login',function(){
		$(this).dialog({title:'<h2>Sign In <span>&nbsp;</span></h2>', show: 'show', position: ['center',100], resizable: false, draggable: false,modal:true,autoOpen:false,width:450});
	});
	$('#btn-signin').live('click',function(){ 
		$("#dialog-signin").dialog('open');
	});
	$("#dialog-signin").find('input').live('keypress',function(e) {
		if (e.keyCode == 13){
			$('.login-submit').click();
			//e.preventDefault();
			return false;
		}
	});
	
	// Submit Signin
	$('.login-submit').live('click', function(){	
		var values = $(this).parents('form').serialize();
		var errorDiv = $(this).parents('form').find('.form-error');
		$('#logOverlay').show();
		$.post('/users/login',values,function(data){
			//console.log(data);
			if(!data['success']) {
				errorDiv.html(data['error']);
				$('#logOverlay').hide();
			} else {
				setTimeout(function(){
					window.location.href = data['redirect'];
				},1000);
			}
		},'json');
	});
	
	
	$("#form-newsletter").validate({
		rules:{'data[email]': {required: true,email:true}}
	});
	
	
	// NEWSLETTER
	$('#newsBtn').live('click',function(){
		if(!$("#form-newsletter").valid()){
			$('#newsBox').find('.infos').removeClass('black');
			return false;
		}
		var values = $('#form-newsletter').serialize();
		$.post('/users/newsletter',values,function(data){
			//$('#debug').html(data);
			if(!data['success']) {
				alert(data['error']);
			} else {
				$('#newsBox div.form').hide();
				$('#newsBox').find('.infos').show().html('You have successfully subscribed to our Newsletter');
			}
		},'json');
		
	});

	$('div.event').live( 'hover',function(ev){
		if (ev.type == 'mouseenter') $(this).addClass("hover"); 
		else if (ev.type == 'mouseleave') $(this).removeClass("hover");
	});
	$('div.featured').live( 'hover',function(ev){
		if (ev.type == 'mouseenter') $(this).parent().find('.verified-infos').show();
		else if (ev.type == 'mouseleave') $(this).parent().find('.verified-infos').hide();
	});
	
	$('.expand a.more').live('click',function(){
		var id = $(this).attr('rel');
		$('.event div.details[rel='+id+']').slideDown();
		$(this).attr('class','less').html('view less<span></span>');
		
	});
	$('.expand a.less').live('click',function(){
		var id = $(this).attr('rel');
		$('.event div.details[rel='+id+']').slideUp();
		$(this).attr('class','more').html('view more<span></span>');
	});
		
	$('#event .bottom h3 a.seeAll').click(function() {
		panel = $(this).attr('rel');
		showPanel(panel);
	});
	

});
