﻿(function($) {
    $.ProductDetail = function(options) {

        // inizializzo le foto colore
        var defaultImageUrl = $('.productDetail .photo img').attr("src");
        var newDefaultImageUrl = null;
        var newDefaultImageTitle = null;


        // definisco eventuali valori di default
        // se non vengono passati come options 
        var settings = $.extend({
            bigImageWidth: 570,
            bigImageHeight: 320,
            thumbImageWidth: 92,
            thumbImageHeight: 68,
            colorsBinaryResourceGroup: 'binaryResourceGroup1',
            iconsAttributeType: 'attributeType6'
        }, options || {});

        // sposto lo specialtiesThermometer dopo le foto colori
        var specialtiesThermometerTag = $('.productDetail .panelAttributeTypeContainer .attributeType12');
        $(specialtiesThermometerTag).appendTo($('.productDetail'));

        // sposto la descrizione in fondo
        var descriptionTag = $('.productDetail .panelProductInfo .description');
        $(descriptionTag).appendTo($('.productDetail'));



        // CARATTERISTICHE
        $(' .attributeItem').each(function() {
            //var img = '<div class="arrow"><img src="/layout/www1/images/tooltipArrow2.gif"/></div>'
            var contentTitleContainer = $('#' + this.id + ' span.attributeItemTitle');
            var contentDescrContainer = $('#' + this.id + ' span.attributeItemDescription');
            var tooltipId = this.id + '_tooltip';
            tooltipBody = '<div class="tooltip"  id="' + tooltipId + '"><div class="tooltipBody"><b>' + contentTitleContainer.html() + '</b><br/>' + contentDescrContainer.html() + '</div></div>';
            $(tooltipBody).appendTo($('#' + this.id));
            $('#' + this.id + ' img.attributeItemLogo').mouseover(function() {
                $('#' + tooltipId).fadeTo(130, 1);
            });
            $('#' + this.id + ' img.attributeItemLogo').mouseout(function() {
                $('#' + tooltipId).fadeTo(130, 0);
            });
        });

        $(' .attributeItem .disabled').each(function() {
            $('#' + this.id).fadeTo(1500, 0.45);
        });



        // neutralizzo il link della foto colore
        $('.' + settings.colorsBinaryResourceGroup + ' a.binaryResourceItem').each(function() {
            $(this).attr('href', '#');
        });

        //Per ogni colore
        if ($('.' + settings.colorsBinaryResourceGroup)) {
            $('.' + settings.colorsBinaryResourceGroup + ' a.binaryResourceItem').each(function() {
                // forzo le dimensioni del thumbnail
                var imageId = $('#' + this.id + ' .binaryResourceContentImage')[0].id;
                $('#' + imageId).attr('src', $('#' + imageId).attr('src').replace("?w=50&h=50", "?w=" + settings.thumbImageWidth + "&h=" + settings.thumbImageHeight + ""));
                $('#' + this.id).click(function() {
                    var imageId = $('#' + this.id + ' .binaryResourceContentImage')[0].id;
                    newDefaultImageUrl = GetBigImage($('#' + imageId).attr('src'));
                    newDefaultImageTitle = $('#' + this.id + ' span').html();
                    ChangeBigImage(imageId);
                    // sbindo il mouseout
                    $('#' + this.id).unbind('mouseout');
                    return false;
                });
                $('#' + this.id).mouseover(function() {
                    var imageId = $('#' + this.id + ' .binaryResourceContentImage')[0].id;
                    ChangeBigImage(imageId);
                    CreateColorLabel($('#' + this.id + ' span').html());
                    // dopo il mouseover bindo il mouseout
                    $('#' + this.id).mouseout(function() {
                        RestoreBigImage(imageId);
                        if (newDefaultImageUrl == null) {
                            RemoveColorLabel();
                        }
                        else {
                            CreateColorLabel(newDefaultImageTitle);
                        }
                        return false;
                    });
                    return false;
                });

            });
        }


        var GetBigImage = function(imageUrl) {
            imageUrl = imageUrl.replace("?w=" + settings.thumbImageWidth + "&h=" + settings.thumbImageHeight + "", "?w=" + settings.bigImageWidth + "&h=" + settings.bigImageHeight + "");
            return imageUrl;
        }

        var ChangeBigImage = function(id) {
            var bigImageUrl = GetBigImage($('#' + id).attr('src'));
            $('.productDetail .photo img').attr("src", bigImageUrl);
        }
        var RestoreBigImage = function(id) {
            newImage = (newDefaultImageUrl != null) ? newDefaultImageUrl : defaultImageUrl;
            $('.productDetail .photo img').attr("src", newImage);
        }
        var CreateColorLabel = function(color) {
            // se non esiste lo creo
            var div = $('.selectedColorTitle');
            var items = div.length;
            if (items == 0) {
                //creo la label per il colore selezioanto
                var labelSelectedColor = "<div class='selectedColorTitle'>" + color + "</div>";
                $(labelSelectedColor).appendTo($('.productDetail .photo'));
            }
            else if (items == 1) {
                //se esiste cambio il contenuto           
                div.html(color);
            }
        }
        var RemoveColorLabel = function() {
            $('.selectedColorTitle').remove();
        }

    };

})(jQuery);


