$.fn.centerPop = function(includeBorder, position) {
    includeBorder = includeBorder || false;
	position = position || false;
	var that = $(this), 
	windowObj = $(window),
	topOffset, leftOffset;
	//pxWidth = ((that.outerWidth() * $('.holderMain').width()) / 100),
	//pxHeight = ((that.outerHeight() * $('.holderMain').height()) / 100);
	
	function center() {
		var ratio = 661 / 537,
		currH = (($(window).height() * 86)  / 100) - 13,
		currW = (currH / ratio) - 14;

		if (currH < 661) {
			$('.newsletConteudo').css({
				'width' : currW,
				'height' : currH
			});
		} else {
			$('.newsletConteudo').css({
				'width' : 512,
				'height' : 631
			});
		}
		
		if (includeBorder) {
			topOffset = (windowObj.height() / 2) - (that.outerHeight() / 2);
			leftOffset = (windowObj.width() / 2) - (that.outerWidth()  / 2);
		} else {
			topOffset = (windowObj.height() / 2) - (that.height() / 2);
			leftOffset = (windowObj.width() / 2) - (that.width()  / 2);
		}                       
		if (position) {
			that.css({
			top : topOffset,
			left : leftOffset
		});
		} else {
			that.css({
			marginTop : topOffset,
			marginLeft : leftOffset
		});
		}
	}
	center();
	$(window).resize(function () {
	center();
	});
	
	return this;
}; 

