/*
	Index
	| document.ready
	| rels
	| notSupMaxPropert
 */

var animate = false;

var actualItem = 1;

var itemSize = 664;
var speedAnimation = 200;

/* +document ready */
$(function(){

	$(".radio").dgStyle();
	
	$(".select-customize").each(function(){
		var id = $(this).attr('id');
		$('#' + id).selectbox().bind('change', function(){
			$('<div>Value of #default-usage-select changed to: '+$(this).val()+'</div>').appendTo('#demo-default-usage .demoTarget').fadeOut(5000, function(){
				$(this).remove();
			});
		});
	});	

	$('#home .destaque .imgs-destaque').cycle({
		fx:     'fade',
		timeout: 8000,
		next:   '#home .destaque .proximo', 
		prev:   '#home .destaque .anterior',
		pager:	'#home .pags .links',
		pagerClick: function(x){
			x = parseInt(x) + 1;
			_gaq.push(['_trackEvent', 'Destaques', 'Click', 'Image ' + x]);
		}
	});

    /*$('#donde-comprar a.open').click(function(){
        var parentElm = $(this).parent();

        if(parentElm.hasClass('active')){
            parentElm.find('.aba').fadeOut(200, function(){
                parentElm.removeClass('active');
                _gaq.push(['_trackEvent', 'Produtcs', 'Close', 'Onde Comprar o produto ' + $(this).attr('rel')]);
            });
        } else {
            parentElm.find('.aba').fadeIn();
            parentElm.addClass('active');
            _gaq.push(['_trackEvent', 'Produtcs', 'Open', 'Onde Comprar o produto ' + $(this).attr('rel')]);
        }

        return false;
    });*/
	
	
	$('#products div.text, #where-to-buy div.formCat').hide();
	$('#sendCat, .closeForm, #sendCatBuy').click(function() {
		$('#products div.text, #where-to-buy div.formCat').slideToggle('slow');
	  //return false;
	});
	
	$('#sendCat').click(function() {
		_gaq.push(['_trackEvent', 'Produtcs', 'Solicitacao-catalogo', 'Catalogo']);
	});	
	$('#sendCatBuy').click(function() {
		_gaq.push(['_trackEvent', 'Donde-Comprar', 'Solicitacao-catalogo', 'Catalogo']);
	});

	$('#downCat').click(function() {
		_gaq.push(['_trackEvent', 'Produtcs', 'Download-catalogo', 'Catalogo ' + $(this).attr('rel')]);
		//alert("_gaq.push(['_trackEvent', 'Produtcs', 'Catalogo', 'Catalogo "+$(this).attr('rel')+"'])");
	});
	

    $('#gallery a.open').click(function(){
    
        var parentElm = $(this).parent();

        if(parentElm.hasClass('active')){
            parentElm.find('div').hide();
            parentElm.removeClass('active');
            _gaq.push(['_trackEvent', 'Produtcs', 'Close', 'Galeria de Imagens do Produto ' + $(this).attr('rel')]);
        } else {
            parentElm.find('div').fadeIn();
            parentElm.addClass('active');
            _gaq.push(['_trackEvent', 'Produtcs', 'Open', 'Galeria de Imagens do Produto ' + $(this).attr('rel')]);
        }

        return false;
    });

    $('#gallery a.close').click(function(){
        var parentElm = $(this).parents('.hide');
        parentElm.hide();
        parentElm.parent().removeClass('active');
        _gaq.push(['_trackEvent', 'Produtcs', 'Close', 'Galeria de Imagens do Produto ' + $(this).attr('rel')]);
        return false;
    });

	if(typeof sIFR == "function"){
		var flashSrc = rootUrl + "public/swf/paralucent.swf";
		sIFR.replaceElement(".contRight h2.sifr", named({sFlashSrc: flashSrc, sColor: "#333333", sCase: "", sWmode:"transparent", sFlashVars:"textalign=right"}));
		sIFR.replaceElement(".contLeft h2.sifr", named({sFlashSrc: flashSrc, sColor: "#333333", sCase: "", sWmode:"transparent", sFlashVars:"textalign=left"}));
        sIFR.replaceElement(".contRight h3.sifr", named({sFlashSrc: flashSrc, sColor: "#333333", sCase: "", sWmode:"transparent", sFlashVars:"textalign=right"}));
        sIFR.replaceElement(".contLeft h3.sifr", named({sFlashSrc: flashSrc, sColor: "#333333", sCase: "", sWmode:"transparent", sFlashVars:"textalign=left"}));
		sIFR.replaceElement("p.sifrLaranja", named({sFlashSrc: flashSrc, sColor: "#FF5200", sCase: "", sWmode:"transparent"}));
	};
	
	if(notSupMaxPropert()) { //ie6
		//flashMinMsieProperties();
	}
	
	rels();
	
	
        /* Contact*/
        $('#contact_form').submit(function(){
            var obj = $(this);

            if(contactValidity()){
                var objPost = obj.serialize();

                $.post(rootUrl + 'contact/index', objPost, function(response){
                            $('#message-contact').html('<span>Registro enviado con &eacute;xito</span>');
                                    $('#message-contact').removeClass('hide');
                                    resetFieldsContact();
                                    _gaq.push(['_trackEvent', 'Contact Form', 'Submit', 'Sucess']);
                });
            }
            else {
                    $('#message-contact').html('<span>Complete los campos<br />de texto obligatorios</span>');
                            $('#message-contact').removeClass('hide');
                            _gaq.push(['_trackEvent', 'Contact Form', 'Submit', 'Error']);
            }

            return false;
        });
        /* Catalog*/
        $('#catalog_form').submit(function(){
            var obj = $(this);

            if(contactValidity()){
                var objPost = obj.serialize();

                $.post(rootUrl + 'catalog/index', objPost, function(response){
                            $('#message-contact').html('<span>Registro enviado con &eacute;xito</span>');
                                    $('#message-contact').removeClass('hide');
                                    resetFieldsContact();
                                    _gaq.push(['_trackEvent', 'Contact Form', 'Submit', 'Sucess']);
                });
            }
            else {
                    $('#message-contact').html('<span>Complete los campos<br />de texto obligatorios</span>');
                            $('#message-contact').removeClass('hide');
                            _gaq.push(['_trackEvent', 'Contact Form', 'Submit', 'Error']);
            }

            return false;
        });
	
	/* home newsletter*/
	$('#news-link-form').click(function() {
	  $('.form-novidades').slideToggle('slow', function() {
	  	if($('#novidades').hasClass('active')){
	  		_gaq.push(['_trackEvent', 'Newsletter Form', 'Close']);
	  		$('#novidades').removeClass('active');
	  	}else{
	  		_gaq.push(['_trackEvent', 'Newsletter Form', 'Open']);	
	  		$('#novidades').addClass('active');
	  	}
	  });
	  return false;
	});

	$('#close-form').click(function() {
	  _gaq.push(['_trackEvent', 'Newsletter Form', 'Close']);
	  $('.form-novidades').slideToggle('slow', function() {
	  	$('#novidades').removeClass('active');
	  	resetFields();
	  });
	});

    $('#newsletter_form').submit(function(){
        var obj = $(this);

        if(commentValidity()){
            var objPost = obj.serialize();

            $.post(rootUrl + 'registers/send', objPost, function(response){
            	_gaq.push(['_trackEvent', 'Newsletter Form', 'Submit', 'Sucess']);
	        	$('#message-news').html('Registro enviado con &eacute;xito');
				$('#message-news').removeClass('hide');
				resetFields();
				
				setTimeout(function(){
					$('.form-novidades').slideToggle('slow', function() {
	                    $('#novidades').removeClass('active');
		  				$('#message-news').addClass('hide');
	                });
				}, 6000);
            });
        }
        else {
        	_gaq.push(['_trackEvent', 'Newsletter Form', 'Submit', 'Error']);
        	$('#message-news').html('Complete los campos de texto obligatorios');
			$('#message-news').removeClass('hide');
        }

        return false;
    });
    
    $('.selectApplication').click(function() {
    	if($('#profissional').is(':checked')){
    		$('#establishment').removeClass('hide');
    		$('#segment').removeClass('hide');    		
    	}
    	else {
    		$('#establishment').addClass('hide');
    		$('#segment').addClass('hide');
    	}
    	
    });
	/* end home */


    $(".watermark").focus(function(){
      if($(this).val() == $(this).attr('title')){
      	$(this).val('');
      }
    }).blur(function(){
      if($(this).val() == ''){
      	//$(this).val($(this).attr('title'));
      }
    });	
	
    $('.menu-linea-pro li a').click(function(){
        if($(this).parent().attr('class') != 'active'){
            var linkItem = $(this);
            linkItem.parents('ul').find('li.active').removeClass('active');
            linkItem.parent().addClass('active');
            $('.item-linea .active').fadeOut(200, function(){
                $(this).removeClass('active');
                $('.item-linea').find('.' + linkItem.attr('rel')).addClass('active').fadeIn(500);
            });
        }
    });

    $('.item-linea .home .links a').click(function(){
        var linkItem = $(this);
        $('.menu-linea-pro li.active').removeClass('active');
        $('.menu-linea-pro li a[rel='+ linkItem.attr('rel') +']').parent().addClass('active');
        $('.item-linea .active').fadeOut(200, function(){
            $(this).removeClass('active');
            $('.item-linea').find('.' + linkItem.attr('rel')).addClass('active').fadeIn(500);
        });
    });

    $('#gallery .thumbs a').click(function(){
        var linkObj = $(this);
        var relId = linkObj.attr('rel');

        $('#gallery .thumbs li.active').removeClass('active');
        linkObj.parent().addClass('active');
        
        if($('#' + relId).length == 0){
            $('<img />').load(function(){
                $(this).hide();
                $('.images-gallery').append($(this));
                $('.video .images-gallery img:visible').fadeOut();
                $('.video .images-gallery #' + relId).fadeIn();
            }).attr('src', linkObj.attr('href')).attr('id', relId);
        } else {
            $('.video .images-gallery img:visible').fadeOut();
            $('#' + relId).fadeIn();
        }
        
        return false;
    });

	/* where to buy */
	$('#where-to-buy #offline-countries .jquery-custom-selectboxes-replaced-list').css('display','block');
	$('#where-to-buy #offline-countries .jquery-custom-selectboxes-replaced-list .item-0').css('display','none');
	//$('#where-to-buy #offline-countries .jquery-selectbox-moreButton').remove();
	//$('#where-to-buy #offline-countries .jquery-selectbox-item').css('border','1px solid red');
	
	$('#where-to-buy #offline-countries .jquery-selectbox-list').css('width','182px');
	$('#where-to-buy .formCat .jquery-selectbox-list').css('width','440px');
	
	$("#select-online-countries").change(function(){
		var id = $(this).val();
		if(id != ""){
			$.post(rootUrl + 'whereToBuy/sites/countryId/'+id,function(data){
				$("#online-tiendas").html(data).fadeIn('slow');

				var track = '/donde-comprar/online/' + $('#online-countries .jquery-selectbox-currentItem').html();
				_gaq.push(['_trackPageview', track ]);
			});
		}
		else {
			$("#online-tiendas").html("").fadeOut('slow');;
		}	
	});	

	$("#select-offline-countries").change(function(){
		var id = $(this).val();
		if(id != ""){
			$.post(rootUrl + 'whereToBuy/states/countryId/'+id,function(data){
				$("#offline-states").html(data);
				$("#offline-stores").fadeOut('slow');
                return false;
			});
		}
		else {
			$("#offline-states").html("");
			$("#offline-stores").fadeOut('slow');
            return false;
		}
		
	});
	
	$('.jquery-selectbox-currentItem').click(function(){
		$(this).parent().find('.jquery-selectbox-moreButton').click();
        });

        $('.jquery-selectbox-item').click(function(){
            var selected = $(this).parent().parent().find('.jquery-selectbox-currentItem').html();
            if(selected == 'Otros'){ $('#other').fadeIn('fast'); }
            else{ $('#other').fadeOut('fast'); }
        });

        $('#close-shops').live('click',function() {
		$("#offline-stores").fadeOut('slow');
                return false;
        });
	$('#close-sites').live('click',function() {
		$("#online-tiendas").fadeOut('slow');
	});	
	/* end where to buy */
	
	
	$('#establishment').addClass('hide');
	$('#segment').addClass('hide');
	$('#novidades').removeClass('active');

    if($('#la-linea-pro').length && location.hash != ''){
        var hash = location.hash;
        hash = hash.replace('#', '');
        $('.item-linea').find('.active').addClass('hide');
        $('.item-linea').find('#' + hash).removeClass('hide').addClass('active');
        $('.menu-linea-pro li.active').removeClass('active');
        $('.menu-linea-pro li a[rel='+hash+']').parent().addClass('active');
    }

/****** PROMOCION *****/
	$('.box-pais').css('display','none');
	$('#box-default').css('display','block');
	//$('#promocion ul.links a.argentina').addClass('active');
	$('#promocion ul.links a').click(function(){
		$(this).parent().parent().find('a').removeClass('active');
		$(this).addClass('active');		
			
		var item = $(this).attr('rel');
		$(this).parent().parent().next().find('.box-pais, #box-default').css('display','none');
		$(this).parent().parent().next().find('#box-'+item).css('display','block');
		return false;
	});
/****** END PROMOCION *****/
	
/****** LA LINEA BOX *****/
    /*HOME*/
    $('#btHome').css('display','none');
    $('.home > .text > .cont').find('a.btClose').click(function() {
        _gaq.push(['_trackEvent', 'Linea Pro', 'Close','home']);
        $('.home > .text > .cont').animate({opacity: 0.0, width: '0px', height: '0px', top: '+=370', left: '+=875'}, 500,function(){$(this).hide();$('#btHome').show();});
        if($.browser.msie){event.returnValue = false;}
        return false;
    });
    $('#btHome').click(function() {
        _gaq.push(['_trackEvent', 'Linea Pro', 'Open','home']);
        $('.home > .text > .cont').animate({opacity: 1.0, width:'505px', height:'474px', top: '-=370', left: '-=875'}, 500, function(){$('#btHome').hide();});
        if($.browser.msie){event.returnValue = false;}
        return false;
    });
    /*TRADICION*/
    $('#btTradicion').css('display','none');
    $('.tradicion > .text > .cont > a.btClose').click(function() {
        _gaq.push(['_trackEvent', 'Linea Pro', 'Close','tradicion']);
        $('.tradicion > .text > .cont ').animate({opacity: 0.0, width: '0px', height: '0px', top: '+=370', left: '+=875'}, 500, function(){$(this).hide();$('#btTradicion').show();});
        if($.browser.msie){event.returnValue = false;}
        return false;
    });
    $('#btTradicion').click(function() {
        _gaq.push(['_trackEvent', 'Linea Pro', 'Open','tradicion']);
        $('#btTradicion').hide();
        $('.tradicion > .text > .cont ').animate({opacity: 0.9, width: '858px', height:'172px', top: '-=370', left: '-=875'}, 500, function(){$('#btTradicion').hide();});
        if($.browser.msie){event.returnValue = false;}
        return false;
    });
    /*SOLUCIONES*/
    $('#btSoluciones').css('display','none');
        _gaq.push(['_trackEvent', 'Linea Pro', 'Close','soluciones']);
        $('.soluciones > .text > .cont > a.btClose').click(function() {
        $('.soluciones > .text > .cont ').animate({opacity: 0.0, width: '0px', height: '0px', top: '+=370', right: '-=50'}, 500, function(){$(this).hide();$('#btSoluciones').show();});
        if($.browser.msie){event.returnValue = false;}
        return false;
        });
    $('#btSoluciones').click(function() {
        _gaq.push(['_trackEvent', 'Linea Pro', 'Open','soluciones']);
        $('#btSoluciones').hide();
        $('.soluciones > .text > .cont ').animate({opacity: 0.9, width: '348px', height:'364px', top: '-=370', right: '+=50'}, 500, function(){$('#btSoluciones').hide();});
        if($.browser.msie){event.returnValue = false;}
        return false;
    });
    /*INNOVACION*/
    $('#btInnovacion').css('display','none');
        _gaq.push(['_trackEvent', 'Linea Pro', 'Close','innovacion']);
        $('.innovacion > .text > .cont > a.btClose').click(function() {
        $('.innovacion > .text > .cont ').animate({opacity: 0.0, width: '0px', height: '0px', top: '+=370', left: '+=875'}, 500, function(){$(this).hide();$('#btInnovacion').show();});
        if($.browser.msie){event.returnValue = false;}
        return false;
    });
    $('#btInnovacion').click(function() {
        _gaq.push(['_trackEvent', 'Linea Pro', 'Open','innovacion']);
        $('#btInnovacion').hide();
        $('.innovacion > .text > .cont ').animate({opacity: 0.9, width: '405px', height:'321px', top: '-=370', left: '-=875'}, 500, function(){$('#btInovacion').hide();});
        if($.browser.msie){event.returnValue = false;}
        return false;
    });
    /*DESENPENO*/
    $('#btDesempeno').hide();
        _gaq.push(['_trackEvent', 'Linea Pro', 'Close','desempeno']);
	$('.desempeno > .text > .cont > a.btClose').click(function() {
        $('.desempeno > .text > .cont ').animate({opacity: 0.0, width: '0px', height: '0px', top: '+=370', left: '+=875'}, 500, function(){$(this).hide();$('#btDesempeno').show();});
        return false;
    });
    $('#btDesempeno').click(function() {
        _gaq.push(['_trackEvent', 'Linea Pro', 'Open','desempeno']);
        $('#btDesempeno').hide();
        $('.desempeno > .text > .cont ').animate({opacity: 0.9, width: '900px', height:'138px', top: '-=370', left: '-=875'}, 500, function(){$('#btDesempeno').hide();});
        return false;
    });
    /*SEGURIDAD*/
    $('#btSeguridad').hide();
        _gaq.push(['_trackEvent', 'Linea Pro', 'Close','seguridad']);
	$('.seguridad > .text > .cont > a.btClose').click(function() {
        $('.seguridad > .text > .cont ').animate({opacity: 0.0, width: '0px', height: '0px', top: '+=370', left: '+=875'}, 500, function(){$(this).hide();$('#btSeguridad').show();});
        return false;
    });
    $('#btSeguridad').click(function() {
      _gaq.push(['_trackEvent', 'Linea Pro', 'Open','seguridad']);
      $('#btSeguridad').hide();
      $('.seguridad > .text > .cont ').animate({opacity: 0.9, width: '900px', height:'221px', top: '-=370', left: '-=875'}, 500, function(){$('#btSeguridad').hide();});
      return false;
    });
    /************************/
});
/*  end document ready  */

