jQuery(document).ready(function(){
   jQuery("#xlinkquest").hide();
   jQuery(".draggable").draggable({ containment: '#maintab1', scroll: false });
   jQuery('#maintab1').tabs();
   jQuery('#maintab2').tabs();
   jQuery(".sclick").click(function() {
      jQuery("#xlinkquest").hide();
      jQuery("#adhocquest").show();
      jQuery("#selectedAdhoc").val(jQuery(this).children().html());
   });
   jQuery("#xlink").click(function() {
      jQuery("#xlinkquest").show();
      jQuery("#adhocquest").hide();
   });

   jQuery("#selectedAdhoc").val("Adhoc US");
   jQuery("#hrl").change(function() {
      createQuests();
   });
   jQuery('#time').ptTimeSelect({zindex: 1000});
   jQuery('#date').datepicker();
   jQuery('#xtime').ptTimeSelect({zindex: 1000});
   jQuery('#xdate').datepicker();

   createQuests();
   loadAdhocQuests("Adhoc US");
   loadAdhocQuests("Adhoc EU");
   loadAdhocQuests("Adhoc JP");
   loadXlinkQuests();
   var refreshId = setInterval(function() {
      loadAdhocQuests("Adhoc US");
      loadAdhocQuests("Adhoc EU");
      loadAdhocQuests("Adhoc JP");
      loadXlinkQuests();
   }, 60000);
   jQuery("#commentDialog").dialog({
      autoOpen: false,
      height: 300,
      width: 400,
      modal: false,
      draggable: false
   });
   updateXLang();
});

function addComment(tagid) {
   var ids = { type: "addcomment",
         tagid: tagid,
         author: jQuery("#cnick").val(),
         comment: jQuery("#cmess").val()};

   jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/adhocTools.php",
		data: ids,
		success: function(html) {

		}
	});
	jQuery("#commentDialog").dialog('close');
}

function createQuests() {
   var ids = { type: "quests", hr: jQuery("#hrl").val() };

	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/adhocTools.php",
		data: ids,
		success: function(html) {
		   jQuery("#questselect").html(html);
		}
	});
}

function loadAdhocQuests(adhoc) {
   var ids = { type: "load", adhoc: adhoc,
      hroffset: Math.floor(new Date().getTimezoneOffset())};
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/adhocTools.php",
		data: ids,
		success: function(html) {
         corkboard = "#adhoc" + adhoc.substring(6) + "cork";
         jQuery(corkboard).html(html);
         jQuery(".adhc").each(function(i) {
           jQuery(this).bind('click', {index:this.id}, function(e){
               getComments(e.data.index);
               return false;
            });
         });
		}
	});
}

function loadXlinkQuests() {
   var ids = { type: "xload",
      hroffset: Math.floor(new Date().getTimezoneOffset())};
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/adhocTools.php",
		data: ids,
		success: function(html) {
         jQuery("#xlinkcork").html(html);
         jQuery(".adhc").each(function(i) {
           jQuery(this).bind('click', {index:this.id}, function(e){
               getComments(e.data.index);
               return false;
            });
         });
		}
	});
}

function updateXLang() {
   var mainarena = jQuery("#xmain").val();
   var langOptions = {
   	"English" : "English",
   	"Non-English" : "Non-English"
   }
   if (mainarena=="MHP2G") {
      langOptions = {
      	"English" : "English",
      	"Europe" : "Europe"
      }
   }
   jQuery("#xlang").empty();
   jQuery.each(langOptions, function(val, text) {
      jQuery('#xlang').append(
        jQuery('<option></option>').val(val).html(text)
      );
   });
   jQuery("#xlang")[0].selectedIndex = 0;
   updateXSubs();
}

function updateXSubs() {
   var mainarena = jQuery("#xmain").val();
   var lang = jQuery("#xlang").val();
   var subOptions = {
   	"G Class" : "G Class",
   	"Group Training" : "Group Training",
   	"HR1" : "HR1",
   	"HR2" : "HR2",
   	"HR3" : "HR3",
   	"HR4" : "HR4",
   	"HR5" : "HR5",
   	"HR6" : "HR6",
   	"HR7" : "HR7",
   	"HR8" : "HR8",
   	"HR9" : "HR9",
   	"Treasure Hunting" : "Treasure Hunting"
   }
   if (mainarena == "Unite" && lang == "Non-English") {
      subOptions = {
      	"Europe" : "Europe",
      	"Rest of the World" : "Rest of the World"
      }
   }
   if (mainarena == "MHP2G" && lang == "Europe") {
      subOptions = {
      	"Espana" : "Espana",
      	"French" : "French"
      }
   }
   jQuery("#xsubs").empty();
   jQuery.each(subOptions, function(val, text) {
      jQuery('#xsubs').append(
        jQuery('<option></option>').val(val).html(text)
      );
   });
   jQuery("#xsubs")[0].selectedIndex = 0;
}

