
var tabs = new Array();


function $(id) {
    return (document.getElementById)? document.getElementById(id) : document.all[id];
}

function removeElementById(id) {
    var node = $(id);
    if ((node) && (node.parentNode)) {
        node.parentNode.removeChild(node);
        return true;
    }
    return false;
}


var inputSearchKeyEvt = {
    init: function() {
        if (!($('illykey'))) return;
        var field = $('illykey');
        field.onfocus = function() {
            if (this.value == this.title) this.value = "";
            this.style.border = '1px #cdcdcd solid';
            $('bttcerca').style.border = '1px #cdcdcd solid';
        }
        field.onblur = function() {
            val = this.value.replace(/^\s+/,'').replace(/\s+$/,'');
            if (val == '') this.value = this.title;
            this.style.border = '1px #ffffff solid';
            $('bttcerca').style.border = '1px #ffffff solid';
        }
    }
}

var inputNLKeyEvt = {
    init: function() {
        if (!($('illynewsletter'))) return;
        var field = $('illynewsletter');
        field.onfocus = function() {
            if (this.value == this.title) this.value = "";
        }
        field.onblur = function() {
            val = this.value.replace(/^\s+/,'').replace(/\s+$/,'');
            if (val == '') this.value = this.title;
        }
    }
}



var slideShow = {

    currphoto       : 0,    /*** foto visualizzabili insieme ***/
    displayphoto    : 0,    /*** foto visualizzabili insieme ***/
    numphoto        : 0,
    photo           : [],
    id              : null,

    init: function(d, id) {

        this.id = (id || 'slideshow');
        $(this.id).className.concat(' json');
        this.displayphoto = d;

        var f = $(this.id).getElementsByTagName('li');
/*         for (var i=0; i<f.length; i++) {
            var w = f[i].getElementsByTagName('img')[0].offsetWidth;
            if (w != 96) {
                $('slideshow').style.width = '504px';
                //$('slideshow').style.overflow = 'hidden';
                f[i].style.width = w + 'px';
            }

        }
 */
        this.numphoto = f.length;


        if (this.numphoto <= this.displayphoto) {
            $('sshow_next').style.visibility = 'hidden';
            $('sshow_prev').style.visibility = 'hidden';
        }

        for(i=0; i<this.numphoto; i++) {
            this.photo.push(f[i]);
        }

        $('sshow_prev').style.visibility = 'hidden';
        this.resetGallery();
    },

    resetGallery: function() {

        //alert('qui');
        var el = $(this.id);
        /*  while (el.getElementsByTagName('li').length > 0) {
            el.removeChild(el.getElementsByTagName('li')[0])
        } */
        for (i = 0; i<el.getElementsByTagName('li').length; i++) {
            el.getElementsByTagName('li')[i].style.display = 'none';
        }

        for (i=0; i<this.displayphoto;  i++) {

            show = this.currphoto + i;
            if (this.photo[show])
                // el.appendChild(this.photo[show]);
                el.getElementsByTagName('li')[show].style.display = 'block';
        }
    },

    prev: function() {

        if (this.currphoto > 0) {
            this.currphoto = (this.currphoto > 0)? (this.currphoto - 1) : this.numphoto-1;
            this.resetGallery();
            $('sshow_prev').style.visibility = 'visible';
        }

        if (this.currphoto < (this.numphoto - this.displayphoto)) {
            $('sshow_next').style.visibility = 'visible';
        }
        if (this.currphoto == 0) {
            $('sshow_prev').style.visibility = 'hidden';
        }
        return false;
    },

    next: function() {

        if (this.currphoto < (this.numphoto - this.displayphoto)) {
            this.currphoto = (this.currphoto < (this.numphoto-1))? (this.currphoto + 1) : 0;
            this.resetGallery();
            $('sshow_prev').style.visibility = 'visible';
        }
        if (this.currphoto > 0) {
            $('sshow_prev').style.visibility = 'visible';
        }
        if (this.currphoto == (this.numphoto - this.displayphoto)) {
            $('sshow_next').style.visibility = 'hidden';
        }
        return false;
    }
}


/** tram slishow video ***/


