var loaded = false;
var left = false;

function is_valid_email (email){
    return /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i .test(email);
}

function bindCarousel(){
    if(!loaded){
        loaded = true;
        var t = false;
        var speed = 20;
        var step = 5;
        left = parseInt(jQuery('.related-overflow').scrollLeft());
        var lstop = false;
        var rstop = false;

        jQuery('.related .prev').hover(
            function(){
                if(!lstop){
                    t = window.setInterval(
                        function(){
                            if(left-step<=0){
                                lstop = true;
                                jQuery('.related .prev').hide();
                                window.clearInterval(t);
                            }
                            else{
                                rstop = false;
                                jQuery('.related .next').show();
                                left -= step;
                                jQuery('.related-overflow').scrollLeft(left);
                            }
                        },speed
                        );
                }
            },
            function(){
                window.clearInterval(t);
            }
            );
        jQuery('.related .next').hover(
            function(){
                if(!rstop){
                    t = window.setInterval(
                        function(){
                            if((left+step)>=jQuery('.related-overflow ul').width()-jQuery('.related-overflow').width()){
                                rstop = true;
                                jQuery('.related .next').hide();
                                window.clearInterval(t);
                            }
                            else{
                                lstop = false;
                                jQuery('.related .prev').show();
                                left += step;
                                jQuery('.related-overflow').scrollLeft(left);
                            }
							
                        },speed
                        );
                }
            },
            function(){
                window.clearInterval(t);
            }
            );

    }
}

