var genderSel = "0";
var styleSel = "0";
var armorSkill = "";
var slotSel = -1;

// ****
// Armor shop Ajax
// ****
function queryArmorShop(actionid) {
   var cookieValue = jQuery.cookie("armorShop");
   var phpArmorString = new Array(0,0,0,0,0).join('~');
   var phpJewelString = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0).join('~');
   
   if (cookieValue!=null) {      
      tempstr = cookieValue.split('#');
      phpArmorString = tempstr[0];
      phpJewelString = tempstr[1];
  }

   var ids = { actionid: actionid, armors: phpArmorString, jewels: phpJewelString, langkey: getGlobalLangKey() };

	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/armorShoppingCart.php",
		data: ids,
		success: function(resultCodes) {
		   var resultArray = resultCodes.split('~');
		   //alert(resultArray[0] + "\n" + resultArray[1]);
		   armorSC = resultArray[1].split('#');
		   jewelsSC = resultArray[2].split('#');
         armorSCFill(resultArray[3]);
         cookieValue = armorSC.join('~') + '#' + jewelsSC.join('~');
         jQuery.cookie("armorShop",cookieValue,{ expires: 30});
		}
	});	
}


// ****
// Initialize the Popup Windows
// ****
jQuery(document).ready(function(){
   if (jQuery("#mbInfoPanel").length!=0) {
              
      jQuery("#mbInfoPanel").buildContainers({
         containment:"document",
         elementsPath:"/wp-content/elements/"
      });
      jQuery("#mbInfoPanel").mb_toggle();
      jQuery(window).scroll(function(){
   			jQuery("#mbInfoPanel")
   				.stop().wait(1000)
   				.animate({"marginTop": (jQuery(window).scrollTop() - 100) + "px"}, "fast" );
   		});
      jQuery("body").append(jQuery("#mbInfoPanel"));
      jQuery("#mbArmorShop").buildContainers({
         containment:"document",
         elementsPath:"/wp-content/elements/"
      });
      jQuery(window).scroll(function(){
   			jQuery("#mbArmorShop")
   				.stop().wait(1000)
   				.animate({"marginTop": (jQuery(window).scrollTop() - 100) + "px"}, "fast" );
   		});
      jQuery("body").append(jQuery("#mbArmorShop"));
      jQuery("#mbArmorShop").mb_close();
   }
});

// ****
// tree navigable weapon paths
// ****
function makeTree() {
   jQuery("#weaponTable").treeTable({
      initialState: "expanded",
      expandable: false
   });
}

// ****
// load armor segment
// ****
function loadArmorSegment(segment, objectID, full) {
      var ids = { geartype: segment.toUpperCase(),
      objectid: objectID,
      langkey: getGlobalLangKey(),
      full: full,
      genderSel: genderSel,
      styleSel: styleSel,
      armorSkill: armorSkill,
      slotSel: slotSel,
      filtername: jQuery("#filtername").val()
      };
   
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/fetch_gear_mhfu.php",
		data: ids,
		success: function(htmlcontent) {
		   jQuery("#"+segment).html(htmlcontent);
         makeSortable();
		}
	});
}

function loadItemViaName(listElement, idtag) {
   var ids = { objectname: listElement.innerHTML, langkey: getGlobalLangKey() };
   loadFelyne(idtag);
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/fetch_iteminfo_mhfu.php",
		data: ids,
		success: function(html) {
         jQuery(idtag).html(html);
		}
	});         
}

// ****
// load item info for objectID into idtag
// ****
function loadItemInfo(objectID, idtag) {
   var ids = { objectid: objectID, langkey: getGlobalLangKey() };
   loadFelyne(idtag);
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/fetch_iteminfo_mhfu.php",
		data: ids,
		success: function(html) {
         jQuery(idtag).html(html);
		}
	});
}

function makeAutocomplete() {
   jQuery("#searchitem").autocomplete("http://www.reign-of-the-rathalos.com/wp-content/item_suggestions_new.php", 
   { minChars: 2,
     cacheLength: 0,
     extraParams: {langkey: getGlobalLangKey()},
     onItemSelect: function(listItem) {
          loadItemViaName(listItem, "#itemdatacontent");   }
      });
}

// ****
// basic load gear
// ****
function loadGear(geartype, objectID, full, idtag) {
   if (idtag==="") {
      idtag = "#datacontent";
   }
   jQuery('#mainTab').tabs().tabs('select', 0);
   loadFelyne(idtag);
   var ids = { geartype: geartype,
      objectid: objectID,
      langkey: getGlobalLangKey(),
      full: full,
      genderSel: genderSel,
      styleSel: styleSel,
      armorSkill: armorSkill,
      slotSel: slotSel
      };

	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/fetch_gear_mhfu.php",
		data: ids,
		success: function(htmlcontent) {
		   jQuery(idtag).html(htmlcontent);
         makeSortable();         
         //makeTree();
		},
		complete: function() {		   
         
      }
	});
}
// ****
// load all armors
// ****
function loadArmors(objectID, full) {

   loadFelyne("#helmet");
   loadFelyne("#gauntlet");
   loadFelyne("#waist");
   loadFelyne("#plate");
   loadFelyne("#jewel");
   loadFelyne("#leg");
   
   loadArmorSegment("helmet", objectID, full);	
   loadArmorSegment("plate", objectID, full);	
   loadArmorSegment("waist", objectID, full);	
   loadArmorSegment("gauntlet", objectID, full);	
   loadArmorSegment("leg", objectID, full);	
   loadArmorSegment("jewel", objectID, full);	
}

// ****
// Monster Selection 
// ****
function createMonsterList(idtag) {
   var ids = { langkey: getGlobalLangKey(), type: "monsterSelect" };
   loadFelyne(idtag);

	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/weaponCalcTools.php",
		data: ids,
		success: function(html) {
		   jQuery(idtag).html(html);
		   jQuery('#monsterSelect').change(function() {
		      getMonsterData("#monsterdatacontent");
		   });
		   getMonsterData("#monsterdatacontent");
		}
	});
}

function getMonsterData(idtag) {
   var monsterid = jQuery("#monsterSelect").val();
   getMonsterDataID(monsterid, idtag, 0);
}

function getMonsterDataID(monsterid, idtag, flagged) {
   var ids = { langkey: getGlobalLangKey(), 
      monsterID: monsterid};
   if (flagged==1) {
      jQuery('#mainTab').tabs().tabs('select', 3);      
   }
   loadFelyne(idtag);   
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/monsterData.php",
		data: ids,
		success: function(html) {
		   jQuery(idtag).html(html);		   
		}
	});
}
