// JavaScript Document

//Funcoes de focus e blur da input text do header
function inputFocus(value){
  if($('.headerSearchWrapper input[type=text]').val() == value){
    $('.headerSearchWrapper input[type=text]').val('');
    $('.headerSearchWrapper input[type=text]').css('color', '#606060');
  }
}

function inputBlur(value){
  if($('.headerSearchWrapper input[type=text]').val() == ''){
    $('.headerSearchWrapper input[type=text]').val(value);
    $('.headerSearchWrapper input[type=text]').css('color', '#6597BC');
  }
}


//Funcoes do focus e blur da input text do footer
function inputFocus2(value){
  if($('.newsletterWrapper input[type=text]').val() == value){
    $('.newsletterWrapper input[type=text]').val('');
    $('.newsletterWrapper input[type=text]').css('color', '#606060');
  }
}

function inputBlur2(value){
  if($('.newsletterWrapper input[type=text]').val() == ''){
    $('.newsletterWrapper input[type=text]').val(value);
    $('.newsletterWrapper input[type=text]').css('color', '#6597BC');
  }
}

$(document).ready(function(){
  //Dar altura a div sliderHome
  $('#sliderHome').height($(document).height()-100);
  $('.relative .img').height($(document).height()-100);
/*
  if($('.produtoWrapper .texto').height() < 20){
    $('.produtoWrapper .texto').css('bottom', '6px');
  }*/
  $('.produtoWrapper .texto').each(function(){
    var that = $(this);
    if($(this).height() < 20){
      that.css('bottom', '8px');
    }
  });
  
  $('.subMenuWrapper').each(function(){
    //console.log($(this).width());
    $('.ulsubmenu > li > a', $(this)).css('max-width', $(this).width());
  });
  
  
});


function fakeElements() {
	var base = this;

	fakeSelect = function () {
		$('.fakeSelect select').live('change', function () {
			$('span.selectValue', $(this).parents('.fakeSelect')).text($('option:selected', this).text());
		});
	}
	
	
	
	fakeCheckboxs = function () {
		$('.fakeCheckbox input[type="checkbox"]').live('change', function () {
			$(this).parent().toggleClass('checked');
		});
	}
	
	fakeRadiobuttons = function () {
		$('.fakeRadiobutton input[type="radio"]').live('change', function () {
			var thatName = $(this).attr('name');
			$('.fakeRadiobutton input[type="radio"][name="'+ thatName +'"]:not(:checked)')
				.parent('.checked')
				.toggleClass('checked');
			$(this).parent().toggleClass('checked');
		});
	}
	
	labelActions = function () {
		$('label').live({
			mouseenter : function () {
				var thatFor = $(this).attr('for'), 
				inputParent = $('input[type="checkbox"][id="'+ thatFor +'"], input[type="radio"][id="'+ thatFor +'"]').parent(':not(.checked, .disabled)');

				if (inputParent.is('.fakeCheckbox:not(:checked)')) {
					inputParent.css('background-position', 'left -15px');
				} else if (inputParent.is('.fakeRadiobutton:not(:checked)')) {
					//inputParent.css('background-position', 'left -13px');
				}
			},
			
			mouseleave : function () {
				var thatFor = $(this).attr('for'), 
				inputParent = $('input[type="checkbox"][id="'+ thatFor +'"], input[type="radio"][id="'+ thatFor +'"]').parent(':not(.checked, .disabled)');
				
				if (inputParent.is('.fakeCheckbox') || inputParent.is('.fakeRadiobutton')) {
					inputParent.removeAttr('style');
				}
			},
			
			click : function () {
				var thatFor = $(this).attr('for'), 
				inputParent = $('input[type="checkbox"][id="'+ thatFor +'"], input[type="radio"][id="'+ thatFor +'"]').parent(':not(.checked, .disabled)');
				
				inputParent.removeAttr('style');
			}
		});
	}
	
	base.seeSelected = function () {
		$('.fakeSelect span.selectValue').each(function() {
			$(this).text($('option:selected', $(this).parent()).text());
		});
		
		$('.fakeCheckbox input[type="checkbox"]:checked').each(function() {
			$(this).parent().removeClass().addClass('fakeCheckbox checked');
		});
		
		$('.fakeRadiobutton input[type="radio"]:checked').each(function() {
			$(this).parent().removeClass().addClass('fakeRadiobutton checked');
		});
	}
	
	base.seeDisabled = function () {
		$('.fakeSelect select:disabled').each(function() {
			$(this).parents('.fakeSelect').removeClass().addClass('fakeSelect disabled');
		});
		
		$('.fakeCheckbox input[type="checkbox"]:disabled').each(function() {
			$(this).parent().removeClass().addClass('fakeCheckbox disabled');
		});
		
		$('.fakeRadiobutton input[type="radio"]:disabled').each(function() {
			$(this).parent().removeClass().addClass('fakeRadiobutton disabled');
		});
	}
	
	init = function () {
	
	
		fakeSelect();
		
		fakeCheckboxs();
		fakeRadiobuttons();
		labelActions();
		base.seeSelected();
		base.seeDisabled();
	}
	
	init();
};  