function fakeElements() {
	var base = this;

	fakeSelect = function () {
		$('.fakeSelect select, .fakeSelectSmall select').live('change', function () {
			$('span.selectValue', $(this).parents('.fakeSelect, .fakeSelectSmall')).text($('option:selected', this).text());
		});
	}
	
	fakeCheckboxs = function () {
		$('.fakeCheckbox input[type="checkbox"], .fakeCheckboxFilter input[type="checkbox"]').live('change', function () {
			$(this).parent().toggleClass('checked');
	
			if ($(this).parent().is('div[class^=fakeCheckboxFilter]')) {
				if ($(this).is(':checked')) {
					$('label', $(this).parents('dd')).css({
						'color' : '#1D1D19',
						'font-weight' : 'bold'
					});
				} else {
					$('label', $(this).parents('dd')).css({
						'color' : '#666666',
						'font-weight' : 'normal'
					});
				}
			}
		});
	}
	
	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 -16px');
				} else if (inputParent.is('.fakeCheckboxFilter:not(:checked)')) {
					inputParent.css('background-position', 'left -17px');
				}
			},
			
			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.is('.fakeCheckboxFilter')) {
					inputParent.attr('style', '');
				}
			},
			
			click : function () {
				var thatFor = $(this).attr('for');
				$('input[type="checkbox"][id="'+ thatFor +'"], input[type="radio"][id="'+ thatFor +'"]')
					.parent(':not(.checked, .disabled)')
					.attr('style', '');
			}
		});
	}
	
	base.seeSelected = function () {
		$('.fakeSelect span.selectValue').each(function() {
			$(this).text($('option:selected', $(this).parent()).text());
		});
		
		$('.fakeCheckbox input[type="checkbox"]:checked, , .fakeCheckboxFilter input[type="checkbox"]:checked').each(function() {
			if ($(this).parent().is('div[class^=fakeCheckboxFilter]')) {
				$(this).parent().removeClass().addClass('fakeCheckboxFilter checked');
				
				if ($(this).is(':checked')) {
					$('label', $(this).parents('dd')).css({
						'color' : '#1D1D19',
						'font-weight' : 'bold'
					});
				} else {
					$('label', $(this).parents('dd')).css({
						'color' : '#666666',
						'font-weight' : 'normal'
					});
				}
			} else {
				$(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();
};

function anaSousaObject() {
	var base = this,
	searchInputDefaultValue = '',
	timeOutHoverMenu;
	
	base.ajaxObj = null;
	base.scrollApi = null;
	
	base.closePopUp = function (popUp) {
		$(popUp,parent.document).hide();
	}
	
	base.openPopUp = function (popUp) {
		$(popUp,parent.document).show();
	}
	
	base.ajaxDefine = function (id) {
	   
		$.get('ajax_define.php', { defineId : id, 
			token : $('input[type="hidden"][name="token"]').val() 
		}, function (data) {
			
      var json = $.parseJSON(data),
			link, targetLink;
			
			link = json.link.href;
			if (link == '') {
				link = 'javascript:void(0)';
			}
			$('.popDefineEstilo').show();
			$('.textoDefineEstilo')
				.find('h1')
				.empty()
				.text(json.desc)
				.end()
				.find('a')
				.attr('href', link)
				.end()
				.find('.blocoDefine > p')
				.empty()
				.html(json.bloco)
				.end();
				
			$('.imgWrapper img').attr('src', json.image);
			$('.definePreloader').hide();
			$('.textoDefineEstilo, .imgWrapper').fadeIn(200);
		});
	};

	base.ajaxLojas = function (id, noresults) {
		noresults = noresults || null;
		
		base.ajaxObj = $.get('ajax_lojas.php', { countryId : id, 
			token : $('input[type="hidden"][name="token"]').val() 
		}, function (data) {
			var json = $.parseJSON(data),
			jsonLen = json.length,
			ulHtml = '';
	
			if (jsonLen > 0) {
				for (var i = 0, coord = ''; i <= jsonLen - 1; ++i) {
					if (json[i].coordenadas != '') {
						coord = json[i].coordenadas;
					}
					ulHtml += '<li mapCoord="' + coord + '"><h5>' + json[i].nome + '</h5><p>' + json[i].bloco + '</p></li>';
				}
			} else {
				ulHtml += '<li><p>' + noresults  + '</p></li>';
			}
			
			$('.conteudo ul.ulLocalizacoes').html(ulHtml);
			$('.lojasPreloader').hide();
			base.scrollApi.reinitialise();
		});
	};
	
	base.initializeGoogleMaps = function (coordMaps) {
		delete myLatlng, myOptions, map;
		var coordsSplit = coordMaps.split(','),
		latlng = new google.maps.LatLng(coordsSplit[0], coordsSplit[1]),
		myOptions = {
			zoom: 8,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	};
	
	function eventHandling() {
		$('.inputSearch input[type="text"]').focusin(function () {
			//var that = $(this);
			//searchInputDefaultValue = that.val();
			//that.val('');
		}).focusout(function () {
			//var that = $(this);
			//if (that.val() == '') {
				//that.val(searchInputDefaultValue);
			//}
		});
		
		function menuHover(that, ev) {
			if ($.browser.msie && $.browser.version == '7.0') {
				if (that.has('ul').length > 0) {
					that.siblings().removeClass('seeHoverSpan').css('z-index', '1');
					that.toggleClass('seeHoverSpan').css('z-index', '10');
				} else {
					that.toggleClass('active');
				}
			} else {
				if (that.has('ul').length > 0) {
					that.siblings().css('z-index', '1');
					that.toggleClass('seeHoverSpan').css('z-index', '10');
				} else {
					that.toggleClass('active');
				}
			}
		}
		
		$('ul.menu > li').bind('mouseenter mouseleave', function (ev) {
			menuHover($(this), ev);
		});
	}

	fakeElements.prototype.inputFocus = function () {
		$('input[type="text"], input[type="password"], textarea').focusin(function () {
			var thatParent = $(this).parent();
			
			if (thatParent.is(':not(.inputSearch)')) {
				thatParent.css('border', '1px solid #AAAAAA');
			}			
		}).focusout(function () {
			var thatParent = $(this).parent();
			
			if (thatParent.is(':not(.inputSearch)')) {
				thatParent.css('border', '1px solid #CCC8C8');
			}
		});
	};

	//inherit fakeElements object
	inheritFakeElements = fakeElements;

	function init() {
		eventHandling();
		inheritFakeElements();
		inheritFakeElements.prototype.inputFocus();
		delete inheritFakeElements;
	}
	
	init();
}

var anaSousaObj;
$(document).ready(function () {
	anaSousaObj = new anaSousaObject();
	
	//Eventos do popup do carrinho
	//Abrir popup
	$('.loginLoggedIn > .bag > a').live('click', function(ev){
		ev.preventDefault();
		$('.carrinhoWrapper').slideDown();
	});
	//Fechar popup
	$('.carrinhoBottom > .closeCarrinho').live('click', function(){
		$('.carrinhoWrapper').slideUp(); 
	});
});

function verify_email(_mail){
	var status = false;     
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	if (_mail.search(emailRegEx) == -1) {
		status = false;
	} else {
		status = true;
	}
	return status;
} 

function deleteFromQuickBag(idp, tamanho, that){
  
     $.ajax({
              type: "GET",
              url: "ajax_cart.php",
              data: "action=4&idp="+idp+"&tamanho="+tamanho,
              success: function(msg){
                  
                  $(".carrinhoWrapper", parent.document).slideUp(500, function () {
                      
                              $(".bag").html(msg); 
                              $(".carrinhoWrapper", parent.document).slideDown(500);
                            

                        
                 });
                   
                   
                
               
              }
        }); 
  
}
function viewSearch(str,texto){
  
  if(str == $('input[name="search"]').val() || $('input[name="search"]').val().length <3){
    alert(texto);
    return false;
  }
  
  return true;
}
function viewEmailNews(estr,estr2){
  if(verify_email($('#email_newsletter').val())==false)
  {
    alert(estr);
    return false;
  }
  var params = {token:$('input[name="token"]').val(),email:$('#email_newsletter').val()};
  var str = jQuery.param(params);
  jQuery.ajax({
    type: "GET",
    url: "ajax_viewemailnew.php",
    data:str,
    success: function(data) {
      if(data > 0){
        alert(estr2);
      }
      return false;  
    }
  });
  return true;
}

function focusSearch(defVal){
  if($('.inputSearch input').val() == defVal){
    $('.inputSearch input').val('');
  }   
}

function blurSearch(defVal){
  if($('.inputSearch input').val() == ''){
    $('.inputSearch input').val(defVal);
  }   
}
function valida_cartao(lg){
  if($('input[name="gid"]').val()==""){
    
    if(lg == "pt"){
      alert("Por favor preencha com o seu cartão presente.");
    } else if(lg == "gb"){
      alert("Please fill in with your gift card");
    } else if(lg == "sp"){
      alert("Por favor, introduzca su tarjeta de regalo.");
    } else if(lg == "fr"){
      alert("S'il vous plaît remplir votre carte-cadeau.");
    }
      
    return false;
  }
  return true;
}
$(window).load(function(){
  $('ul.menu').fadeIn(100);
})