function VideoSlideShow () {
  this.currphoto = 0;    /*** foto visualizzabili insieme ***/
  this.displayphoto = 0;    /*** foto visualizzabili insieme ***/
  this.numphoto = 0;
  this.divname= '';
  this.idlist = '';
  this.photo = [];
  this.elPrev;
  this.elNext;

  this.init = initVideoSS;
  this.resetGallery = resetGallery;
  this.prev = prev;
  this.next = next;
  //this.loadImg = loadImgSS;
}

function loadImgSS (ssObj) {
  if (!tabs[ssObj.divname]) {
    tabs[ssObj.divname] = true;
    for (var i =0; i<ssObj.photo.length; i++) {

      var imgs = ssObj.photo[i].getElementsByTagName('img');
      for (var j=0; j<imgs.length; j++) {
        if (imgs[j].id && imgs[j].id.indexOf(ssObj.divname) != -1) {
          //alert("video/thumb/"+ssObj.divname+"/thumb_"+(i+1)+".jpg");
          imgs[j].src="video/thumb/"+ssObj.divname+"/thumb_"+(i+1)+".jpg";
        }//if
      }//for
    }//for
  }//if
  else
    return true;

}//loadImgSS

function initVideoSS (divname, d) {
 //   $('slideshowvideo').className.concat(' json');

    this.displayphoto = d;
    this.divname = divname;
    this.idlist = $(this.divname).getElementsByTagName('ul')[0].id;

    var as = $(this.divname).getElementsByTagName('a');

    for (var i=0; i<as.length; i++){
     if (as[i].className && as[i].className == 'sshow_prev')
        this.elPrev = as[i];
      else if (as[i].className && as[i].className == 'sshow_next')
        this.elNext = as[i];
    }//for


    var f = $(this.idlist).getElementsByTagName('li');

    this.numphoto = f.length;

    if (this.numphoto == this.displayphoto) {
        this.elNext.style.visibility = 'hidden';
        this.elPrev.style.visibility = 'hidden';
    }

    for(i=0; i<this.numphoto; i++) {
        this.photo.push(f[i]);
    }

    this.elPrev.style.visibility = 'hidden';
    this.resetGallery();
}

function resetGallery() {

  while ($(this.idlist).getElementsByTagName('li').length > 0) {
    $(this.idlist).removeChild($(this.idlist).getElementsByTagName('li')[0]);
  }//while

  for (i=0; i<this.displayphoto;  i++) {
    show = this.currphoto + i;
    if (this.photo[show]) {
      $(this.idlist).appendChild(this.photo[show]);
    }
  }//for
}//resetGallery


function prev() {
  if (this.currphoto > 0) {
    this.currphoto = (this.currphoto > 0)? (this.currphoto - 1) : this.numphoto-1;
    this.resetGallery();
    this.elPrev.style.visibility = 'visible';
  }//if

  if (this.currphoto < (this.numphoto - this.displayphoto)) {
    this.elNext.style.visibility = 'visible';
  }//if
  if (this.currphoto == 0) {
    this.elPrev.style.visibility = 'hidden';
  }//if
  return false;
}//prev

function next () {
  if (this.currphoto < (this.numphoto - this.displayphoto)) {
    this.currphoto = (this.currphoto < (this.numphoto-1))? (this.currphoto + 1) : 0;
    this.resetGallery(this.currphoto);
    this.elPrev.style.visibility = 'visible';
  }//if
  if (this.currphoto > 0) {
    this.elPrev.style.visibility = 'visible';
  }//if
  if (this.currphoto == (this.numphoto - this.displayphoto)) {
    this.elNext.style.visibility = 'hidden';
  }//if
  return false;
}//next



