// ****
// Vars for the global language and the armor selector widgets
// ****
var globalLangKey="e";
var flagged = 0;

// ****
// For the spotlight
// ****
var htmlarray = new Array(      
      "<a href='http://www.minegarde.com'><img src='http://www.reign-of-the-rathalos.com/pics/minebanner.jpg'/></a>",
      "<a href='http://mh3.reign-of-the-rathalos.com/creator'><img src='http://www.reign-of-the-rathalos.com/pics/creator.jpg'/></a>",      
      "<b>Kirin giving you the finger? Check out our Video Matrix!</b><br/><a href='http://www.reign-of-the-rathalos.com/mhp2g-mhfu/video-tutorials/'><img src='http://www.reign-of-the-rathalos.com/pics/kirin.jpg'/></a>",
      "<a href='http://www.twitter.com/RotR_News'><img src='http://www.reign-of-the-rathalos.com/wp-content/themes/fmot.jpg'/></a>",
      "<b>Get your own RotR blog!</b><br/><a href='http://blogs.reign-of-the-rathalos.com'><img src='http://www.reign-of-the-rathalos.com/wp-content/themes/blogpic.jpg'/></a>",
      "<b>Felyne your world with FelyneIt!</b><br/><a href='http://www.reign-of-the-rathalos.com/2009/07/felyne-it/'><img src='http://www.reign-of-the-rathalos.com/wp-content/themes/felyneit.jpg'/></a>",
      "<b>Need a new shirt?</b><br/><a href='http://www.reign-of-the-rathalos.com/mhp2g-mhfu/the-supporters/'><img src='http://www.reign-of-the-rathalos.com/wp-content/themes/diehard.jpg'/></a>"
      );

// ****
// Spotlight init and Halloween init
// ****
jQuery(document).ready(function(){
   jQuery("#ann-cont-inner").html(htmlarray[0]).hide();
   jQuery("#ann-cont-inner").fadeIn("slow",function() {
      callNext(0);
   });
   getBlogsUpdate();
});//document.ready

// ****
// Get recent entries for the mu blogs
// ****
function getBlogsUpdate() {
   var ids = { };
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/blog_collector.php",
		data: ids,
		success: function(html) {		   
         jQuery("#blogs_update").html(html);
		}
	});   
}


// **** 
// Wait plugin
// ****
jQuery.fn.wait = function(time, type) {
        time = time || 4000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                jQuery(self).dequeue();
            }, time);
        });
};

// ****
// print plugin
// ****
jQuery.fn.print = function() {
		var html = 	"<HTML>\n<HEAD>\n<style type='text/css'>\n"+
					"</style>"+"\n</HEAD>\n<BODY>\n";
	   html += this.html();
		html = html + "</BODY>\n</HTML>";
		var printWP = window.open("","printWebPart");
		printWP.document.open();
		printWP.document.write(html.replace("238, 238, 238","0, 0, 0"));
		printWP.document.close();
		printWP.print();
		return this;
};

// ****
// Cookie plugin
// ****
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// ****
// Spotlight
// ****
function callNext(index) {
   if (index==htmlarray.length-1) {
      index = -1;
   }
   index++;
   jQuery("#ann-cont-inner").wait().fadeOut("slow", function() {
     jQuery(this).html(htmlarray[index]);
    });

   jQuery("#ann-cont-inner").fadeIn("slow",function() {
      callNext(index);
   });
}

// ****
// Sortable tables
// ****
function makeSortable() {
   jQuery("#sortme").tablesorter( {sortList: [[0,0]],
    headers: {
            7: {  sorter: false },
            8: { sorter: false },
            9: { sorter: false },
            10: { sorter: false }
        }
     } );
}

// ****
// load throbber
// ****
function loadFelyne(idtag) {
   jQuery(idtag).html("<div style='text-align:center;'><img style='border:0px' src='http://www.reign-of-the-rathalos.com/pics/shakalaka.gif'/></div>");
}

// ****
// lazy getter for globallangkey
// ****
function getGlobalLangKey() {
   if (globalLangKey===undefined) {
     globalLangKey = "e";
   }
   return globalLangKey;
}

// ****
// create a specified FAQ
// ****
function createFAQ(faqtype) {
   var ids = { faq: faqtype, langkey: getGlobalLangKey() };
   loadFelyne("#faqdatacontent");
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/faqcreator.php",
		data: ids,
		success: function(html) {
         jQuery("#faqdatacontent").html(html);
         makeSortable();
		}
	});
}

// ****
// Set language text
// ****
function setLanguageText() {
   var lk = getGlobalLangKey();
   var text = "English";
   if (lk=="p") {
      text="MHP2G TeamHGG";
   }
   if (lk=="g") {
      text="Deutsch";
   }
   if (lk=="f") {
      text="Fran&ccedil;ais";
   }
   if (lk=="s") {
      text="Espa&ntilde;ol";
   }
   if (lk=="i") {
      text="Italiano";
   }

   jQuery("#langfield").html(text);
}

// ****
// skillselection for armors
// ****
function createSkillSelector(idtag) {
   var ids = { langkey: getGlobalLangKey() };
   loadFelyne(idtag);

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

// ****
// helper for skill machine
// ****
function testSkillmachine() {
   var ids = { langkey: getGlobalLangKey(), gender: '0', wtype: '1',
      skillid1: '500190', skillid2: '500133', skillid3: '500128', skillid4: '' };
   loadFelyne("#faqdatacontent");

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

// ****
// load ingredients into popup
// ****
function loadIngredientsMB(objectID, idtag, domobj) {
   var ids = { objectid: objectID, langkey: getGlobalLangKey() };
   containerShow(jQuery("#mbInfoPanel"));
   loadFelyne("#mbInfoPanel .content");
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/fetch_ingr_mhfu.php",
		data: ids,
		success: function(html) {
         containerFill(html, domobj);
      }
	});
}

// ****
// clear popup
// ****
function containerClear() {
   //jQuery("#mbInfoPanel .content").html("<br/>Hover over an element!");
}

// ****
// show specific popup
// ****
function containerShow(container) {
   if (container.mb_getState("closed")) {
      container.mb_open();
      container.mb_moveTo(160,600);
   }
   if (container.mb_getState("collapsed")) {
     container.mb_toggle();
   }
   container.fadeTo(0,1);
}

// ****
// fill mbinfopanel
// ****
function containerFill(html, domobj) {
   containerShow(jQuery("#mbInfoPanel"));
   jQuery("#mbInfoPanel .content").html(html);
}

// ****
// fill mbarmorshop
// ****
function armorSCFill(html) {
   containerShow(jQuery("#mbArmorShop"));
   jQuery("#mbArmorShop .content").html(html);
}

// ****
// skill description into popup
// ****
function loadSkillDescriptionMB(objectID, idtag, domobj) {
   var ids = { objectid: objectID, langkey: getGlobalLangKey() };
   containerShow(jQuery("#mbInfoPanel"));
   loadFelyne("#mbInfoPanel .content");
	jQuery.ajax({
		type: "post",
		url: "http://www.reign-of-the-rathalos.com/wp-content/fetch_skillinfo.php",
		data: ids,
		success: function(html) {
         containerFill(html, domobj);
      }
	});
}


// ****
// initialize language
// ****
function setGLK(keyvalue) {
   globalLangKey = keyvalue;
   makeAutocomplete();
   setLanguageText();
}

// ****
// browser check
// ****
function isBrowserOK() {
   var browser=navigator.appName;
   var b_version=navigator.appVersion;
   var version=parseFloat(b_version);
   if (browser=="Microsoft Internet Explorer") {
      return 1;
   }
   return 0;
}

