$(document).ready(function() {
	$("#contmsg #cerrorbox").clone().prependTo("#main");
	$("#contmsg #cmsgbox").clone().prependTo("#main");
	$("#contmsg").remove();
	setTimeout("$('#cerrorbox').hide('slow')", 8000);
	setTimeout("$('#cmsgbox').hide('slow')", 10000);
	
	setTimeout("$('.advBanner').hide('slow')", 7000);
	setTimeout("$('.postit').hide('slow')", 7000);
});

function selectAllList(selectBox, chk) {
	var selectBox = document.getElementById(selectBox);
	if ( chk.checked == true) {
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = true;
		}
	} else {
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = false;
		}
	}
	
}

function getTwitts(event, page) {
	$.post(baseurl + '/newajax/twitts.php', 
		{
			action: 'getTwitts',
			id: event,
			p: page
		}
		, function(result) {
			if ( result.success == 'OK') {
				$('#twitter-comments').fadeOut();
				$('#twitter-comments').html(result.html);
				$('#twitter-comments').fadeIn();
			}
		}
		,'json')
}

function postComment(tipo, inputComentario, divAddMsg, divAddComments, divHide, divShow, userID, objID) {
	$.post(baseurl + '/newajax/comments.php', 
		{
			action: 'addComment',
			type: tipo,
			obj: objID,
			uid: userID,
			comm: $("#" + inputComentario).val()
		}
		, function(result) {
			if ( result.success == 'OK') {
				$('#' + divAddComments).append(result.html);
				$("#" + inputComentario).val('Write your comment here...');
			}
			$('#' + divHide).hide();
			if ($('#' + divAddComments).hasClass('jsHide') ) {
				$('#' + divAddComments).removeClass('jsHide');
			}
			$('#' + divAddMsg).html(result.msg);
			$('#' + divAddMsg).show();
			setTimeout("$('#" + divAddMsg + "').hide();", 4000);
		}
		,'json')
}

function inlineDisplay(tipo, id, div, fondo) {
	if ( tipo == 'video' ) {
		$('#' + div).hide();
		$('#' + div).html('<iframe src="' + baseurl + '/library_popup.php?vid=' + id + '&col=' + fondo + '" width="620" height="410" frameborder="0" scrolling="no" name="inlinePlayer"></iframe>');
		$('#' + div).show();
	} else {
		$('#' + div).hide();
		$('#' + div).html('<iframe src="' + baseurl + '/download.php?type=document&id=' + id + '&col=' + fondo + '" width="620" height="410" frameborder="0" scrolling="no" name="inlinePlayer"></iframe>');
		$('#' + div).show();
	}
}



// shows form to add emails.
function showGetEmailContactsForm ( mail_provider )
{
	
	var dialog = $('<div style="display:hidden" id="newDialog"></div>').appendTo('body');

	$("#newDialog").html("<image src='../contacts_importer/images/progressAnimation.gif'/><a>&nbsp; Loading...<a/>");
				
	$("#newDialog").dialog({ 
		bgiframe: true,
		buttons: { "Cancel": function() { $(this).dialog("close"); }, 
			   "Add Selected Emails": function() { extractEmails ( ); } 
			 },
		modal: true,
		show: 'scale', //Allowed Values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
		hide: 'scale', //Allowed Values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
		title: mail_provider.substring( 0, 1 ).toUpperCase() + mail_provider.substring( 1 ) + ' Emails Importer',
		height: 350,//290
		width: 487,
		close: function() {
			$(this).dialog("close");
			$("#newDialog").dialog("destroy");
			$("#newDialog").remove();
		},
		dialogClass: 'formDialog'
	});
	

	emailContactsForm ( mail_provider );
	
}


