var apiOverlay;

function flashChecked()
{
	$('#checkFlash').remove();
	if ($('#checkSystem').children().length==0)
	{
		$('#checkSystem').remove();
	}
}

var RecaptchaOptions = {
 
   lang  : 'fr',
     theme : 'custom',
   custom_theme_widget: 'recaptcha_widget'
};


$("document").ready(function()
{
	/* CHECKER */
	
	swfobject.embedSWF('/lib/checker/check.swf', 'checkFlashFake', 1, 1, '9.0.0',null);
	
	
	$('#checkJS').remove();
	if ($('#checkSystem').children().length==0)
	{
		$('#checkSystem').remove();
	}
	


	/* Elements for UI */
	$('body').append('<div class="apple_overlay" id="overlay"><div class="contentWrap"></div></div>');
	$('body').append('<div id="tip"></div>');

	/* UI */
	//$("input.date").datepicker({ dateFormat: 'dd/mm/yy' });
	$( 'textarea.richtext' ).ckeditor({filebrowserBrowseUrl: "/lib/kfm/index.php"});
	//$('.genericVideoPlayer').each(function(){swfobject.embedSWF('/style/flash/mediaplayer.swf?file='+$(this).attr('title'), $(this).attr('id'), '640', '480', '6.0.0',null,null,null,{'wmode':'transparent'});});
    
    $('.genericVideoPlayer').each(function(){
    	/*
    	if ($(this).css('width')) width = $(this).css('width');
    	if ($(this).css('height')) height = $(this).css('height');*/
    	var width = 640;
    	var height = 480;
    	var dim = $(this).attr('class').split(' ')[1];
    	
    	if (dim)
    	{
    		width = dim.split('x')[0];
    		height = dim.split('x')[1];
    	}
    	
		swfobject.embedSWF('/style/flash/video/video.swf', $(this).attr('id'), width, height, '9.0.0',null,{mediasXML:'/ajax/video.php?'+$(this).attr('title')},{'wmode':'transparent'});
	});
	
	
    $("ul.tabs").each(function(){$(this).tabs('#'+$(this).attr('id')+"Panes > div",{  effect: 'fade'});});
	$(".accordion").each(function(){
		var initIndex = null;
		if ($('#'+$(this).attr('id')+'Init'))
		{
			initIndex = $('#'+$(this).attr('id')+'Init').val()*1;
		}
		$(this).tabs('#'+$(this).attr('id')+" div.pane", {tabs: 'h2', effect: 'slide', initialIndex:initIndex })
	});
	
	$(".accordion h2").click(function(){
	
		if ($(this).find('a'))
		{
			href = $(this).find('a').attr('href');
			if ((href.indexOf('/')==0 || href.indexOf('http')==0) && href.indexOf('#')!=0)
			{
				$(location).attr('href',href);
				return false;
			}
		}
		
	});

	$(".accordionVert").each(function(){
		$(this).tabs('#'+$(this).attr('id')+" div", {tabs: 'img.icon',  effect: 'horizontal'});
	})
	
	$(".tooltipped[title]").tooltip({tip:'#tip',effect:'slide'}); 
	$(".exposed").click(function() {$(this).expose({api: true}).load(); }); 
	$(".datepicker").datepicker({ altFormat: 'dd-mm-yyyy' });
	 
	$(".gmap").each(function(){
		if (GBrowserIsCompatible()) {
			
			var map = new GMap2(document.getElementById($(this).attr('id')));
			map.setCenter(new GLatLng($(this).attr('title').split('x')[0],$(this).attr('title').split('x')[1]), 13);
			map.setUIToDefault();
			return map;
		}

	})
	
	/* FORMS */
	$("form.toValidate").validate({
		rules: {
			mdp: {
				required: true,
				minlength: 5
			},
			confirm_mdp: {
				required: true,
				minlength: 5,
				equalTo: "#mdp"
			}	
		},
		messages: {
			mdp: {
				required: "Veuillez entrer votre mot de passe",
				minlength: "Le mot de passe doit être de 5 caractères minimum"
			},
			confirm_mdp: {
				required: "Veuillez confirmer votre mot de passe",
				minlength: "Le mot de passe doit être de 5 caractères minimum",
				equalTo: "La confirmation n'est pas identique au mot de passe"
			}
		}
	});

	
	/* BOXES */
	$("a.boxFrame[rel]").overlay({
    	expose: '#fff', 
        effect: 'apple', 
        api: true,
        onBeforeLoad: function() { 
            var wrap = this.getContent().find(".contentWrap"); 
 			wrap.addClass('frame');
            wrap.html('<iframe frameborder="0" src="'+this.getTrigger().attr("href")+'"></iframe>');
            //wrap.load(this.getTrigger().attr("href")); 
        } 
	}); 

	$("a.boxAjax[rel]").overlay({
    	expose: '#fff', 
        effect: 'apple', 
        api: true,
        onBeforeLoad: function() { 
            var wrap = this.getContent().find(".contentWrap"); 
 			wrap.removeClass('frame');
            wrap.load(this.getTrigger().attr("href")); 
        } 
	}); 

	$("a.boxFrameCrop[rel]").overlay({
    	expose: '#fff', 
        effect: 'apple', 
        api: true,
        onBeforeLoad: function() { 
            var wrap = this.getContent().find(".contentWrap"); 
 			wrap.addClass('frame');
            wrap.html('<iframe frameborder="0" src="'+this.getTrigger().attr("href")+'"></iframe>');
            //wrap.load(this.getTrigger().attr("href")); 
        },
        onBeforeClose: function()
					{
						
						fieldName = $('#overlay iframe').contents().find('#cropField').val();
						what = $('#overlay iframe').contents().find('#what').val();
						
						pict = $('#overlay iframe').contents().find('#imageContainer img').attr('src');
						if (pict && !($('#overlay iframe').contents().find('#fileToTransmit').length))
						{
							tmp = pict.split('/');
							jfile = tmp[tmp.length-1];
							;
							$('#'+fieldName).val(jfile);
							$('#addPhoto'+fieldName).html('Changer');
							
							if ($('#imageCro'+fieldName))
							{
								$('#imageCro'+fieldName).remove();
							}
							var img = new Image();
							img.src = pict;
							$(img).attr('id','imageCro'+fieldName);
							
							var zone = $('#image');
							if (zone)
							{
								var zone = $('#image'+fieldName);
							}
							zone.append(img);
						}
					}
	});

	

	/* UPLOADIFY */
	$('.uploadifiedPrivate').each(function(){
		var fileInput = $(this);
		


		$(this).uploadify({
			'uploader'  : '/lib/uploadify/uploadify.swf',
			'script'    : '/lib/uploadify/uploadprivate.php',
			'cancelImg' : '/lib/uploadify/cancel.png',
			'wmode' 	: 'transparent',
			'auto'      : true,
			'queueID'   : false,
			'buttonImg' : '/style/admin/button-browse.png',
			'width' 	: 100, 
			'folder'    : '/upload',
		
		
			'queueSizeLimit' : 1,
			'multi'          : false,
			'sizeLimit'      : '81920000000',
			'onComplete'  	 : function(event,queueID,fileObj,response,data) 
								{
									var dataresponse = eval('(' + response + ')');
									
									fileInput.val(dataresponse.file)
								}
		});
	});	
	
	
	$('.uploadified').each(function(){
		var fileInput = $(this);
		var authFiles = $(this).attr('class');
		var authFilesArray = authFiles.split(' ');
		authFiles = '';
		authDesc = '';
		$(authFilesArray).each(function()
		{
			value = this.replace('File','');
			if (value!='uploadified')
			{
				authFiles += '*.'+value+';';
				authDesc  += value+';';
			}
		});



		$(this).uploadify({
			'uploader'  : '/lib/uploadify/uploadify.swf',
			'script'    : '/lib/uploadify/upload.php',
			'cancelImg' : '/lib/uploadify/cancel.png',
			'wmode' 	: 'transparent',
			'auto'      : true,
			'queueID'   : false,
			'buttonImg' : '/style/admin/button-browse.png',
			'width' 	: 100, 
			'folder'    : '/upload',
			'fileDesc'  : authDesc,
			'fileExt'   : authFiles,
			'queueSizeLimit' : 1,
			'multi'          : false,
			'sizeLimit'      : '81920000000',
			'onComplete'  	 : function(event,queueID,fileObj,response,data) 
								{
									var dataresponse = eval('(' + response + ')');
									
									fileInput.val(dataresponse.file)
								}
		});
	});	
	
	
	
	$('.uploadifiedMovie').each(function(){
		var fileInput = $(this);
		var authFiles = $(this).attr('class');
		var authFilesArray = authFiles.split(' ');
		authFiles = '';
		authDesc = '';
		$(authFilesArray).each(function()
		{
			value = this.replace('File','');
			if (value!='uploadified')
			{
				authFiles += '*.'+value+';';
				authDesc  += value+';';
			}
		});



		$(this).uploadify({
			'uploader'  : '/lib/uploadify/uploadify.swf',
			'script'    : '/lib/uploadify/upload.php',
			'cancelImg' : '/lib/uploadify/cancel.png',
			'wmode' 	: 'transparent',
			'auto'      : true,
			'queueID'   : false,
			'buttonImg' : '/style/admin/button-browse.png',
			'width' 	: 100, 
			'folder'    : '/upload',
			'fileDesc'  : authDesc,
			'fileExt'   : authFiles,
			'queueSizeLimit' : 1,
			'multi'          : false,
			'sizeLimit'      : '81920000000',
			'onComplete'  	 : function(event,queueID,fileObj,response,data) 
								{
									var dataresponse = eval('(' + response + ')');
									
									fileInput.val(dataresponse.file);
									fileInput.parent().next('.genericVideoPlayer').remove();
									fileInput.parent().after('<div class="genericVideoPlayer" id="video'+fileInput.attr('id')+'"></div><br class="clear"/>');
									swfobject.embedSWF('/style/flash/video/video.swf', 'video'+fileInput.attr('id'), '640', '480', '6.0.0',null,{mediasXML:'/ajax/video.php?file='+dataresponse.file},null,{'wmode':'transparent'});
								}
		});
	});	
});