function assertWaterMark(elm) {
    return (elm.value != elm.title);
}

function resetFieldsContact() {
	$('#contact_name').val('Nombre').removeClass('validity-erroneous');	
	$('#contact_email').val('Email').removeClass('validity-erroneous');
        $('#contact_provincia').val('Provincia/Ciudad').removeClass('validity-erroneous');
	$('#contact_message').val('Mensage').removeClass('validity-erroneous');
        $('#contact_telefone').val('Telefono').removeClass('validity-erroneous');	
}

function resetFields() {
	$('#name').val('Nombre').removeClass('validity-erroneous');
	$('#city').val('Ciudad').removeClass('validity-erroneous');
        $('#phone').val('Telefono').text('Teléfono');
	//$('#phone').val('Telefono').removeClass('validity-erroneous');
	$('#email').val('Email').removeClass('validity-erroneous');
	$('#other').val('Qual?').removeClass('validity-erroneous');
		
    $('#newsletter_form select').each(function(){
    	$(this).parents('.bg-select').css('border','1px solid #F2F2F2');
    });
}

function commentValidity() {
    $.validity.start();
    $.validity.setup({outputMode:"summary"});
    $('#name').require("").assert(assertWaterMark,"");
    $('#city').require("").assert(assertWaterMark,"");
    $('#select-countries').require("").assert(assertWaterMark,"");
    $('#email').require("").assert(assertWaterMark,"").match('email','');
    $('#phone').require("").assert(assertWaterMark,"");
    $('#select-gender').require("").assert(assertWaterMark,"");
    $('#select-knowledge').require("").assert(assertWaterMark,"");

	if($('#select-knowledge').val() == 'otros') {
    	$('#other').require("").assert(assertWaterMark,"");
	}

    if($('#profissional').is(':checked')){
        $('#select-establishments').require("").assert(assertWaterMark,"");
        $('#select-segments').require("").assert(assertWaterMark,"");
    }

    var result = $.validity.end();

    $('#newsletter_form select').each(function(){
        $(this).parents('.bg-select').css('border','1px solid #F2F2F2');
    });
    $('#newsletter_form select.validity-erroneous').each(function(){
        $(this).parents('.bg-select').css('border','1px solid #FE5200');
    });

    return result.valid;
}

function contactValidity() {
    $.validity.start();
    $.validity.setup({outputMode:"summary"});
    $('#contact_name').require("").assert(assertWaterMark,"");
    $('#default-usage-select').require("").assert(assertWaterMark,"");
    $('#contact_email').require("").assert(assertWaterMark,"").match('email','');
    $('#contact_provincia').require("").assert(assertWaterMark,"");
    $('#contact_message').require("").assert(assertWaterMark,"");

    var result = $.validity.end();


    $('#contact_form select').each(function(){
        $(this).parents('.bg-select').removeClass('validity-erroneous');
    });
    $('#contact_form select.validity-erroneous').each(function(){
        $(this).parents('.bg-select').addClass('validity-erroneous');
    });

    return result.valid;
}

/* +document ready functions */
function rels() {
	$("a[rel*=external]").attr("target", "_blank");
}
/* end document ready functions */


/* +ie hacks */
function notSupMaxPropert() { // não suporta CSS max properties
	if (typeof document.body.style.maxHeight === "undefined") return true;
	else return false;
}
/* end ie hacks */