function emailContactsForm ( mail_provider )
{
	
	if ( mail_provider != "facebook" ) {

		//the AJAX request
		$.ajax({
			   type: "POST",
			   url: "../contacts_importer/emailContactsForm.php",
			   data: "r="+Math.floor(Math.random()*9999)+"&mail_provider="+mail_provider,
			   success: function( msg ) {
		
					if ( msg != '' ) {
	
						$("#newDialog").html( msg );
						
					}	   	   
	
			   }
		});

	} else {

		//the AJAX request
		$.ajax({
			   type: "POST",
			   url: "../contacts_importer/class.facebook.php",
			   data: "r="+Math.floor(Math.random()*9999),
			   success: function( msg ) {
		
					if ( msg != '' ) {
	
						$("#newDialog").html( msg );
						
					}	   	   
	
			   }
		});	

	}

}


function showContacts ( )
{

	$("#contactList").html("");
	//$("#errorMessage").html("");
	$("#errorMessage").html("<image src='../contacts_importer/images/progressAnimation.gif'/><a>&nbsp; Loading...<a/>");

	var mail_provider = $("#mail_provider").val();

	//validate mail provider selection
	if ( mail_provider == "" ) {
		//alert ("Please Select a Mail Provider First!");
		$("#errorMessage").html("<image src='../contacts_importer/images/dialog-warning.png'/><a>&nbsp; Please Select a Mail Provider First!<a/>");
		return;
	}
	
	var mail          = $("#mail").val();
	var password      = $("#password").val();

	//validate mail and password are entered
	if ( mail == "" || password == "" ) {
		//alert ("You must complete all the fields!");
		$("#errorMessage").html("<image src='../contacts_importer/images/dialog-warning.png'/><a>&nbsp; You must complete all the fields!<a/>");
		return;
	}
	
	//if it's not a valid email address
	if ( ! validEmail ( mail ) ) {
		$("#errorMessage").html("<image src='../contacts_importer/images/dialog-warning.png'/><a>&nbsp; You must enter a valid email address!<a/>");
		return;	
	}

	//validate mail and provider match
	if ( mail_provider != "facebook" ) { //cause facebook could have any email host as a user.
		if ( mail.search( mail_provider ) == -1 ){
			//alert ("The Provider and the email must match!");
			$("#errorMessage").html("<image src='../contacts_importer/images/dialog-warning.png'/><a>&nbsp; The Provider and the email must match!<a/>");
			return;
		}
	}

	
	
	
	//the AJAX request
	$.ajax({
		   type: "POST",
		   url: "../contacts_importer/class."+mail_provider+".php",
		   data: "mail="+mail+"&password="+password,
		   success: function(msg){
		
				if ( msg == 'error' ) {
	
					$("#errorMessage").html("<image src='../contacts_importer/images/dialog-warning.png'/><a>&nbsp; The user and/or password are wrong!<a/>");
						
				} else {
					
					$("#accordion").accordion( 'activate' , 1 );

					$("#contactList").html("<image src='../contacts_importer/images/progressAnimation.gif'/><a>&nbsp; Loading...<a/>");

					$("#errorMessage").html("");

					buildContactList ( msg );
					 
				}	   	   
	
		   }
	});
	
}

function buildContactList ( msg ) 
{

	var arrData = msg.split("--->");
	
	if ( arrData[0] != "error" ) { 

		var content = "";
		var aRow    = "";

		if ( arrData.length > 0 ) {
		
			content += "<table>";

			content += "<tr>";
			content += "<td><input type='checkbox' onClick='jqCheckAll();' value=''/></td>"
			//content += "<th>Name</th>"
			content += "<th>E-Mail</th>"
			content += "</tr>";

			var personName = "";			

			for ( i = 2 ; i < arrData.length ; i++ ) {
			
				aRow = arrData[i].split("||");

				content += "<tr>";
				content += "<td>";
				content += "<input type='checkbox' value='"+aRow[1]+"' />";
				content += "</td>";

				if ( aRow[0] != "No Name" ) {
					personName = " ("+aRow[0]+")";
				} else {
					personName = "";
				}

				content += "<td id='mail'>";
				content += aRow[1] + personName;
				content += "</td>";

				//content += "<td id='name'>";
				//content += aRow[0];
				//content += "</td>";

				content += "</tr>"; 
				
			}

			content += "</table>";

		}
		
		$("#contactList").html(content);
		
	}
	
}