var first = 0;
jQuery(document).ready( function(){

    /*
		 *
		 *   CHECK SUBMIT CONTATTI
		 *
		 */
		 
    jQuery('#formcontatti').bind('submit', function(){
			
			
        if(!jQuery('#formcontatti_privacy_container').attr('checked')){
            return false;
        }
				
					
			
    });
		 
		 
    jQuery('#formcontatti').each(
        function(){
            if(!jQuery('#formcontatti_privacy_container').attr('checked')){
                jQuery('#submit_formcontatti').attr('disabled', true);
            }
            if(jQuery('#formcontatti_privacy_container').attr('checked')){
                jQuery('#submit_formcontatti').removeAttr('disabled');
            }
            jQuery('#formcontatti_privacy_container').change(
                function(){
                    if(!jQuery(this).attr('checked')){
                        jQuery('#submit_formcontatti').attr('disabled', true);
                    }
                    else{
                        jQuery('#submit_formcontatti').removeAttr('disabled');
                    }
                }
                );
        }
        );
		
    /*
		 *
		 *   CHECK SUBMIT NEWSLETTER
		 *
		*/


    jQuery('#formnewsletter_email').click(function(){
        if(jQuery(this).val() == 'email...'){
            jQuery(this).attr('value', '');
        }
    });

		
    jQuery('#formnewsletter').bind('submit', function(){
			
			
        if(!jQuery('#formnewsletter_privacy_container').attr('checked')){
            return false;
        }
				
					
			
    });
		
    jQuery('#formnewsletter').each(
        function(){
            if(!jQuery('#formnewsletter_privacy_container').attr('checked')){
                jQuery('#submit_formnewsletter').attr('disabled', true);
            }
            if(jQuery('#formnewsletter_container').attr('checked')){
                jQuery('#submit_formnewsletter').removeAttr('disabled');
            }
            jQuery('#formnewsletter_privacy_container').change(
                function(){
                    if(!jQuery(this).attr('checked')){
                        jQuery('#submit_formnewsletter').attr('disabled', true);
                    }
                    else{
                        jQuery('#submit_formnewsletter').removeAttr('disabled');
                    }
                }
                );
        }
        );






    if( jQuery('#menuist').length > 0 ){
        jQuery('#menuist').jmenu();
        jQuery('#menuist .ul .li .ul .li:first-child a,  #menuist .ul .li .ul .li:first-child a,#menuist .ul .li .ul .li .ul .li:first-child a').css('border','none');
    }
	
	
	
    if(jQuery('.testo .divdx .related-overflow ul').length > 0) {
        var imgcount = 0;
        var ullength = 0;
        jQuery('.testo .divdx .related-overflow img:not(.hiddenimage)').load(
            function(){
				
                ullength += jQuery('.testo .divdx .related-overflow>ul>li:eq('+imgcount+')').width();
                imgcount++;
                jQuery('.testo .divdx .related-overflow>ul').css({
                    width:ullength
                });
				
                if(imgcount==jQuery('.testo .divdx .related-overflow img:not(.hiddenimage)').length){
                    jQuery('.testo .divdx .related-overflow').carousel('.prev', '.next');
                }
				
            }
            );
    }
	
    if(jQuery('.catcollection .coldx .related-overflow ul').length > 0) {
		
        var imgcount = 0;
        var ullength = 0;
		
        jQuery('.catcollection .coldx .related-overflow img:not(.hiddenimage)').bind(
            'load',
            function(){
				
                ullength += jQuery('.catcollection .coldx .related-overflow>ul>li:eq('+imgcount+')').width();
                imgcount++;
                jQuery('.catcollection .coldx .related-overflow>ul').css({
                    width:ullength
                });
				
            /*
				
				
				if(
					imgcount==jQuery('.catcollection .coldx .related-overflow img:not(.hiddenimage)').length 
					&& 
					jQuery('.catcollection .coldx .related-overflow img:not(.hiddenimage)').length>3
				){
					
					bindCarousel();
					
				}
				
				
				*/
				
            }
            );
		
        bindCarousel();
		
    }
	
    if(jQuery('.testo .content .textcontainer table').length > 0) {
        jQuery('.testo .content .textcontainer table tr:last td').css('border', 'none');
    }
	

    jQuery('#loginfrm #login-name').attr('value', 'username');

	
    if(jQuery('.testo .divdx .related-overflow a img').length > 0) {
		
        var fistsrc = jQuery('.testo .divdx .related-overflow a img.hiddenimage:first').attr('src');
		
        jQuery('#imggrande').html('<a href="javascript:;"><img src="/Thumb/phpThumb.php?src='+fistsrc+'&amp;q=100&amp;w=495&amp;h=342&amp;far=1&amp;bg=FFFFFF&fltr[]=wmi|/images/watermark.png|C" /></a>');
		
        jQuery('#imggrande a')
        .attr('href','/Thumb/phpThumb.php?src='+fistsrc+'&q=100&fltr[]=wmi|/images/watermark.png|C')
        .addClass('highslide')
        .bind('click',
            function(){
                return hs.expand(this);
            }
            );
		
		
        jQuery('.testo .divdx .related-overflow a').live(
            'click',
            function(){
				
                var el = jQuery(this);
                jQuery('#imggrande').append('<div class="loading">loading</div>');
                var bigImg = jQuery('#imggrande img');
                bigImg.bind(
                {
                    load:
                    function(){
                        jQuery('#imggrande .loading').remove();
                        bigImg.fadeIn();
                    }
                }
                );
				
                var newImage = el.children('.hiddenimage').attr('src');
                bigImg.fadeOut(
                    'normal',
                    function(){
                        jQuery('#imggrande .loading').show();
                        bigImg.attr('src', '/Thumb/phpThumb.php?src='+newImage+'&q=100&w=495&h=342&far=1&bg=FFFFFF&fltr[]=wmi|/images/watermark.png|C');
                    }
                    );
                bigImg.parent().attr('href','/Thumb/phpThumb.php?src='+newImage+'&q=100&fltr[]=wmi|/images/watermark.png|C');
				
            }
            );

    }

    if(jQuery('.main-collection .dxcontainers .dxcontainers-overflow a img').length > 0) {
		
        jQuery('.main-collection .dxcontainers .dxcontainers-overflow a').bind(
            'click',
            function(){
			
                var el = jQuery(this);
                var bigImg = jQuery('.main-collection .sx img');
				
                jQuery('.main-collection .sx').append('<div class="loading">loading</div>');
                bigImg.bind(
                {
                    load:
                    function(){
                        jQuery('.main-collection .sx .loading').remove();
                        bigImg.fadeIn();
                    }
                }
                );
				
                var newImage = el.children('.hiddenimage').attr('src');
                bigImg.fadeOut(
                    'normal',
                    function(){
                        jQuery('.main-collection .sx .loading').show();
                        bigImg.attr('src', '/Thumb/phpThumb.php?src=' + newImage + '&q=100&w=495&h=342&far=1&bg=FFFFFF&fltr[]=wmi|/images/watermark.png|C');
                    }
                    );
                bigImg.parent().attr('href',newImage);
				
            }
            );

    }
	


    jQuery('#original').hide();


        

	
    /* switch foto dettaglio */
    if(jQuery('#dettaglio #foto .fotos .fotos-overflow').length > 0) {

        jQuery('#dettaglio #foto .fotos a').bind('click', function(){
			
            var el = jQuery(this);

            var newImage = el.children('.hiddenimage').attr('src');

            if( newImage != jQuery('#original').html()){

                jQuery('#original').html(newImage);

                var bigLink = jQuery('#foto #bigimage .highslide');
                var bigImg = jQuery('#foto #bigimage .highslide img');

                bigImg.fadeOut();
                bigLink.attr('href', '/Thumb/phpThumb.php?src=' + newImage + '&q=100&far=1&bg=FFFFFF&fltr[]=wmi|/images/watermark.png|C' );
                bigImg.attr('src', '/Thumb/phpThumb.php?src=' + newImage + '&q=100&w=545&h=379&far=1&bg=FFFFFF&fltr[]=wmi|/images/watermark.png|C');
                bigImg.fadeIn();


            }

        });
		
		
		
    }
	
	
    if(jQuery('#dettaglio #color select').length > 0) {
	
        jQuery('#dettaglio #color select').change(function() {


            var selectInput = jQuery(this);
            var color = selectInput.find(':selected').attr('name');




            // prendo la foto per colore
            var elFotoColore = jQuery('#dettaglio #foto .fotos .fotos-overflow .'+color.replace(new RegExp('/','g'),'').replace(new RegExp(' ','g'),'')+':first').parent().children('.hiddenimage');

            // se c'è una foto di quel colore
            if( elFotoColore.length > 0){

                if( elFotoColore.attr('src') != jQuery('#original').html()){


                    jQuery('#original').html(elFotoColore.attr('src'));

                    var bigLink = jQuery('#foto #bigimage .highslide');
                    var bigImg = jQuery('#foto #bigimage .highslide img');
                    bigImg.fadeOut();
                    bigLink.attr('href',elFotoColore.attr('src'));
                    bigImg.attr('src', '/Thumb/phpThumb.php?src=' + elFotoColore.attr('src') + '&q=100&w=545&h=379&far=1&bg=FFFFFF&fltr[]=wmi|/images/watermark.png|C');
                    bigImg.fadeIn();

                    jQuery('#cartimage').attr('value',elFotoColore.attr('src'));
                                    
                }

            } else {

                elFotoColore = jQuery('#dettaglio #foto .fotos .fotos-overflow .foto:first').parent().children('.hiddenimage');

                jQuery('#cartimage').attr( 'value', elFotoColore.attr('src') );

            }

			
        });
	
    }
	
	
    var allowedkeys = [];
    for(var i=48;i<=57;i++){
        allowedkeys.push(i);
    }
    /*
	for(var i=96;i<=105;i++){
		allowedkeys.push(i);
	}
*/

    jQuery('.cart-table input[name=amount]').each(
        function(){
			
			
            var lang = location.href.match(/\/(\w{2})\//).pop();
            var edit = '';
			
            switch(lang){
                case 'it':
                    edit = 'modifica';
                    break;
                default:
                    edit = 'edit';
                    break;
            }
			
            var oldval = false;
			
            if( jQuery(this).attr('class') != 'nomod' ){
			
                jQuery(this).bind(
                    'keypress keyup',
                    function(e){
					
                        var val = jQuery(this).val();
                        var check = '';
					
                        for(var i=0;i<val.length;i++){
                            check += ((jQuery.inArray(val.charCodeAt(i),allowedkeys)==-1)?'':val.charAt(i));
                        }
					
                        jQuery(this).val(check);
					
                    }
                    ).bind(
                    {
                    focus:
                    function(){
                        jQuery('.cart-table .cartx_edit_button_wr').remove();
                        jQuery(this).after('<span class="cartx_edit_button_wr"><input type="submit" value="'+edit+'" class="cartx_edit_button" /></span>');
                    }
                }
                );
			
            }
			
        }
        );









});

