/**
* @version		$Id: SmartPlace 2009-09-03 11:50
* @package		SmartPlace
* @copyright	Copyright (C) 2004 - 2010
*/

/*--------------------------------------------------*/
/*--               INCLUDE function               --*/
// allow to include other javascript within a javascript
function include(file)  
{  
  var script  = document.createElement('script');  
  script.src  = file;  
  script.type = 'text/javascript';  
  script.defer = true;  
  document.getElementsByTagName('head').item(0).appendChild(script);  
}

/*------------------------------------------------*/
/*--               JQUERY plugins               --*/
include('/common/javascript/jquery/ajaxqueue.js');
include('/common/javascript/jquery/autocomplete.js');
include('/common/javascript/jquery/bgiframe.js');
include('/common/javascript/jquery/carousel.js');
include('/common/javascript/jquery/cookie.js');
include('/common/javascript/jquery/colorpicker.js');
include('/common/javascript/jquery/colorpicker.eye.js');
include('/common/javascript/jquery/fancybox.js');
include('/common/javascript/jquery/form.js');
include('/common/javascript/jquery/labelify.js');
include('/common/javascript/jquery/thickbox-compressed.js');
include('/common/javascript/jquery/timepicker.js');
//include('/common/javascript/jquery/timepickr.min.js');
include('/common/javascript/jquery/timeentry.js');
include('/common/javascript/jquery/tinymce.js');
// include('/common/javascript/jquery/timepicker-basic.js');
// include('/common/javascript/jquery/timepickr.js'); // Still developement : production release is not out yet
include('/common/javascript/jquery/tokeninput.js');
include('/common/javascript/jquery/treeview.js');
include('/common/javascript/jquery/utils.js');
//include('/common/javascript/jquery/treeview.demo.js');

$ = jQuery.noConflict();


/*------------------------------------------------*/
/*--            SMARTPLACE Functions            --*/
include('/common/javascript/smartplace.function.js'); 
include('/common/javascript/ajaxsearch.js');

// Skin
include('/common/javascript/smartplace/ajax.js');


$(document).ready(function(){
	$(":text").labelify({labelledClass: "labelinside"});
	$("a.image_appear").fancybox();
	$("a.lightbox").fancybox();
	$("a.image_zoom_basic").fancybox({
		'overlayOpacity'	:	0.7,
		'overlayColor'		:	'#FFF'
	});
	$("a.image_zoom").fancybox({
		'zoomSpeedIn'		:	300,
		'zoomSpeedOut'	:	600
	});

});

jQuery(function($){
	$.datepicker.regional['fr'] = {clearText: 'Effacer', clearStatus: '',
		closeText: 'Fermer', closeStatus: 'Fermer sans modifier',
		prevText: '&lt;Préc', prevStatus: 'Voir le mois précédent',
		nextText: 'Suiv&gt;', nextStatus: 'Voir le mois suivant',
		currentText: 'Courant', currentStatus: 'Voir le mois courant',
		monthNames: ['janvier','février','mars','avril','mai','juin',
		'juillet','août','septembre','octobre','novembre','décembre'],
		monthNamesShort: ['Jan','Fév','Mar','Avr','Mai','Jun',
		'Jul','Aoû','Sep','Oct','Nov','Déc'],
		monthStatus: 'Voir un autre mois', yearStatus: 'Voir un autre année',
		weekHeader: 'Sm', weekStatus: '',
		dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
		dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
		dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
		dayStatus: 'Utiliser DD comme premier jour de la semaine', dateStatus: 'Choisir le DD, MM d',
		dateFormat: 'dd/mm/yy', firstDay: 0, 
		initStatus: 'Choisir la date', isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['fr']);
});



/***
 * AJAX SELECT
 * Allow to automatically load ajax content of a select
 * depending on the value of another inpur in this form (inputSon)
 * ThinkMarketPlace 2009-09-30
 ***/
jQuery.fn.smartAjaxSelect = function( inputSon, sonDefaultValue, ajaxC ) {
  return $(this).each(function() {

		/* Update select if father is changed */
  	$(this).change(function(){
			if( $(this).val() ) {
				$('select#'+inputSon).css("display","inline");
				$.post( '?spmnid=aissr&ac='+ajaxC, { father: $(this).val() }, function(ajaxData){
					var options = "";
	    		for (var i = 0; i < ajaxData.length; i++){ options += "<option value='" + ajaxData[i].value + "'>" + ajaxData[i].display + "</option>"; }
	    		$('select#'+inputSon).html(options);
	    	}, "json" );
	    }
	    else { $('select#'+inputSon).css("display","none"); }
		});
			
		/* Initialize if there is already a value */
		if( $(this).val() ) {
			$('select#'+inputSon).css("display","inline");
			$.post( '?spmnid=aissr&ac='+ajaxC, { father: $(this).val() }, function(ajaxData){
				var options = "";
    		for (var i = 0; i < ajaxData.length; i++){
    			options += "<option value='" + ajaxData[i].value + "'";
    			/* Activte default value */
    			if( sonDefaultValue == ajaxData[i].value ) { options += " selected"; }
    			options += ">" + ajaxData[i].display + "</option>"; }
    		$('select#'+inputSon).html(options);
    	}, "json" );
    }
    else { $('select#'+inputSon).css("display","none"); }
	});
};


/***
 * CRM Ajax File
 * Will automatically display an AJAX popup with the file of the requested user 
 * ThinkMarketPlace 2009-09-30
 ***/
function smartCrmAjaxFile( crmType, crmWay, crmUserId, crmParameter, crmTitle ) {
	$("#smart_crm_ajax").load( "/?spmnid=crmform&crm_type="+crmType+"&crm_way="+crmWay+"&user_id="+crmUserId+"&crm_parameter="+crmParameter );
	var dialogParam = {
		modal: true,
		bgiframe: true,
		autoOpen: false,
		cache: false,
		height: 500,
		width: 750,
		title: crmTitle,
		draggable: true,
		resizeable: true
	};
  $("#smart_crm_ajax").dialog(dialogParam);
  $("#smart_crm_ajax").dialog("open");
};




/**********************************************/
/*                    SKIN                    */
/**********************************************/

/******************/
/*      MENU      */
jQuery(document).ready(function() { 
	jQuery("#menu ul").css({display: "none"}); // Opera Fix 
	jQuery("#menu li").hover(
		function(){ 
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(300); 
			$(this).parents('li').addClass("hover");
			$(this).addClass("hover");
		},
		function(){
			$(this).find('ul:first').css({ visibility: "hidden" });
			$(this).parents('li').removeClass("hover");
			$(this).removeClass("hover");
		}
	);
});

jQuery(document).ready(function() { 
	jQuery("#smart_crm_admin_menu ul").css({display: "none"}); // Opera Fix 
	jQuery("#smart_crm_admin_menu li").hover(
		function(){ 
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(300); 
			$(this).parents('li').addClass("hover");
			$(this).addClass("hover");
		},
		function(){
			$(this).find('ul:first').css({ visibility: "hidden" });
			$(this).parents('li').removeClass("hover");
			$(this).removeClass("hover");
		}
	);
});

jQuery(document).ready(function() { 
	jQuery("#smart_crm_menu ul").css({display: "none"}); // Opera Fix 
	jQuery("#smart_crm_menu li").hover(
		function(){ 
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(300); 
			$(this).parents('li').addClass("hover");
			$(this).addClass("hover");
		},
		function(){
			$(this).find('ul:first').css({ visibility: "hidden" });
			$(this).parents('li').removeClass("hover");
			$(this).removeClass("hover");
		}
	);
});