function getComments(tagid) {
   if (jQuery("#commentDialog").dialog('isOpen')) {
      return false;
   }
   var ids = { type: "comments", tagid: tagid};

 	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/adhocTools.php",
		data: ids,
		success: function(html) {
		   jQuery("#commentDialog").html(html).dialog('open');
		}
   });
}

function validate() {
   result = true;
   if (jQuery("#world").val()=="" || jQuery("#world").val().toUpperCase()<"A" ||
         jQuery("#world").val().toUpperCase()>"J" ) {
      result = false;
      jQuery("#world").css({'background-color' : '#FFC0CB'});
   } else {
      jQuery("#world").css({'background-color' : 'white'});
   }
   if (jQuery("#lobby").val()=="" || jQuery("#lobby").val()<1 || jQuery("#lobby").val()>64) {
      result = false;
      jQuery("#lobby").css({'background-color' : '#FFC0CB'});
   } else {
      jQuery("#lobby").css({'background-color' : 'white'});
   }
   if (jQuery("#nick").val()=="") {
      result = false;
      jQuery("#nick").css({'background-color' : '#FFC0CB'});
   } else {
      jQuery("#nick").css({'background-color' : 'white'});
   }
   if (jQuery("#date").val()=="") {
      result = false;
      jQuery("#date").css({'background-color' : '#FFC0CB'});
   } else {
      jQuery("#date").css({'background-color' : 'white'});
   }
   if (jQuery("#time").val()=="") {
      result = false;
      jQuery("#time").css({'background-color' : '#FFC0CB'});
   } else {
      jQuery("#time").css({'background-color' : 'white'});
   }

   return result;
}

function validateX() {
   result = true;
   if (jQuery("#xnick").val()=="") {
      result = false;
      jQuery("#xnick").css({'background-color' : '#FFC0CB'});
   } else {
      jQuery("#xnick").css({'background-color' : 'white'});
   }
   if (jQuery("#xroom").val()=="") {
      result = false;
      jQuery("#xroom").css({'background-color' : '#FFC0CB'});
   } else {
      jQuery("#xroom").css({'background-color' : 'white'});
   }

   if (jQuery("#xdate").val()=="") {
      result = false;
      jQuery("#xdate").css({'background-color' : '#FFC0CB'});
   } else {
      jQuery("#xdate").css({'background-color' : 'white'});
   }
   if (jQuery("#xtime").val()=="") {
      result = false;
      jQuery("#xtime").css({'background-color' : '#FFC0CB'});
   } else {
      jQuery("#xtime").css({'background-color' : 'white'});
   }

   return result;
}

function addAdhocQuest() {
   hroffset = Math.floor(new Date().getTimezoneOffset());
   if (!validate()) {
      return;
   }
   var ids = { type: "add",
      hr: jQuery("#hrl").val(),
      world: jQuery("#world").val().toUpperCase(),
      lobby: jQuery("#lobby").val(),
      adhoc: jQuery("#selectedAdhoc").val(),
      nickname: jQuery("#nick").val(),
      date: jQuery("#date").val(),
      time: jQuery("#time").val(),
      quest: jQuery("#quests").val(),
      notes: jQuery("#notes").val(),
      hroffset: hroffset };

	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/adhocTools.php",
		data: ids,
		success: function(html) {
		   loadAdhocQuests(jQuery("#selectedAdhoc").val());
		   clearValues();
		   //jQuery("#selectedAdhoc").val(html);
		}
	});
}

function addXlinkQuest() {
   hroffset = Math.floor(new Date().getTimezoneOffset());
   if (!validateX()) {
      return;
   }
   var ids = { type: "xadd",
      marena : jQuery("#xmain").val(),
      larena : jQuery("#xlang").val(),
      sarena : jQuery("#xsubs").val(),
      nickname: jQuery("#xnick").val(),
      room: jQuery("#xroom").val(),
      monster: jQuery("#xmonster").val(),
      date: jQuery("#xdate").val(),
      time: jQuery("#xtime").val(),
      notes: jQuery("#xnotes").val(),
      hroffset: hroffset };

	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/adhocTools.php",
		data: ids,
		success: function(html) {
		   loadXlinkQuests();
		   clearXValues();
		}
	});
}

function clearValues() {
   jQuery("#world").val("");
   jQuery("#lobby").val("");
   jQuery("#date").val("");
   jQuery("#time").val("");
   jQuery("#notes").val("");
}

function clearXValues() {
   jQuery("#xdate").val("");
   jQuery("#xtime").val("");
   jQuery("#xnotes").val("");
}

