/* =========================================================

// common.js

// Date: 2009-09-15
// Author: CREA Labs
// Web: http://www.crea.com.uy

*
*

// ========================================================= */

$(document).ready(
	function(){ 
		//PNG Fix for IE6
		$(document).pngFix(); 
			
		//Nav Menu tabs hover
		$('.nav li').mouseover(function (){
				$(this).addClass('hover');
			}).mouseout(function(){
				$(this).removeClass('hover');
		});	

		//collapsible sidebar Menu
		$('.collapsed').parent().children('.submenu').hide();
		
		/*
		$('.side-nav h3').click(function () {
			if($(this).hasClass('.collapsed')) {
				$(this).parent().children('.submenu').show();				
				$(this).removeClass('collapsed');
			}else{
				$(this).parent().children('.submenu').hide();				
				$(this).addClass('collapsed');				
			}
		});
		*/
		
		//innerfade for sidebar featured websites
		$('#mini-showcase').innerfade({ 
			animationtype: 'fade', 
			timeout: '4000',
			speed: 'fast',
			containerheight: '160px'			
		}); 
		
		// Editor Toolbar
		$('.editor-toolbar a.window-icon').click (
			function() {
				if($(this).parent().parent().hasClass('.restored')) {
					$(this).parent().parent().removeClass('restored').addClass('minimized');
				}else{
					$(this).parent().parent().removeClass('minimized').addClass('restored');
				}
			}
		);
		
		$('select').selectmenu({style: 'dropdown'});
		$(window).scroll(function () { 
	     	$('.ui-selectmenu-open').removeClass('ui-selectmenu-open');
	    });
		$('.espaceEtire').click(function() {
			var temp = $('#template_content__');
			var hauteur = temp.height();
			temp.css('height', hauteur + 150);
		});
	}
); 

function closeBoxy(){
	$("table.boxy-wrapper").remove(); 
	$("div.boxy-modal-blackout").remove();
}

function forgotPassword() {
	Boxy.load('forgotPassword-first.html',{title: $lang.forgot_password, unloadOnHide:true, afterShow : forgotInit, modal:true});
}

function forgotClose() {
	window.location.href = 'index.php';
}

function forgotInit() {
	$('#forgotFirst').validate({
		rules:{
			email:{
				required: true,
				email : true
			}
		},
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
		submitHandler: function () {
			$('#forgotFirst').ajaxSubmit({
				dataType: 'json',
				success: function(data) {
					if (data.success) {
						closeBoxy();
						Boxy.alert(data.message, null , {title: $lang.forgot_password});	
					} else {
						$('.error').html(data.message);
					}
				}
			});
		}
	});
	$('#forgotSecond').validate({
		rules:{
			email:{
				required: true,
				email : true
			},
			password:'required',
			password2: {
				required:true,
				equalTo:'#mdp'
			}
		},
		messages:{
			password2:{
				equalTo:$lang.retype_password
			}	
		},
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
		submitHandler: function () {
			$('#forgotSecond').ajaxSubmit({
				dataType: 'json',
				success: function(data) {
					if (data.success) {
						closeBoxy();
						Boxy.alert(data.message, function() { window.location.href = 'index.php'; } , {title: $lang.forgot_password});	
					} else {
						$('.error').html(data.message);
					}
				}
			});
		}
	});
}

function openContact() {
	Boxy.load('ajax.php?page=contact&action=contact&popup=1',{title: $lang.contact_us, afterShow : initContact, unloadOnHide:true, modal:true, fixed:false});
}

function initContact() {
	$('#contactForm').validate({
		rules:{
			email:{
				required:true,
				email:true
			},
			subject:'required',
			message:'required'
		},
		submitHandler: function() {
			$('#contactForm').ajaxSubmit({
				dataType:'json',
				success:function(data) {
					if (!data.error) {
						Boxy.alert($lang.message_sent, closeBoxy, {title: $lang.message_sent_title});
					} else {
						$('#error').html(data.error_message).show();
					}
				}
			});
		}
	});
}

function moveTheme(side, max) {
	var item = $('.items_theme');
	if ((side == 'left' && (parseInt(item.css('left')) + 156) > 0) || (side == 'right' && (parseInt(item.css('left')) - 156) < 0-max) || $('.items_theme').is(':animated')) {	
	} else {
		var move = (side == 'left') ? '+=156px' : ((side == 'right') ? '-=156px' : '0px');	
		$('.items_theme').animate({'left' : move});
	}
}

function signup() {
	Boxy.load('ajax.php?page=signup&popup=1',{title: $lang.signup, unloadOnHide:true, modal:true, fixed:false});
}

function signin() {
	Boxy.load('ajax.php?page=signin&popup=1',{title: $lang.signin, unloadOnHide:true, modal:true, fixed:false});
}

function affCGU() {
	Boxy.load('lang/'+$lang.lang+'/cgu.php',{title: $lang.cgu_title, unloadOnHide:true, modal:true, fixed:false});
}