// JavaScript Document

$(document).ready(function()
{

	$("a.playlist-add").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut':	'elastic',
		'speedIn'		:	200, 
		'speedOut'		:	100, 
		'overlayShow'	:	false,
		'titleShow'		: false,
		'onComplete' : function()
		{
			$('#playlist-select').change(function()
			{
				if( $(this).val() == "-1" )
					$('#playlist-other').show();
				else
					$('#playlist-other').hide();
			});
			
			$('#form-article-add').ajaxForm({
				dataType: 'json',
				beforeSubmit: function(){
					$('#form-article-add button').attr('disabled', true);
					if(confirm('Voulez-vous vraiment ajouter ce document à la liste ?'))
					{
						return true;
					}
					else
					{
						$('#form-article-add button').attr('disabled', false);
						return false;
					}
				},
				success: function(data)
				{
					if( data.code == "0" ||  data.code == "-1" )
					{
						$.fancybox.close();
						//alert(data.msg+"\nLa page va se recharger...");
						//setTimeout('reloadPage()', 2000);
						alert(data.msg);
						reloadPage();
					}
					else
					{
						$("#playlist-add-msg").hide().html(data.msg).show();	
						//$('#form-article-add button').attr('disabled', parseInt(data.code, 10) <=  0 ? true : false);
						$('#form-article-add button').attr('disabled', false);
					}
				}
			});
		}	
	});
	
	$("a.playlist-rename").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut':	'elastic',
		'speedIn'		:	200, 
		'speedOut'		:	100, 
		'overlayShow'	:	false,
		'titleShow'		: false,
		'onComplete' : function()
		{
			$('#form-rename').ajaxForm({
				dataType: 'json',
				beforeSubmit: function(){
					$('#form-rename button').attr('disabled', true);
				},
				success: function(data)
				{
					reloadPage();
				}
			});
		}	
	});
});


function delArtFromPlaylist( pId, aId )
{
	if( pId && aId)
	{
		$.ajax({
			dataType: 'json',
			url: 'playlist_art_del.php?p='+pId+'&a='+aId,
			success: function (data){
				if( data.code == "0" )
					$('#playlist-item-'+pId+'-'+aId+'').fadeOut(reloadPage);
				else
					alert('error');
			}
		});
	}
}

function delPlaylist( pId )
{
	if( pId && confirm('Voulez-vous vraiment supprimer cette liste ?') )
	{
		$.ajax({
			dataType: 'json',
			url: 'playlist_del.php?p='+pId,
			success: function (data){
				if( data.code == "0" )
					reloadPage();
				else
					alert('error');
				
			}
		});
	}
}
function affichePopup(id)
{
	var chaine = document.location.href;
	var dernierSlash = chaine.lastIndexOf('/') + 1;
	var dernierPoint = chaine.lastIndexOf('.html');
	var basename = chaine.substring(dernierSlash,dernierPoint);
	window.open('/popup.php?id='+id+'&rub_rw='+basename,'popup','width=565,height=500,scrollbars=yes');
}
function afficheForms()
{
	var ancre = document.location.hash.substr(1);
	switch( ancre )
	{
		case "formcontact" : 
			affichePannelTrame('formulaireContact');
			break;
		case "formnewsletter" : 
			affichePannelTrame('formulaireNewsletter');
			break;
	}
}

function reloadPage ()
{
	document.location.href = document.location.href.replace(/#(.*)/, '');
}


function view(a_param){
	window.open('view.php?h='+a_param, "viewer","width=1024,height=768");
}

function view2(a_param){
	window.open('../view.php?h='+a_param, "viewer","width=1024,height=768");
}
//============================================================================================================================
// constants to define the title of the alert and button text.
var ALERT_TITLE = "AMORC";
var ALERT_BUTTON_TEXT = "OK";

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
/*
if(document.getElementById) {
	window.alert = function(txt) {
		createCustomAlert(txt);
	}
}
*/

function createCustomAlert(txt) {
if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				//$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				//$("#TB_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}
	
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.innerHTML = txt;
	
	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { removeCustomAlert();return false; }

	
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	tb_remove();
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}