function extractEmails ( )
{

	previousMails = $("#recipients").val( );
	var newMails = "";

	$("#contactList input:checkbox:checked").each(function(){ 
		if ( this.value != "on" && this.value != "off" && this.value != "" ) {
			if ( previousMails != "" ) {		
				previousMails += "," + this.value;
				newMails      += "," + this.value;
			} else {
				previousMails += this.value;
				newMails      += this.value;
			}
		}
	});
	
	$("#recipients").val( previousMails );
	
	if ( newMails == "" ) { 
		$("#errorMessage").html("<image src='../contacts_importer/images/dialog-warning.png'/><a>&nbsp; Youm must login and select some emails!<a/>");
	} else {
		$("#newDialog").dialog("close");
		$("#newDialog").dialog("destroy");
		$("#newDialog").remove();	
	}

}


checkedFlag = 1;

function jqCheckAll ( )
{
   if ( checkedFlag == 0 ) {
	   
       $("#contactList INPUT[type='checkbox']").attr('checked', false);

       checkedFlag = 1;

   } else {
	   
       $("#contactList INPUT[type='checkbox']").attr('checked', true);

       checkedFlag = 0;
      
   }

   
}

function validEmail ( mail )
{

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if(reg.test(mail) == false) {
		//alert('Invalid Email Address');
		return false;
	} else {
		return true;
	}

}




// shows form to show the Terms of Use & Privacy Policy.
function showTermsAndPrivacyPolicy ( which )
{

	// Terms of Use & Privacy Policy
	if ( which == "terms" ) text = "Terms of Use";
	else text = "Privacy Policy";
	
	var dialog = $('<div style="display:hidden" id="newDialog"></div>').appendTo('body');

	$("#newDialog").html("<image src='../contacts_importer/images/progressAnimation.gif'/><a>&nbsp; Loading...<a/>");
				
	$("#newDialog").dialog({ 
		bgiframe: true,
		buttons: { "Close Window": function() { $(this).dialog("close"); }, 
			   "Accept ": function() { $("#accept_terms").attr('checked', true); $(this).dialog("close");}
			 },
		modal: true,
		show: 'scale', //Allowed Values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
		hide: 'scale', //Allowed Values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
		title: text + '',
		height: 340,//290
		width: 487,
		close: function() {
			$(this).dialog("close");
			$("#newDialog").dialog("destroy");
			$("#newDialog").remove();
		},
		dialogClass: 'formDialog'
	});
	

	loadContent ( which );
	
}


function loadContent ( page )
{

	//the AJAX request
	$.ajax({
		   type: "POST",
		   url: "../"+page+".php",
		   contentType: "application/x-www-form-urlencoded; charset=utf-8",
        	   success: function( msg ) {
		
				if ( msg != '' ) {
	
					$("#newDialog").html( msg );
						
				}	   	   
	
		   }
	});

}


function abrirPopup(direccion, pantallacompleta, herramientas, direcciones, estado, barramenu, barrascroll, cambiatamano, ancho, alto, izquierda, arriba, sustituir){ 
     var opciones = "fullscreen=" + pantallacompleta + 
                 ",toolbar=" + herramientas + 
                 ",location=" + direcciones + 
                 ",status=" + estado + 
                 ",menubar=" + barramenu + 
                 ",scrollbars=" + barrascroll + 
                 ",resizable=" + cambiatamano + 
                 ",width=" + ancho + 
                 ",height=" + alto + 
                 ",left=" + izquierda + 
                 ",top=" + arriba; 
     var ventana = window.open(direccion,"venta",opciones,sustituir); 

}

function abrirFacebook () {
	abrirPopup ("../contacts_importer/class.facebook.php",0,0,0,0,0,0,0,500,580,320,100,1 );
}

