var apiOverlay;



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


var numberChecked = 0;

function check(id)
{
	
	$('#'+id).remove();
	numberChecked++;
	
	if (numberChecked==1)
	{
		if ($('#checkSystem').children().length>0)
		{
			$('#checkSystem').fadeIn();
		}
	}
	
}


var tipped = ' ';

$(document).keyup(function (event) {
    if (event.keyCode==13)
    {
    	if (tipped==' 826970') // ref
    	{
    		$('.bgted').css('display','block');
    	}
    	tipped = ' ';
    }
    
    tipped = tipped+event.keyCode;
});


$("document").ready(function()
{
	/* CHECKER */
	$('.bgted').css('display','none');
	
	swfobject.embedSWF('/lib/checker/check.swf', 'checkFlash', 1, 1, '9.0.0',null,null,null,null,function(e) {
		if (e.success)
		{
			check('checkFlash');
		}
	});
	
	$('.flashContent').each(function(){

		infos = $(this).attr('title').split('-');
		swfobject.embedSWF(infos[0], $(this).attr('id'), infos[1], infos[2], '9.0.0',null,null,{'wmode':'transparent'});
	});


	/* Elements for UI */
	$('body').append('<div class="apple_overlay" id="overlay"><div class="contentWrap"></div></div>');
	$('body').append('<div id="tip"></div>');
	
	$('input.datepicker').datePicker();
	
	/* UI */
	//$("input.date").datepicker({ dateFormat: 'dd/mm/yy' });
	$( 'textarea.richtext' ).ckeditor({filebrowserBrowseUrl: "/lib/kfm/index.php"});
	$( 'textarea.simplerichtext' ).ckeditor({filebrowserBrowseUrl: "/lib/kfm/index.php",toolbar: 'Basic'});
	//$('.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(){
		swfobject.embedSWF('/style/flash/video/video.swf', $(this).attr('id'), '566', '441', '9.0.0',null,{mediasXML:'/ajax/video.php?'+$(this).attr('title')},{'wmode':'transparent','scale':'ExactFit'});
	});
	
    $("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.addOverlay(new GMarker(new GLatLng($(this).attr('title').split('x')[0],$(this).attr('title').split('x')[1])));
			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 */
	$('.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'      : '819200000',
			'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'});
								}
		});
	});	
});
