/**
 * @author FINSA Consulting s.r.l.
 * @classDescription Functions for manage Image-Thumb
 */
	var old_class = '';
    window.addEvent('load', initGallery );

    /*
		Enable Thumb Image effects.
		Enable Tooltip.
		Define onClick function.
    */
    function initGallery() {
        try {
            // Search for all Thumb's images
            var list = $$('.imgGridThumb');
            // for each image set effects
            list.each(function(element) {
                element.addEvents({
                    'mouseover': function(){
                         if (this.hasClass("imgThumbSelected")) return; /* immagine corrente */
                         this.removeClass("imgThumbUnselected");
                         this.addClass("imgThumbSelectedNB");
                    },
                    'mouseout': function(){
                         if (this.hasClass("imgThumbSelected")) return; /* immagine corrente */
                         this.removeClass("imgThumbSelectedNB");
                         this.addClass("imgThumbUnselected");
                    },
                    'click': function () {
                            if (this.hasClass("imgThumbSelected")) return; /* immagine corrente */
                            // WARNING: Image ID must be <name>_<id>
                            var elem = $$('.imgThumbSelected');
                            elem.removeClass("imgThumbSelected");
                            elem.addClass("imgThumbUnselected");
                            this.removeClass("imgThumbSelectedNB");
                            this.addClass("imgThumbSelected");
                    }
                });
            });
            // Search for all Thumb's A
            list_a_thumb = $$('#imgThumb a');
            // for each image set effects
            list_a_thumb.each(function(element) {
                element.addEvents({
                    'click': function () {
                            if (this.hasClass("imgThumbSelected")) return false; /* immagine corrente */
                            image = this.getElement('img');
                            if (image==null){
                            	image = this.getElement('div');
                            }
                            imageThumb_OnClick(image);
                            initToolTip(false,null,null);
                            return false;
                    }
                });
            });
            // paging events
            list_a_pager = $$('.pager a');
            list_a_pager.each(function(element) {
                element.addEvents({                  
                    'click': function () {
                            pageChange_OnClick(this.href);
                            return false;
                    }
                });
            });
            // paging events
            list_a_selector = $$('.subtitleBoxSection a');
            list_a_selector.each(function(element) {
                element.addEvents({                  
                    'click': function () {
                            pageChange_OnClick(this.href);
                            return false;
                    }
                });
            });
            initToolTip(false,null,null);
        } catch (e) {
            //alert (e.message);
        }
    }

	function imageThumb_OnClick (image) {
		// Insert here code to change Middle Image (imgBig) and Text [Title+Description] (#contextDescription)
		var imgBig = $$('#imgBig');
		var divContext = $$('#contextDescription div');

		var id_img = image.getAttribute('id');
		var i = id_img.split("_")[1];
		var div_titolo = $('cont_titolo_'+i);
		var div_descr = $('cont_descr_'+i);
		var div_tip = $('cont_tip_'+i);
		
		if (image.tagName=='IMG'){
			if(imgBig[0].tagName=='DIV'){
				var copy = image.clone();
				copy.setProperty('id','imgBig');
				copy.replaces(imgBig[0]);
				imgBig[0] = copy;
			}
			imgBig[0].setProperty('class',central_element_class);
			imgBig[0].removeProperty('style');
			imgBig[0].setProperty('src', image.src.replace('thumb', 'zoom'));
		}else{
			var copy = image.clone();
			copy.setProperty('id','imgBig');
			copy.setProperty('class','');
			copy.setProperty('style','position:relative; top:200px;');
			copy.replaces(imgBig[0]);
			imgBig[0] = copy;
		}
		
		imgBig[0].setProperty('alt', image.getProperty('alt'));
		if (imgBig[0].hasClass('toolTip')){
			imgBig[0].setProperty('title', div_tip.getProperty('html'));
		}else{
			imgBig[0].setProperty('title', '');
		}
		
		// Title
		divContext[0].setProperty('html', div_titolo.getProperty('html'));

		if (imgBig[0].hasClass('toolTip'))
			divContext[1].setProperty('html', div_descr.getProperty('html'))
		else
			if (div_tip.getProperty('html'))
				divContext[1].setProperty("html", div_descr.getProperty('html') + div_tip.getProperty('html'));
			else
				divContext[1].setProperty("html", div_descr.getProperty('html'));
		
		// setting della funzione di condivisione
		var condividi = $('link_condividi');
		var fld_nomehost = $('nomehost');
		if (fld_nomehost != null) {
			var link = fld_nomehost.getProperty('value');
			var addthis_language = "";
			if (lang == 'it' || lang == 'en')
				addthis_language = lang;
			else	
				addthis_language = "ja";
		}
		
		// Link alla Collezione - solo per la pagina MyMaxMara
		var goToCollection = $('goToColl');
		if(goToCollection != null){
			collAlias = $('pageAliasColl');
			if(collAlias!=null){
				if (image.get("coll") == '72') { /* xmas */
					collAlias = 'Christmas';
				} else {
					collAlias = collAlias.get('value');
				}
				collName = image.get("coll");
				position = image.get("imgpos");
				if(position == null){
					goToCollection.set("href","/" + lang + "/" + collAlias + "/" + collName);
					link += "/" + lang + "/" + collAlias + "/" + collName;
				}else{
					goToCollection.set("href","/" + lang + "/" + collAlias + "/" + collName + "/" + position);
					link += "/" + lang + "/" + collAlias + "/" + collName + "/" + position;
				}
				// funzione di condivisione - share -
				var addthis_url = "http://www.addthis.com/bookmark.php?lng=" + addthis_language + "&pub=mmfgblogs&url=" + link + "&title=" + document.title;
				var open_share_win = "window.open(\"" + addthis_url + "\", 'addthis', \"width=640, height=550\");";
				if (condividi != null) {
					condividi.addEvent('click', function() {
						window.open(addthis_url, 'addthis', "width=640, height=550");
					});
				}
			}
		}

		// Modifica valori nel form - solo per la pagina MyMaxMara
		var idBookmarkInput = $('id_bkmk');
		if (idBookmarkInput != null){
			idBookmarkInput.set("value",image.get("bookmark"));
		}
	}      

	function pageChange_OnClick (hRef) {
		$('loading').setStyle('display', 'block');
		var new_href = hRef + '?ajax=true';
		var req = new Request({url:new_href, 
			method: 'get',
			onSuccess: function(html) {
				if (html == 'SESSION_EXPIRED') {
					window.location = hRef;
					return false;
				}
				$('headCont').set('html', html);
				if (typeof($('emptyGallery')) != 'undefined' && $('emptyGallery') != null)
					mainImgLoaded();
				initGallery();
			},
			onFailure: function() {
				alert('Error during call');
			}
		});
		req.send();     
		return false;
	}

	function mainImgLoaded() {
		if (typeof($('loading')) != 'undefined' && $('loading') != null)
			$('loading').setStyle('display', 'none');
	}

