$(function(){
	var TrrWiki = (function(){
		// Elements IDīs
		var wikiIframeId      = "#wiki-trr-iframe",
			wikiClose         = "#wiki-close",
			wikiAdvTitle      = "#wiki-adv-title",
			wikiHeaderURL     = "#wiki-header-url",
			wikiHeaderWelcome = "#wiki-trr-welcome";

		// Query String Parse
		var strLocation       = document.location.toString(),
			arrLocation       = strLocation.split('/'),
			strWikiType       = arrLocation[3],
			strWikiURL        = arrLocation[4],
			strLogoURL        = '',
			strLanguage       = '',
			strHeaderLanguage = '',
			wikiCountry       = '', 
			wikiDomain        = strWikiType + ".org"; // Will be tested when the other types is supported (dictionary, media, etc)
			
		// Apply header logo link
		strLogoURL = document.domain.toString().replace("dsv-webx1.",''); // Remove this implementation when deploy
		//strLogoURL = "http://" + ((strLogoURL.replace("wm.",'www.') == "www.terra.com") ? "en.terra.com" : strLogoURL.replace("wm.","www."));
		strLogoURL = "http://" + strLogoURL.replace("wm.","www.");
		
		
		// Get the querystring
		if(typeof(strLocation.split('?')[0]) != "undefined"){
			
			var regexDomain = /.*\?(lang=|cab=)([a-z]{0,2})(\&(cab=|lang=)([a-z]{0,2}))?/, // RETURNS: ["http://wm.dsv-webx1.ter...dia/Blog?cab=en&lang=es", "cab=", "en", "&lang=es", "lang=", "es"]
				arrResult   = strLocation.match(regexDomain);
	
			if(arrResult != null){
				$.each(arrResult, function(index, item){
					if(item == "lang="){
						strLanguage = arrResult[index+1];
					}
					if(item == "cab="){
						// The cab parameter is valid only for terra.com & en.terra.com
						strHeaderLanguage = (arrResult[index+1] == "en" && (strLogoURL == "http://www.terra.com/" || strLogoURL == "http://www.terra.com" || strLogoURL == "http://en.terra.com/" || strLogoURL == "http://en.terra.com")) ? "en" : '';
						
						// Apply logoUrl again if cab = en
						strLogoURL = (strHeaderLanguage == "en") ? "http://en.terra.com" : strLogoURL;
					}
				});
			}
		}
		
		$(wikiHeaderURL).attr("href",strLogoURL);
		
		// About header configuration 
		if(strHeaderLanguage != ''){
			$(wikiHeaderWelcome).html(TrrWikiDictionary[strHeaderLanguage].headerWelcome);
			$(wikiClose).html(TrrWikiDictionary[strHeaderLanguage].close);
			$(wikiAdvTitle).html(TrrWikiDictionary[strHeaderLanguage].advertisement);
		}else{
			$(wikiHeaderWelcome).html(TrrWikiDictionary[wikiLanguage].headerWelcome);
			$(wikiClose).html(TrrWikiDictionary[wikiLanguage].close);
			$(wikiAdvTitle).html(TrrWikiDictionary[wikiLanguage].advertisement);
		}
		
		// Verifiy if attribute lang is sended on request
		if(typeof(strLanguage) != "undefined"){
			// Permited languages
			var arrLanguages = {
				en : "en",
				es : "es",
				pt : "pt"
			};
			
			// Validate sended language
			if(typeof(arrLanguages[strLanguage.replace("lang=",'')]) != "undefined"){
				wikiDomain =  "http://" + arrLanguages[strLanguage.replace("lang=",'')] + "." + wikiDomain;			
			}else{
				wikiDomain =  "http://" + wikiLanguage + "." + wikiDomain;
			}
		}else{
			wikiDomain =  "http://" + wikiLanguage + "." + wikiDomain;
		}
		
		//Iframe resize function
		var resizeIframe = function(){
			$(wikiIframeId).css("width",$(window).width() + 'px');
		};
		
		// Create iframe URL destination
		try{strWikiURL = strWikiURL.split('?')[0];}catch(i){}
		
		var iframeURL = wikiDomain + '/wiki/' + strWikiURL;
		$(wikiIframeId).attr("src",iframeURL);

		if(navigator.appName == "Microsoft Internet Explorer"){
			resizeIframe();
			window.onresize = resizeIframe;
		}
		
		// Add iframe source URL and close button href
		$(wikiClose).attr("href",iframeURL);
	})();
});