$(document).ready(function(){

  /*scroll*/
  
  //Get our elements for faster access and set overlay width
    var div = $('div.scroll'),
                 ul = $('ul.ulslider'),
                 // unordered list's left margin
                 ulPadding = 30;

    //Get menu width
    var divWidth = div.width();

    //Remove scrollbars
    div.css({overflow: 'hidden'});

    //Find last image container
    var lastLi = ul.find('li:last-child');

    //When user move mouse over menu
    div.mousemove(function(e){

      //As images are loaded ul width increases,
      //so we recalculate it each time
      var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

      var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
      div.scrollLeft(left);
      //$('.bgscroll').css('left', 4+left+'px');
    });

  /*end scoll*/
  
  
  var alt = 0;
  var count = 0;
  /*$('.ulbreadcrumb > li:eq(0)').each(function(){
    console.log($(this));
    alt = $(this).height();
    count++;
  });*/
  
  $('.ulbreadcrumb').each(function(){
    alt = $('li:eq(0)', $(this)).height();
    count++;
  });
  
  $('#breadcrumbwrapper').height(alt*count + 8*count +64);
  
  if($('#breadcrumb > .ulbreadcrumb').size() == 0){
    alt= 49; 
    $('#breadcrumbwrapper').height(alt + 8 +64);  
  }
  
  if($('#breadcrumb > .ulbreadcrumb:eq(0) > li').size() > 8){
    $('#breadcrumbwrapper').height($('#breadcrumbwrapper').height()+64);
  }
  
  //Mostrar por defeito o submenu da opcao activa
  $('.ulProdutos > li.active > .ulProdutosSubmenu').show();
  //$('.focoPicWrapper').height($('.ulProdutos').height()+30);
  
  //Script para abrir/fecahr os menus na pagina de produtos
  $('.ulProdutos > li > a').live('click', function(e){
     e.preventDefault();
     if($(this).parent().hasClass('active')){
        $(this).parent().removeClass('active');
        $(this).next().slideUp();
     }
     else{
      var that = $(this);
      $('.ulProdutos > li').removeClass('active');
      $('.ulProdutos > li > .ulProdutosSubmenu').slideUp();
      that.parent().addClass('active');
      that.next().slideDown();
     } 
  });
  
  //Tabs da Historia
  
  $('.tabsWrapper').height($('.tabsWrapper > .tabDesc:eq(0)').height());
  $('.tabsWrapper > .tabDesc').hide();
  $('.tabsWrapper > .tabDesc:eq(0)').show();  
  
  //Abrir o formulario de envio de informação a um amigo
  $('.sendFriend').live('click', function(e){
    e.preventDefault();
    if($(this).next().is(':visible')){
      $(this).next().fadeOut();
    }
    else{
      $(this).next().fadeIn();
    }
  });
  
});
$(window).load(
  function(){
   if($('.rightSide').width() == 0){
      //console.log('hello');
      //$('.mainArea').css('width', '99%');       
    }
    
    if($('.rightSide > div').size() == 0){
      $('.mainArea').css('width', '99%');
    } 
  }
);

function RedicomBox(index){
  var base = this,
  totChilds = $('.galeriaImgsWrapper > .imagemWrapper').size();
  base.act = 0;
  
  base.VerificaActual = function(){
    $('.nextImg').show();
    $('.prevImg').show();
    if((base.act + 1) == totChilds){
      $('.nextImg').fadeOut();
    }
    if(base.act == 0){
      $('.prevImg').fadeOut(); 
    }
  }

  $('.controls > h3 > #actual').text(base.act + 1);
  $('.controls > h3 > #total').text(totChilds);
  $('.prevImg').show(); 
  $('.nextImg').show();
  
  base.changeIMG = function(direction){
    if(direction == 'left'){
      base.act++;  
      $('.prevImg').show();
    }
    if(direction == 'right'){
      base.act--;  
      $('.nextImg').show();
    }
    
    $('.controls > h3 > #actual').text(base.act + 1);
    base.VerificaActual();
    $('.preloader').show();
    $('.imgwrapper > img').css('display', 'none');
    $('.imgwrapper img').attr('src', $('.galeriaImgsWrapper > .imagemWrapper:eq('+base.act+') > div > img').attr('src'));
    var newWidth =  $('.galeriaImgsWrapper > .imagemWrapper:eq('+base.act+') > div > img').width();
    var newHeight = $('.galeriaImgsWrapper > .imagemWrapper:eq('+base.act+') > div > img').height();
    while(newHeight > ($(window).height()-80)){
        newHeight = newHeight * 0.8;
        newWidth = newWidth * 0.8;
    }
    $('.preloader').animate({
      left: (newWidth/2)-20+'px'
    }, 500);
    $('.imgwrapper').animate({
      width: newWidth,
      height: newHeight
      }, 500, function(){
        $('.preloader').hide();
        $('.imgwrapper > img').width(newWidth).height(newHeight);
        $('.imgwrapper > img').css('display', 'block');
      });
    $('.innerpopupGaleria').animate({
      width: newWidth,
      height: newHeight
      }, 500);      
    //alert('hello2');
  }
  
  function init() {
    base.act = index;
    base.changeIMG('');
  }
  
  init();

}