DomLoad.load(function() {
    inputSearchKeyEvt.init();
    inputNLKeyEvt.init();

    //try {
        if (typeof initLightbox === 'function') { initLightbox(); };
    //} catch (e) {}

    if ($('slideshow')) {
        slideShow.init(5, 'slideshow');
    }
    if ($('slideshow_3')) {
        slideShow.init(3, 'slideshow_3');
    }
    if ($('slideshow_2')) {
        slideShow.init(2, 'slideshow_2');
    }

/*     if ($('related_content')) {
        initLightbox();
    } */
});




    var DOMbuilder = function() {

        /* private properties */
        var _defaultnode = document.body;
        var _shortcuts = {
                'text'	    : 'innerHTML',
                'class'	    : 'className',
                'longdesc'	: 'longDesc'
        };

        var _$ = function(id) {
            if (typeof id !== 'string') return id;
            return (document.getElementById)
                ? document.getElementById(id)
                : document.all[id];
        }

        /* private methods */
        var _strToObj = function(strObj) {
            return eval(["(", strObj ,")"].join(''));
        };

        /* public/privileged methods */
        return {
            append :    function(obj, node) {

                var node = (_$(node))
                        ? _$(node)
                        : _$(_defaultnode);

                if (obj === 'string') obj = _strToObj(obj);

                for (var p in obj) {
                    if (obj.hasOwnProperty(p)) {

                        var element = p.split('#');

                        if (typeof obj[p] === 'object') {
                            var currnode = document.createElement(element[0]);
                            if (typeof element[1] === 'string') {
                                currnode['id'] = element[1];
                            }
                            arguments.callee(obj[p], currnode);
                            node.appendChild(currnode);

                        } else {

                            var prop = (_shortcuts[p])? _shortcuts[p] : p;
                            if (prop === 'innerHTML') {
                                    var textnode = document.createTextNode(obj[p]);
                                    node.appendChild(textnode);
                            } else {
                                    node[prop] = obj[p];
                            }
                        }
                    }
                }
            },

            remove :	function(id, complete) {

                var node = _$(id);
                if (!!complete) {
                    return node.parentNode.removeChild(node);
                }
                while (node.childNodes[0]) {
                    node.removeChild(node.childNodes[0]);
                }
            }
        }

	}


    DomLoad.load(function() {


        var newsDOM = new DOMbuilder();
        newsDOM.append({
            'div#layer_menu_nl' : { }
        });


        if ($('content_menu_nl_form')) {
            $('content_menu_nl_form').onsubmit = function() {

                $('inserted_nl').style.border = "2px #fff solid";
                $('msg_error_inserted_nl').style.display = 'none';
                $('msg_error_agreement_nl').style.visibility = 'hidden';

                if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)+(\.\w{2,4})+$/i.test($('inserted_nl').value))) {
                    $('inserted_nl').style.border = "2px #d12420 solid";
                    $('inserted_nl').focus();
                    $('msg_error_inserted_nl').style.display = 'inline';
                    return false;
                }
                if ($('nl_not_agreed').checked) {
                    $('msg_error_agreement_nl').style.visibility = 'visible';
                    return false;
                }

            }
        }

        if ($('bttnlsubscribe')) {
            $('bttnlsubscribe').onclick = function() {

                if (document.body.scrollTop) { document.body.scrollTop = 0; } else { window.scrollTo(0,0); }
                var html = document.getElementsByTagName('html')[0];
                var body = document.getElementsByTagName('body')[0];

                $('inserted_nl').style.border = "2px #fff solid";
                $('msg_error_inserted_nl').style.display = 'none';
                $('msg_error_agreement_nl').style.visibility = 'hidden';

                $('inserted_nl').value =  $('illynewsletter').value;
                $('layer_menu_nl').style.height = html.scrollHeight + 'px';
                $('layer_menu_nl').style.display = 'block'
                $('content_menu_nl').style.display = 'block';
            }
        };

        if ($('close_nl_layer')) {
            $('close_nl_layer').onclick = function() {

                $('layer_menu_nl').style.display = 'none'
                $('content_menu_nl').style.display = 'none';
                $('illynewsletter').value = $('inserted_nl').value;
                return false;
            }
        }

    });

    DomLoad.load(function() {


        var locatorurl = location.href;
/*
        var locatorurl  = "http://www.illy.com/wps/wcm/connect/IT/illy";
        var locatorurl  = "http://www.illy.com/wps/wcm/connect/it/illy/?reg=1";
       var locatorurl  = "http://www.illy.com/wps/wcm/connect/it/illy/fuori-casa/artisti-del-gusto/l-idea/?idLocale=455020";
       var locatorurl  = "http://www.illy.com/wps/wcm/connect/IT/illy?numVeloce=1&indirizzo=genova&citta=genova&stato=genova&tipologia_arr=1,2,3,4&codiceLingua=1&"
       var locatorurl  = "http://www.illy.com/wps/wcm/connect/it/illy/fuori-casa/artisti-del-gusto/l-idea/?numVeloce=1&indirizzo=italia&citta=italia&stato=italia&tipologia_arr=0,2,0,0&codiceLingua=1&"
*/
        var lang = 'EN'; //default
        var langre = /^http(?:.+)"?connect\/(\w{2})\/illy/i;
        if (match = locatorurl.match(langre)) {
            lang = match[1];
        }

        var langre = /^http(?:.+)"?connect\/(\w{2})\/illy\/\?reg=\d/i;
        if (match = locatorurl.match(langre)) {
            lang = match[1];
        }
        else {
            var re = /^http(?:.+)"?connect\/(\w{2})\/illy(?:.*?)\?(.+?)(?:&)?$/i;

            if (data = locatorurl.match(re)) {
                data.reverse().pop();
                lang = data.pop();
                data = data.toString();
                if (data != '') {
                    if ((data.indexOf("tipologia_arr=") < 0)  &&
                         (data.indexOf("idLocale=") < 0)
                    ) {  return false; }
                }

                createlayer(lang.toLowerCase(), data);
                openLocatorLayer();
            };
        }

        function openLocatorLayer() {

            if (document.body.scrollTop) { document.body.scrollTop = 0; } else { window.scrollTo(0,0); }
            var html = document.getElementsByTagName('html')[0];
            var body = document.getElementsByTagName('body')[0];

            if (!(document.all && !window.opera)) {
                $('layer_menu_locator').style.height = html.scrollHeight + 'px';
            }
            else {
                var height = Math.max(html.scrollHeight, html.clientHeight);
                $('layer_menu_locator').style.height = height + 'px';
            }
            $('layer_menu_locator').style.display = 'block'
            $('content_menu_locator').style.display = 'block';

        }

        function createlayer(lang, data) {



            var codlang = {
                "xx" : 0,
                "it" : 1,
                "us" : 2,
                "en" : 2,
                "fr" : 3,
                "es" : 4,
                "de" : 5,
                "nl" : 6,
                "pt" : 7
            };

            var src = 'http://locator.illy.com/flash.php?codiceLingua=' + codlang[lang]
            if (data) src += '&' + data;
            //else src = 'http://locator.illy.com/flash.php';

            var locatorDOM = new DOMbuilder();


            locatorDOM.append({
                'div' : {
                    'id'  : 'layer_menu_locator'
                }
            }, 'body');

            locatorDOM.append({
                'div' : {

                    'id'        : 'content_menu_locator',
                    'a'         : {
                        'href'      : '#',
                        'text'      : 'Chiudi',
                        'onclick'   : function() { closeLocationLayer() }
                    },

                    'iframe'        : {
                        'src'           : src,
                        'border'        : '0',
                        'frameBorder'   : 'no'
                    }
                }
            }, 'body');
        }

        if ($('open_locator_layer')) {
            $('open_locator_layer').onclick = function() {
                createlayer(lang.toLowerCase(), '');
                openLocatorLayer();
                return false;
            }
        };

    });

    function closeLocationLayer() {
        $('layer_menu_locator').style.display = 'none'
        $('content_menu_locator').style.display = 'none';
        var cleanurl = location.href.replace(/(\?.*)?$/, "");
        location.replace(cleanurl);
        return false;
    }

/***********************************************************************/

function loadTabImages (id) {
  if (!tabs[id]) {
    tabs[id] = true;
    var div = $(id);
    var imgs = div.getElementsByTagName('img');
    var cont = 1;
    for (var i=0; i< imgs.length; i++) {
      if (imgs[i].id && imgs[i].id.indexOf(id) != -1) {
        imgs[i].src="video/thumb/"+id+"/thumb_"+cont+".jpg";
        cont++;
      }//if
    }//for
  }//if
  else
    return true;
}//loadTabImages


/* GET ELEMENTS BY CLASS NAME ***********************************************/

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp('(^|\\s)' + strClassName + '(\\s|$)');
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}