// shows form to show the Terms of Use & Privacy Policy.
function showMediaDialog ( url )
{

	var dialog = $('<div style="display:hidden" id="newDialog"></div>').appendTo('body');

	$("#newDialog").html("<image src='../contacts_importer/images/progressAnimation.gif'/><a>&nbsp; Loading...<a/>");
				
	$("#newDialog").dialog({ 
		bgiframe: true,
		//buttons: { "Close Window": function() { $(this).dialog("close"); } },
		modal: true,
		show: 'scale', //Allowed Values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
		hide: 'scale', //Allowed Values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
		//title: 'Media',
		height: 477,
		width: 750,
		close: function() {
			$(this).dialog("close");
			$("#newDialog").dialog("destroy");
			$("#newDialog").remove();
		},
		dialogClass: 'formDialog'
	});
	

	loadMediaContent ( url );
	
}


function loadMediaContent ( url )
{

	//the AJAX request
	$.ajax({
		   type: "GET",
		   url: url,
		   contentType: "application/x-www-form-urlencoded; charset=utf-8",
        	   success: function( msg ) {
		
				if ( msg != '' ) {
	
					$("#newDialog").html( msg );
						
				}	   	   
	
		   }
	});

}

function changeShareStatus ( type, user_id, id )
{

	//type: Video or Document

	if ( $("#txt_status_"+id).html() == 'Deleted' ) return;

	//the AJAX request
	$.ajax({
		   type: "POST",
		   url: "../switchShareStatus.php",
		   data: "",
		   contentType: "application/x-www-form-urlencoded; charset=utf-8",
        	   success: function( msg ) {
				//../img/borrar.png
				//../img/reactivar.png
				
				if ( msg != "" ) { 
				
					var txt = $("#txt_status_"+id).html();
					
					if ( txt == 'Stop Sharing' ) {
		
						$("#img_status_"+id).html('');
						$("#txt_status_"+id).html('Deleted');
													
					} else {
						
						$("#img_status_"+id).html('<img src="../img/borrar.png"/>');
						$("#txt_status_"+id).html('Stop Sharing');
						
					}
			
				} else {
					
					alert("Error! Please Try Again!");
					
				}
	
		   }
	});

}


////////////////////////////////////////////////////////////////////////


// shows form to show the Terms of Use & Privacy Policy.
function editVideoOsym ( vid )
{

	var dialog = $('<div style="display:hidden" id="newDialog"></div>').appendTo('body');

	$("#newDialog").html("<image src='../contacts_importer/images/progressAnimation.gif'/><a>&nbsp; Loading...<a/>");
				
	$("#newDialog").dialog({ 
		bgiframe: true,
		//buttons: { "Close Window": function() { $(this).dialog("close"); } },
		modal: true,
		show: 'slide', //Allowed Values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
		hide: 'slide', //Allowed Values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
		//title: 'Media',
		height: 477,
		width: 750,
		close: function() {
			$(this).dialog("close");
			$("#newDialog").dialog("destroy");
			$("#newDialog").remove();
		},
		dialogClass: 'formDialog'
	});
	

	loadVideoEditContent ( vid );
	
}


function loadVideoEditContent ( vid )
{

	//the AJAX request
	$.ajax({
		   type: "GET",
		   url: baseurl + "osymplayer/OSMFAdminSymposier.php?vid="+vid,
		   contentType: "application/x-www-form-urlencoded; charset=utf-8",
        	   success: function( msg ) {
		
				if ( msg != '' ) {
	
					$("#newDialog").html( msg );
						
				}	   	   
	
		   }
	});

}

function updateOsymUse ( vid, elem )
{
	
	var status = ( elem.checked == true ? 1 : 0 );

	//the AJAX request
	$.ajax({
		   type: "GET",
		   url: baseurl + "newajax/ajax_change_video_display.php?video_id="+vid+"&status="+status,
		   contentType: "application/x-www-form-urlencoded; charset=utf-8",
        	   success: function( msg ) {
		
				if ( msg != '' ) {
	
					$("#newDialog").html( msg );
						
				}	   	   
	
		   }
	});
	
}



