// ****
// 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>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);
   });
	//jQuery('body').raptorize();

});//document.ready

// **** 
// 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;
}

// ****
// Background image transition plugin
// ***
jQuery.extend({
    BgImageTransitions: []
});

jQuery.fn.extend({
    BgImageTransition: function(src, options) {
        if( !src ){ return jQuery; }

        //copy css from the element to the helper element function
        function copyCSS( from, to ){
            jQuery(['border-bottom-color', 'border-bottom-style', 'border-bottom-width', 'border-left-lolor',
            'border-left-style', 'border-left-width', 'border-right-color', 'border-right-style',
            'border-right-width', 'border-spacing', 'border-top-color', 'border-top-style',
            'border-top-width', 'bottom', 'height', 'left', 'margin-bottom', 'margin-left',
            'margin-right', 'margin-top', 'marker-offset', 'max-height', 'max-width', 'min-height',
            'min-width', 'opacity', 'outline-color', 'outline-offset', 'outline-width',
            'padding-bottom', 'padding-left', 'padding-right', 'padding-top', 'position', 'right',
            'top', 'width', 'z-index']).each( function(i,v){
                jQuery(to).css( v, jQuery(from).css( v ) );
            });
        }

        //make sure there is a zIndex set - we will the helperElement to be *above* the original one
        if( !this.css('zIndex') ){
            this.css('zIndex',1);
        }

        //default plugin settings
        var settings = jQuery.extend({
            effect: {opacity: 'toggle'},
            duration: 'slow',
            easing: 'linear',
            callback: function(){},
            helperElementId: this.attr('id')+'2',
            zindex: parseInt(this.css('zIndex'),10)+1
        }, options);

        //check the bgImageTransition array and see whether there is already a helperElement for the original one. Generate one, if not
        var helperElement = null;
        if( !jQuery.BgImageTransitions[this.attr('id')] ){
            helperElement = this.clone();
            copyCSS( this, helperElement );
            helperElement.css('zIndex', settings.zindex);
            helperElement.css('display', 'none');
            helperElement.attr('id', settings.helperElementId );
            helperElement.insertAfter(this);
            jQuery.BgImageTransitions[this.attr('id')] = helperElement;
        }
        else{
            helperElement = jQuery.BgImageTransitions[this.attr('id')];
        }

        //load the image file into cache first, so that we get a nice and fast load. Make the transition when the image has been loaded in cache
        var tempImage = new Image();

        jQuery(tempImage).load( function(){
            var newImage = ( helperElement.css('display') == 'block' ) ? jQuery(this) : jQuery(helperElement);
            newImage.css('backgroundImage', 'url('+tempImage.src+')');
            helperElement.animate( settings.effect, settings.duration, settings.easing, settings.callback );
        });

        tempImage.src = src;

        return jQuery;
    }
});

jQuery(document).ready(function(){	
	// ****
	// Background image rotator
	// ****

	
	
  var bgImages = [ 'header.jpg', 'header2.jpg', 'header.jpg', 'header2.jpg'];
  var currImageCount = 0;    
  setInterval( function(){    		
      currImageCount++;
      if (currImageCount>bgImages.length-1) {
      	currImageCount=0;
      }
      jQuery('.art-header-jpeg').BgImageTransition( 'http://www.reign-of-the-rathalos.com/wp-content/themes/RotR/images/'+bgImages[currImageCount] );
      if (bgImages[currImageCount]=="header2.jpg") {
      	jQuery('.art-header-jpeg').click(function(){
      			window.open("http://www.reign-of-the-rathalos.com/mhp3/mhp3-supporters-page/");
      		}).css("cursor","pointer");
      	jQuery(".art-logo").hide();
      } else {
      	jQuery('.art-header-jpeg').click(function(){}).css("cursor","auto");
      	jQuery(".art-logo").show();
      }
  }, 10000);

	
	
	// ****
	// Support options 
	// ****
	
	jQuery("#supportOptions").hide();
	jQuery("#supportClick").click(function() {
		jQuery("#supportOptions").slideToggle();	
	});
	jQuery('.twitter_search').twitterpopup();

});