//file per funzioni Javascript custom
$(document).ready(function() {

	

    //   //aggiungo l'event al bottone buy online
    //   if($('#aBuyOnline')){
    //      $('#aBuyOnline').click(function(){
    //           //$('#dFindStore').css('display','none');
    //           $('#dBuyOnline').slideToggle();
    //      });
    //        //bottone chiusura
    //        if($('#dBuyOnlineClose')){
    //            $('#dBuyOnlineClose').click(function(){
    //                $('#dBuyOnline').slideToggle();
    //            });
    //        }
    //    }

    P7_ExpMenu(); // inizializzo il menu a tendina...

    //    $('.newRegisterBoxSubmit').click(function() {
    //        //sposto i modalPopup in fondo
    //        $('.modalPopupExtender').appendTo($('#container'));
    //    });

    //aggiungo l'event ai filtri ricerca
    if ($('#filterLink')) {
        $('#filterLink').click(function() {
            if ($('#SearchFilters').css('display') == 'none') {
                $('#filterLink').switchClass('openedFilters', 'closedFilters');
				$('#filterLink').html('<b>Chiudi</b>');
            } else {
                $('#filterLink').switchClass('closedFilters', 'openedFilters');
				$('#filterLink').html('Affina la ricerca');
            }
            $('#SearchFilters').slideToggle();
        });
    }

    //MENU CATEGORIE
    $('.menuAreas A').each(function() {
        $('#' + this.id).click(function() {
            $('.menuAreas A').each(function() {
                $('#' + this.id).removeClass('selected');
            });
            $('#MenuHorizRow').removeClass();
            $('#MenuHorizRow').addClass(this.id);
            $('#' + this.id).addClass('selected');
        })
    });
	
	$("#SearchText").keypress(function(event){
	if (event.keyCode == '13') {
     event.preventDefault();
	 document.location.href='/Search.athx?ccode=it-IT&SearchText=' + document.getElementById('SearchText').value + '&searchword=false&searchmode=2';
   }

	});
    

    try {
        // mi aggancio alle richieste asincrone
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(CheckMoveModalPopup);    
    }
    catch (err) {
        //Handle errors here
        //alert("error...");
    }

    InitializeCheckBoxCatalog();
});






