
// Country selector
// =================================================

function getSelectData(theitem, targetContainer){
  var options = document.getElementById(theitem);
  var target = document.getElementById(targetContainer);
  var data = "";
  
  options.style.display = "none";
    
  //cycle trough the select fields
  for(var i=0; i < options.length; i++) {
    if(i == 0) {
      data += "<li class=\"open active\" id=\"" + options[i].value + "\">";      
    } else {
      data += "<li id=\"" + options[i].value  + "\">";
    }
      data += "<a href=\"#\" onclick=\"showSelectContent('" + targetContainer + "', '" + options[i].value + "'); return false;\">";
      data += options[i].text;
      data += "</a></li>" + "\n";
  }
  target.innerHTML = data;
}

function showSelectContent(targetContainer, value) {
  var container = document.getElementById(targetContainer);
  
  if(container.className != "skinnedSelect open") {
    container.className = "skinnedSelect open";
  } else {
    container.className = "skinnedSelect closed";
    selectMe(value);
  }
}

function selectMe(value) {
  // data array is in country.js
  if(value != "null") {
    window.location = country[value];    
  }
}


// Google analytics function
// =================================================
function sendGoogleAnalyticsTrack(method, value) {
  var returnedData; 
  if(method == "html") {
    returnedData = value.replace(/_/, " ");
    data = method + "/" + returnedData;
    __utmLinker(data);    
  } else {
    data = method + "/" + value;
    urchinTracker(data);
  }
}

// Behavior.js rules
// =================================================
var myrules = {
	// add a event onclick for all ahref with class name 'google_analytics'
	'a.google_analytics' : function(el){
		el.onclick = function(){
		  sendGoogleAnalyticsTrack("html", this.name)
		}
	}
};


// target _blank fix to keep pages xhtml valid
// ================================================
function getExternalLinks() {
  links = document.getElementsByTagName('a');

  for(i = 0; i < links.length; i++) {
    if(links[i].rel == "external") {
      e = links[i];
      e.setAttribute("target", "_blank");
    }    
  }
}


// Ticker Function
// =================================================
currentItem = 0;
var tickerLoop;

function initTicker(parentId, tickerClassName) {
  var loopTime = 2500;    
  tickerLoop = setInterval('activeTickerItem(\'' + parentId + '\',\'' + tickerClassName + '\')', loopTime);
}

function activeTickerItem(parentId, tickerClassName) {
  var items = listTickerItems(parentId, tickerClassName);
  var max = items.length;
    
  if(currentItem < max) {
    activateTickerItem(parentId, currentItem, 'tickerItem'); 
    currentItem++;    
  } else {
    currentItem = 0;
  }
}

//build an array of ticker items
function listTickerItems(parentId, theClassName) {
  // our container unique ID
  var container = document.getElementById(parentId);  
  
  // getter of all LI inside that container
  var tickerItems = container.getElementsByTagName("li");
  
  // return values in array format
  return tickerItems;  
}

// active One element within a list of elements with the same class name
function activateTickerItem(theitem, currentItem, theClassName) {  
  // our container unique ID
  var container = document.getElementById(theitem);  
  
  // getter of all LI inside that container
  var items = container.getElementsByTagName("li");
  
  //hide all the element
  for(i = 0; i < items.length; i++) {
    items[i].className = theClassName + '';
  }
  //active the currentItem
  items[currentItem].className = theClassName + ' active';
}


// usefull scripts
// =================================================

// ====== calculate page height ======
function setWrapHeight() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    pageHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    pageHeight = document.documentElement.clientHeight;
  }
  var element = document.getElementById('wrap');
  var height = pageHeight;
  element.style.height = 'auto';
  element.style.height = height + "px";
}

// ====== set flash height ==========
function setFlashHeight(divid, newH){
  newH = newH + 180;
	document.getElementById(divid).style.height = newH+"px";
}

//fixing IE6 bug on Windows XP
function setFlashAutoHeight(divid){
  setWrapHeight();
	document.getElementById(divid).style.height = "100%";
}


// ====== Check Flash ======
function checkFlash() {
  //flash detect
  flash_versions = 20;

  // Initialize variables and arrays
  var flash = new Object();
  flash.installed=false;
  flash.version='0.0';

  // Dig through Netscape-compatible plug-ins first.
  if (navigator.plugins && navigator.plugins.length) {
  	for (x=0; x < navigator.plugins.length; x++) {
  		if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
  			flash.version = navigator.plugins[x].description.split('Shockwave Flash ')[1];
  			flash.installed = true;
  			break;
  		}
  	}
  }

  // Then, dig through ActiveX-style plug-ins afterwords
  else if (window.ActiveXObject) {
  	for (x = 2; x <= flash_versions; x++) {
  		try {
  			oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
  			if(oFlash) {
  				flash.installed = true;
  				flash.version = x + '.0';
  			}
  		}
  		catch(e) {}
  	}
  }

  // Create sniffing variables in the following style: flash.ver[x]
  // Modified by mjac
  flash.ver = Array();
  for(i = 4; i <= flash_versions; i++) {
  	eval("flash.ver[" + i + "] = (flash.installed && parseInt(flash.version) >= " + i + ") ? true : false;");
  }
  
  if (flash.installed) {
  	//alert(flash.version);
  	flashInstalled = true;
  } else {
  	// Flash is NOT installed.  Do something else.
  	flashInstalled = false;	
  }  
}


// ============================================================
// onload cross browser
// ============================================================
// will make sure the DOM is ready
// Dean Edwards/Matthias Miller/John Resig
// source > http://dean.edwards.name/weblog/2006/06/again

function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer
    if (_timer) clearInterval(_timer);

    // ========== onload events start here ==========
    	
    	// function for resizing the #wrap onLoad
      setWrapHeight();
    
      // init events registration for behavior.js 
      Behaviour.register(myrules);

      // this function add a target blank to rel="external" links
    	// important to keep the page XHTML valid
    	getExternalLinks();  

      // set an interval every 50 milliseconds checking if google's checkUrchin is available
      // the functopm checkUrchin is inline
      if (typeof(checkUrchin) != 'undefined') {
        checkUrchinInterval = window.setInterval(checkUrchin, 50);
      }
      // google analytics variables
      _ulink = 1;
      _udn = "none";
   
    // ========== onload events end ==========
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onLoad = init;

