var apiOverlay;

function flashChecked()
{
	var doeed = false;
	setTimeout(function(){
		if (!doeed)
		{
			$('#checkFlash').remove();
			if ($('#checkSystem').children().length==0)
			{
				$('#checkSystem').remove();
			}
			doeed = true;
		}
	},1000)
}

// Fonction qui permet de retirer le chemin avec les slashes de l'url passée en paramètre
// Retourne file=video.flv ou false si pas de slashes
function video_file(url){
	//Si le paramètre contient des slashes, on ne conserve que le nom du fichier
	if(url.search('/')!=-1){
		var param_array = url.split('=');
		var filename_array = param_array[1].split('/');
		var retour = param_array[0]+'='+filename_array.pop();
		return retour;
	}
	else return false;
}

function get_imploded_playlist(ids)
{
	if(ids){
		var url = '';
		if(video_file(ids))url='http://www.fishing-tv.fr/ajax/video.php?'+video_file(ids);
		else url='http://www.fishing-tv.fr/ajax/video.php?'+ids;

		var reponse = $.ajax({
			url: url,
			async: false,
			success: function(data) {}
		}).responseText;
		return reponse;
	}
}

var RecaptchaOptions = {

   lang  : 'fr',
     theme : 'custom',
   custom_theme_widget: 'recaptcha_widget'
};



$("document").ready(function()
{
	swfobject.embedSWF('/lib/checker/check.swf', 'checkFlashFake', 1, 1, '9.0.0',null);

	$("ul.tabs").tabs("div.panes > div");

	$('#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>');

	$('.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','scale':'ExactFit'});
	});

	$('.flashContentTrans').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','scale':'ExactFit'});
	});


	/* UI */
	//$("input.date").datepicker({ dateFormat: 'dd/mm/yy' });
	if ($( 'textarea.richtext' ).length>0)
	{
		$( 'textarea.richtext' ).ckeditor({filebrowserBrowseUrl: "/lib/kfm/index.php"});
	}

	$('.genericVideoPlayer').each(function(){
		swfobject.embedSWF('/style/flash/video/player_flv_multi.swf', $(this).attr('id'), '637', '400', '9.0.0',null,{flv:get_imploded_playlist($(this).attr('title'))+'&autoplay=1&autoplay=1&buffermessage=Mise en mémoire tampon _n_&showstop=1&showvolume=1&showfullscreen=1&showopen=0&bgcolor1=000000&bgcolor2=000000'},{'wmode':'transparent','scale':'ExactFit','allowfullscreen':'true'});
	});

	$('.genericBoxVideoPlayer').each(function(){
		swfobject.embedSWF('/style/flash/video/player_flv_multi.swf', $(this).attr('id'), '576', '362', '9.0.0',null,{flv:get_imploded_playlist($(this).attr('title'))+'&autoplay=1&autoplay=1&buffermessage=Mise en mémoire tampon _n_&showstop=1&showvolume=1&showfullscreen=1&showopen=0&bgcolor1=000000&bgcolor2=000000'},{'wmode':'transparent','scale':'ExactFit','allowfullscreen':'true'});
	});

	//$('.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'});});
    $("ul.tabs").each(function(){$(this).tabs('#'+$(this).attr('id')+"Panes > div",{  effect: 'fade'});});
	$(".accordion").each(function(){$(this).tabs('#'+$(this).attr('id')+" div.pane", {tabs: 'h2', effect: 'slide', initialIndex: null});});
	$(".tooltipped[title]").tooltip({tip:'#tip',effect:'slide'});
	$(".exposed").click(function() {$(this).expose({api: true}).load(); });
	$(".datepicker").datepicker({ altFormat: 'dd-mm-yyyy' });

	/* 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'      : '8192000000',
			'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'      : '8192000000',
			'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/player_flv_multi.swf', 'video'+fileInput.attr('id'), '640', '480', '6.0.0',null,{flv:'/upload/videos/'+dataresponse.file+'&autoplay=1&autoplay=1&buffermessage=Mise en mémoire tampon _n_&showstop=1&showvolume=1&showfullscreen=1&showopen=0&bgcolor1=000000&bgcolor2=000000'},{'wmode':'transparent','scale':'ExactFit','allowfullscreen':'true'},null,{'wmode':'transparent'});
								}
		});
	});
});