/* 
  ------------------------------------
  PVII Menu CSS Express Drop-Down Menu
  by Project Seven Development
  www.projectseven.com
  ------------------------------------
*/
function P7_ExpMenu(){ //v1.1.0.2 by PVII-www.projectseven.com
 if(navigator.appVersion.indexOf("MSIE")==-1){return;}
 var i,k,g,lg,r=/\s*p7hvr/,nn='',c,cs='p7hvr',bv='p7menubar';
 for(i=0;i<10;i++){g=document.getElementById(bv+nn);if(g){
 lg=g.getElementsByTagName("LI");if(lg){for(k=0;k<lg.length;k++){
 lg[k].onmouseover=function(){c=this.className;cl=(c)?c+' '+cs:cs;
 this.className=cl;};lg[k].onmouseout=function(){c=this.className;
 this.className=(c)?c.replace(r,''):'';};}}}nn=i+1;}
}

function CheckMoveModalPopup(sender, args) {
    //alert('CheckMoveModalPopup');

    var elem = args.get_postBackElement();
    var target = null;
    
    target = $('.newRegisterBoxSubmit, .billingAddressEditButton, .newShippingAddressButton, .shippingAddressEditButton');

    
    if (target != null) {
        for (i = 0; i < target.length; i++) {
            if (target[i].id == elem.id) {
                BindMoveModalPopup();
            }
        }
    }
                
}

var handlerInitialized = false;
var originalModalPopups = new Array();

function BindMoveModalPopup() {
    //alert('BindMoveModalPopup');
    if (!handlerInitialized) {
        Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequestHandler);
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(MoveModalPopup);
        handlerInitialized = true;
        //alert("handlerInitialized");
    }
    
}
function InitializeRequestHandler(sender, args) {
    //alert("InitializeRequestHandler");

    //controllo se il postback é stato fatto all'interno del modalpopup,
    // in questo caso risistemo successivamente
    
    
    if ($('.modalPopupExtender.moved').size() > 0) {
        $('.modalPopupExtender').each(function(i) {
            RestoreModalPopup();
        });
    }
}

function MoveModalPopup(sender, args) {
    //alert('MoveModalPopup');
    // sposto il modalpopup
    //alert("da spostare: " + $('.modalPopupExtender').not(".moved").size());
    $('.modalPopupExtender').not(".moved").each(function(i) {
        //alert("id: " + $(this).attr("id") + " - parent: " + $(this).parent().attr("id"));
        SetModalPopupPosition($(this).attr("id"), $(this).parent().attr("id"));
        $(this).addClass("moved").appendTo($('#container'));
    });
}



function SetModalPopupPosition(idModal, idParent){
    //alert("SetModalPopupPosition");
    tmp = new Array();
    tmp[0] = idModal;
    tmp[1] = idParent;
    originalModalPopups[originalModalPopups.length] = tmp;
    //alert("originalModalPopups.lenght: " + originalModalPopups.length);
}

function RestoreModalPopup() {
    //alert("RestoreModalPopup");
    for (i = 0; i < originalModalPopups.length; i++) {
        $('#' + originalModalPopups[i][0]).removeClass("moved").appendTo($('#' + originalModalPopups[i][1]));
    }
}

//funzione per mailinglist (abilita disabilita ricezzionecatalogo)

function EnableDisable(firstin){
    var check;
    $('span.catalog input').each( function(i){
        check = $(this + ':checked').val();
    });
    
    $('div .formRowHiddable').each(function (i){
        if (check != null){
            $(this).show();
        }
        else{           
            $(this).hide();
        }
    });
    
    if(firstin == false){
        if(check != null){
             SwitchValidation(true);
        }
        else{
             SwitchValidation(false);
        }
    }
}
      
      
function InitializeCheckBoxCatalog(){
    var check;
    $('span.catalog input').each( function(i){
        $(this).bind('click', function(e){
            EnableDisable(false);
        });
     });
     
     
     EnableDisable(true);
}

function SwitchValidation(val){
  $('div.formRowHiddable span.validator').each(function (i){
    var validator = document.getElementById($(this).attr('id'));
    ValidatorEnable(validator, val);
  });
}



