// Unified JQuery system, for dynamic loading of objects, web-pages, etc into DIV, SPAN, etc elements, supporting REQUEST and POST data.
// This system utilizes JQUery methods, plus dynamic caching, to reduce the requests to the server, thus the server load
// (once a module is loaded in the browser, it remains cached and it's not requested by the server nor does it load again, for as
// long as the user remains in the same page).

$(document).ready(function() {
  $("#loading").bind("ajaxSend", function() {
    $(this).show();
 	}).bind("ajaxComplete", function() {
    $(this).hide();
 	});
})


// Load a URL with an XML-http request, with parameters passed to this URL, using AJAX & JQuery, with a fading animation.
// We also style the form controls with "Uniform"
function loadAJAX (file,object) {
  $("#"+object).hide();
  $("#"+object).load (file, function() {
    $("#"+object).fadeIn("slow");
  });
  $("#"+object).load (file, function() {
    $("#"+object+" :select").uniform();
  	$("#"+object+" :radio").uniform();
  	$("#"+object+" :checkbox").uniform();
  });
  return file;
  return object;
}


// Same function as above, but without the fading animation, to avoid z-index and textarea focus issues, when loading
// elements using TinyMCE or other such other libraries/editors.
// This is also needed in cases where we dynamically have to toggle the object's visibility on mouse-over, like in the
// bottom "user menu" floating bar.
// We do not style the form controls with "Uniform" in this function, to avoid issues with 3rd-party libraries.
function loadAJAX_no_anim (file,object) { 
  $("#"+object).load (file);
  return file; 
  return object;
}

// Similar function as the above, to load Text Ads, avoiding issues with javascript files passed to the Ad text
function loadAD (file,object) {
  $(document).ready(function() {
    $.get(file, function(contents){
    var div = document.getElementById(object);  
      div.innerHTML = contents;
      var x = div.getElementsByTagName("script");
		  var h = document.getElementById("head")[0];
		  for(var i=0;i<x.length;i++) {
			var s = document.createElement("script");
			s.type="text/javascript";
			if(x[i].src != '') {
        s.src = x[i].src;
			}
			h.appendChild(s);
			s.text=x[i].text;
		}
   });
  });
}

// Post data to a URL and then load this URL, using AJAX & JQuery.
function postAJAX(postdata,file,object) {
  $("#"+object).hide();
   $.ajax({
    type: "POST",
    url: file,
    data: postdata,
    success: function(msg) {
      $("#"+object).html(msg);
      $("#"+object).fadeIn();
      $("#"+object+" :select").uniform();
    	$("#"+object+" :radio").uniform();
    	$("#"+object+" :checkbox").uniform();
    }
  });
}


// Globally used function, to add a video to the "Quicklist"
function add_to_quicklist(id) {
  var video_id = id;
  var url = "http://www.cinehube.com/site/modules/global_add_to_qlist.php?ID="+video_id;
  var div_id = "add_to_qlist_"+video_id;
  loadAJAX(url, 'quicklist');
  document.getElementById(div_id).style.backgroundImage = 'url(http://www.cinehube.com/site/templates/Cinehube/images/qlist_added.gif)';
  document.getElementById(div_id).style.backgroundRepeat = "no-repeat";
}


// Function to pop-up a thickbox player window, used for the "QuickPlay" function
function quickplay(id) {
  var video_id = id;
  var URL = "http://www.cinehube.com/site/embed.php?video="+video_id+"&width=465&height=345&TB_iframe=true&amp;height=393&amp;width=456";
  tb_show("VistaRápida", URL); 
}

// Function to pop-up a thickbox login box (used in the AJAX authentication module, for checking permissions inside page modules)
function raise_login_box() {
  var loginURL = "http://www.cinehube.com/site/login/?show_mini=1&amp;keepThis=true&amp;TB_iframe=true&amp;height=307&amp;width=400";
  tb_show("Inicie Sesión", loginURL); 
}


// Simple function to show and hide a div or span element
function show_hide(id) {

  var more_img = "http://www.cinehube.com/site/templates/Cinehube/images/show_more.png";
  var less_img = "http://www.cinehube.com/site/templates/Cinehube/images/show_less.png";

  el = document.getElementById(id);
           
  if (el.style.display == 'none') {
    el.style.display = '';
    el = document.getElementById('showhide_' + id);
    el.innerHTML = '<img border="0" align="absbottom" src='+less_img+' />';
  } else {
    el.style.display = 'none';
    el = document.getElementById('showhide_' + id);
    el.innerHTML = '<img border="0" align="absbottom" src='+more_img+' />';
  }
}


// Same function as above, for the "extra info", in "view video", "view photo" o "read blog" pages
function show_hide_media_info(id) {

  var more_img = "http://www.cinehube.com/site/templates/Cinehube/images/show_more.png";
  var less_img = "http://www.cinehube.com/site/templates/Cinehube/images/show_less.png";

  el = document.getElementById(id);
           
  if (el.style.display == 'none') {
    el.style.display = '';
    el = document.getElementById('showhide_' + id);
    el.innerHTML = '<img border="0" align="absbottom" src='+less_img+' />';
    el = document.getElementById('more_less_text');
    el.innerHTML = 'Menos';
    el = document.getElementById('descr_trimmed');
    el.style.display = 'none';
  } else {
    el.style.display = 'none';
    el = document.getElementById('showhide_' + id);
    el.innerHTML = '<img border="0" align="absbottom" src='+more_img+' />';
    el = document.getElementById('more_less_text');
    el.innerHTML = 'Más';
    el = document.getElementById('descr_trimmed');
    el.style.display = '';
  }
}

// Commonly used function for the "Search suggestions"
function search_suggest_onclick() {
  var search_id = document.getElementById('search_id').value;
  var post_data = "search_id="+search_id;
  var post_url = 'http://www.cinehube.com/site/modules/global_search_suggestions.php';
  postAJAX(post_data,post_url,'search_suggestions');
}


// Function to stylize form data, using the "Uniform" JQuery plugin, ONLY after the <body> has fully loaded,
// to avoid problems with post data in forms where non-valid XHTML is used.
// "not([size='10']" is a nasty hack, not to apply "Uniform" in the multi-row select box of the "invite people to Channel" page
function do_uniform() {
$(function(){
	$("select:not([size='10']), input:radio, input:checkbox").uniform();	
});
}
