/*######################################################################
#                        COUNTRY SELECTOR                              #
########################################################################
#       Autor:        Frank Herrmann                                   #
#       Homepage:     www.codeking.eu                                  #
########################################################################
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 3 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# This program is distributed in the hope that it will be useful,      #
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        #
# GNU General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU General Public License    #
# along with this program; if not, see <http://www.gnu.org/licenses/>  #
# or write to the                                                      #
# Free Software Foundation, Inc.,                                      #
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.            #
#######################################################################*/

  var flagPath = baseURL + '/scripts/countries/flags';
  var ie6 = window.clipboardData && document.compatMode && !window.XMLHttpRequest;
  var selCountry = new Array(), selInput = null;

  var country = {

    initStructure: function(input) {    
    // div structures
      if(!document.getElementById('countryList_' + input))
      {
        var countryList = document.createElement('div');
            countryList.setAttribute('id', 'countryList_' + input);
            countryList.className = 'countryList';
        document.body.appendChild(countryList);

      // insert countries
        var thisRegion = '';
        var selcountries = document.getElementById(input).getElementsByTagName('option');
        for(var i=0;i<selcountries.length;i++) {
          if(selcountries[i].className == 'kat') {
            thisRegion = selcountries[i].value + '_' + input;
            var region = document.createElement('div'); region.className = 'region';
            var h2 = document.createElement('h2'); h2.className = selcountries[i].value; h2.innerHTML = selcountries[i].innerHTML;
            var region_scroll = document.createElement('div'); region_scroll.setAttribute('id', thisRegion); region_scroll.className = 'region_scroll';

            region.appendChild(h2); region.appendChild(region_scroll);
            document.getElementById('countryList_' + input).appendChild(region);
          } else {
            var iso = selcountries[i].className.replace(/cc\_/, '');
            var thisCountry = document.createElement('span');
                thisCountry.setAttribute('dir', selcountries[i].value);
                thisCountry.setAttribute('title', selcountries[i].innerHTML + '(' + iso + ')');
                thisCountry.style.backgroundImage = 'url(' + flagPath + '/' + iso.toLowerCase() + '.gif)';
                thisCountry.innerHTML = '<b class="cc_' + iso + '">' + selcountries[i].innerHTML + '</b>';
                thisCountry.onmouseover = function() { this.className = 'country_hover'; }
                thisCountry.onmouseout = function() { this.className = ''; }
                thisCountry.onclick = function() { country.set(this); }

            document.getElementById(thisRegion).appendChild(thisCountry);
          }
        }
      }
    },

    initCountry: function(input) {
    // selected element
      var flag = document.createElement('div');
      var sel = document.getElementById(input).options[document.getElementById(input).selectedIndex].value;

      if(sel != null) {
       // var c = document.getElementById('countryList_' + input).getElementsByTagName('span');
        var c = document.getElementById(input).getElementsByTagName('option');
        for(var i=0; i<c.length; i++) {
          if(c[i].innerHTML.indexOf(sel) != -1) {
            var iso = c[i].className.replace(/cc\_/, '');
            flag.style.backgroundImage = 'url(' + flagPath + '/' + iso.toLowerCase() + '.gif)'; flag.innerHTML = c[i].innerHTML;
            document.getElementById('countrySelected_' + input).appendChild(flag);
            break;
          }
        }
      }
      
      if(document.getElementById('countrySelected_' + input).innerHTML.length == 0) {
        flag.style.backgroundImage = c[7].style.backgroundImage; flag.innerHTML = c[7].innerHTML;
        document.getElementById('countrySelected_' + input).appendChild(flag);
      }

      var s = document.getElementById(input).getElementsByTagName('option');
      for(var i=0; i<s.length; i++) selCountry[s[i].value] = i;

      document.getElementById(input).style.visibility = 'hidden';
      document.getElementById('countrySelected_' + input).style.display = 'inline';
      document.getElementById('countrySelected_' + input).onclick = function() {
        var r = this.id.split('_'); r = r[0] + '_';
        country.list(this.id.replace(r, ''));
      }
    },

    list: function(input) {
      country.initStructure(input);
      selInput = input;
      country.buildTransparentLayer();
      document.getElementById('transparent').onclick = function() { country.hide(); }

      var h = country.getPageDimensions();
      if(ie6) document.getElementById('countryList_' + input).style.top = '50%';
      else document.getElementById('countryList_' + input).style.top = (parseInt(h[3] / 2) - 300) + 'px';
      window.setTimeout("document.getElementById('countryList_" + input + "').style.display = 'block';", 20);
    },

    set: function(obj) {
      var value = obj.getAttribute('dir');

      var flag = document.createElement('div');
          flag.style.backgroundImage = obj.style.backgroundImage;
          flag.innerHTML = obj.innerHTML;

      document.getElementById('countrySelected_' + selInput).innerHTML = '';
      document.getElementById('countrySelected_' + selInput).appendChild(flag);
      document.getElementById(selInput).selectedIndex = selCountry[value];
      document.getElementById(selInput).options[document.getElementById(selInput).selectedIndex].value;

      country.hide();
    },

    hide: function() {
      document.getElementById('countryList_' + selInput).style.display = 'none'
      document.getElementById('transparent').style.display = 'none'
    },

    buildTransparentLayer: function() {
      if(!document.getElementById('transparent'))
      {
        var transparent = document.createElement('div');
            transparent.setAttribute('id','transparent');
        document.body.appendChild(transparent);

        country.setOpacity(transparent, 60);
      } else {
        document.getElementById('transparent').style.display = 'block';
      }

      country.setAbsoluteHeight('transparent');
    },

    setAbsoluteHeight: function(objID) {
      var h = country.getPageDimensions();
      return document.getElementById(objID).style.height = h[1] + 'px';
    },

    setOpacity: function(obj, opacity) {
      if(obj)
      {
        obj.style.filter       = "alpha(opacity:"+opacity+")";
        obj.style.KHTMLOpacity = opacity/100;
        obj.style.MozOpacity   = opacity/100;
        obj.style.opacity      = opacity/100;
      }
    },

    getPageDimensions: function() {
    	var xScroll, yScroll, windowWidth, windowHeight;
    	if(window.innerHeight && window.scrollMaxY) {
    		xScroll = document.scrollWidth;
    		yScroll = self.innerHeight + (this.isFrame ? parent.scrollMaxY : self.scrollMaxY);
    	} else if (document.body.scrollHeight > document.body.offsetHeight){
    		xScroll = document.body.scrollWidth;
    		yScroll = document.body.scrollHeight;
    	} else {
    		xScroll = document.getElementsByTagName('html').item(0).offsetWidth;
    		yScroll = document.getElementsByTagName('html').item(0).offsetHeight;
    		xScroll = (xScroll < document.body.offsetWidth) ? document.body.offsetWidth : xScroll;
    		yScroll = (yScroll < document.body.offsetHeight) ? document.body.offsetHeight : yScroll;
    	}
    	if(self.innerHeight) {
    		windowWidth  = self.innerWidth;
    		windowHeight = self.innerHeight;
    	} else if (document.documentElement && document.documentElement.clientHeight) {
    		windowWidth  = document.documentElement.clientWidth;
    		windowHeight = document.documentElement.clientHeight;
    	} else if (document.body) {
    		windowWidth  = document.getElementsByTagName('html').item(0).clientWidth;
    		windowHeight = document.getElementsByTagName('html').item(0).clientHeight;
    		windowWidth  = (windowWidth == 0) ? document.body.clientWidth : windowWidth;
    		windowHeight = (windowHeight == 0) ? document.body.clientHeight : windowHeight;
    	}
    	var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
    	var pageWidth  = (xScroll < windowWidth) ? windowWidth : xScroll;
    	return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    }

  }