(function($){
	// MENU
	$(function(){
		//$('#topnav li ul').hide();

		$('#topnav li').hover(function() {
			$('ul', this).fadeIn('fast');
			$(this).addClass('navHover');
		}, function() {
			$('ul', this).fadeOut('fast');
			$(this).removeClass('navHover');
		});	
	});


	// History
	function loadContent(hash) {
		if(hash != ""){
			// load link
			$('#centerColumn').load(hash);

		}
	}
	$.history.init(loadContent);


	$(function(){
		$('#rightNav ul li').click(function(){
			$href = $(this).children('a').attr('href');
			hash = $href.replace('#', '');
			
			if(window.location.hash == $href){
				//$('#centerColumn').html('loading...');
				$('#centerColumn').load($href);
			}else{
				$.history.load(hash);
			}
			return false;
		});
	});

	$(function(){
		var browser = jQuery.browser;
		var version = jQuery.browser.version;
			
		if(browser = 'msie' && version == 6.0) {		
			// Removes jQuery generated 'Rounded Corners' on IE6 due to issues
			$('#topnav li ul').each(function() {
				$(this).find('ul.roundedBtm').removeClass('roundedBtm');
				$('div#registerNow').removeClass('roundedBtm');
			});
			
			$('#dropTabs').each(function() {
				$('span.roundedBtm').removeClass('roundedBtm');
			});
		}
	});

	// Text Resize
	$(function(){
		var originalFontSize = $('body').css('font-size');   
			
		// Increase Font Size
		$(".increaseFont").click(function() {
			var currentFontSize = $('body').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var largestFontSize = 16;
			
			if(currentFontSizeNum < largestFontSize){
				var newFontSize = Math.floor(currentFontSizeNum + 2);
				$('body').css('font-size', newFontSize);
			}
			
			else {
				$('body').css('font-size', largestFontSize);
			}
			return false;
		});
		// Decrease Font Size
		$(".decreaseFont").click(function() {
			var currentFontSize = $('body').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);    	
			var smallFontSize = 12;   	
			
			if(currentFontSizeNum > smallFontSize){
				var newFontSize = Math.floor(currentFontSizeNum - 2);
				$('body').css('font-size', newFontSize);
			}
			
			else {
				$('body').css('font-size', smallFontSize);
			}
			return false;
		});
	});


	// Alternate Site Dropdowns
	$(function(){
		$('#drop1, #drop2').hide();
		
		$('#dropTabs p').toggle(function() {
			var content = $(this).find('a').attr('href');
			$('#dropTabs p').find('a').addClass('selected');
			$(this).siblings().css({'visibility':'hidden'});
			$(this).parent('#dropTabs').addClass('dropOpen');
			$(content).slideDown();
		}, function() {
			var content = $(this).find('a').attr('href');
			
			$('#dropTabs p').find('a').removeClass('selected');
			$(content).slideUp('fast');
			$('#dropTabs p').css({'visibility':'visible'});
			$(this).parent('#dropTabs').removeClass('dropOpen');
		});
		return false;
	});


	// Image Gallery by Galleria
	var overlayImg = new Array(4);

	$(function(){
		if ($('#productGallery')[0]) { // Se a div productGallery existir...
			$('.gallery_nostyle').addClass('imageGallery'); // adds new class name to maintain degradability

			$('ul.imageGallery').galleria({
				history   : false, // activates the history object for bookmarking, back-button etc.
				clickNext : false, // helper for making the image clickable
				insert    : '#largeImage', // the containing selector for our main image
				onImage   : function(image,caption) { // let's add some image effects for demonstration purposes

					// fade in the image & caption
					if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
						image.css('display','none').fadeIn(1000);
					}
					caption.css('display','none');

					// adding Lightbox 'view larger' link to image container			
					var imgSrc = $('li.active').children('img').attr('id');
					var imgLength = imgSrc.length;
					var imgID = imgSrc.substring(imgLength-1);
					var imageTitle = $('li.active').children('img').attr('title');
					var imageSrc = $('li.active').children('img').attr('rel');
					$('.galleria_wrapper').append('<a href="'+ imageSrc +'" title="' + imageTitle + '">Ver maior +</a>');

					// Lightbox Activation for Gallery
					$('.galleria_wrapper a').lightBox();
				}
			});
		}
	});

	// Show/Hide
	$(function(){
		$('#signInBox, #shareBox, #emailBox, #forgetPassBox, #faqsAnswers div.slider, .categoryGroup').hide();

		$('.showHide').click(function() {
			var toggleBox = $(this).attr('href');
			$(toggleBox).fadeIn();
			$(toggleBox).hover(function() {
				$(this).show();
			}, function() {
				$(this).fadeOut();
			});
			return false;
		});
		
		$('.showClose').click(function() {
			var toggleBox = $(this).attr('href');
			$(toggleBox).fadeIn();
			return false;		
		}); 
		
		$('.closeThis').click(function() {
			var toggleBox = $(this).attr('href');
			$(toggleBox).fadeOut();
			return false;
		});
		
		$('.faqsShowHide').click(function() {
			var toggleBox = $(this).attr('href');
			$(toggleBox).slideToggle();
			return false;
		});
		
		$('.closeAnswer').click(function() {
			$(this).parent().parent().slideToggle();
			return false;
		});
		
		$('.forgetPass').click(function() {
			var toggleBox = $(this).attr('href');
			$('#signInBox').hide();
			$(toggleBox).fadeIn();
			return false;		
		});
			
		$('.closePassFind').click(function() {
			var toggleBox = $(this).attr('href');
			$('#forgetPassBox').fadeOut();
			return false;		
		});
	});

	// Rounded Corners
	//jQuery(function roundedCorners($) {
	$(function roundedCorners(){
		$('.rounded').corner({
			tl: { radius: 5 },
			tr: { radius: 5 },
			bl: { radius: 5 },
			br: { radius: 5 },
			antiAlias: true
		});
		$('.roundedTop').corner({
			tl: { radius: 5 },
			tr: { radius: 5 },
			bl: { radius: 1 },
			br: { radius: 1 },
			antiAlias: true
		});
		$('.roundedBtm').corner({
			tl: { radius: 1 },
			tr: { radius: 1 },
			bl: { radius: 5 },
			br: { radius: 5 },
			antiAlias: true
		});
		
		// Alternate Rounded Corners
		$('.tabModuleCopy').wrap('<div class="tl"><div class="tr"><div class="bl"><div class="br">' + '</div></div></div></div>');
		$('.widgetModuleCopy').wrap('<div class="tl"><div class="tr"><div class="bl"><div class="br">' + '</div></div></div></div>');
	});

	// Tabs
	$(function(){
		$('#tabs a').click(function() {
			$('div.tabContent').hide().filter(this.hash).fadeIn('normal');
			$('#tabs a').removeClass('selected');
			$(this).addClass('selected');

			return false;
		}).filter(':first').click();
	});

	// Page Print
	$(function(){
		$('.printPage').click(function() {
			window.print();
			return false;
		});
	});

	// Tool Tip/Product Info Bubble
	$(function(){
		$('a.Hover').mouseover(function(e) {
			var popup_div = $(this).attr('rel');

			var tipPos = $(this).position();		
			var popx = tipPos.left;
			var popy = tipPos.top;

			var wordWidth = $(this).width();
			var wordHeight = $(popup_div).height();

			leftVal = (e.pageX+10)+"px";
			topVal = (e.pageY-28)+"px";

			$(popup_div).css({left:leftVal,top:topVal}).show().end();
			return false;	
		});				

		$('a.Hover').mouseout(function() {	
			var popup_div = $(this).attr('rel');
			$(popup_div).hover(function() {	
				$(this).show();
			}, function() {
				$(this).hide();
			});
			$(popup_div).hide();
		});
	});

	// Input Button Hover
	$(function(){
		$('span.blueBdr').hover(function () {
			$(this).addClass('btnHover');
		}, function() {
			$(this).removeClass('btnHover');
		});
	});

	// CSS Manipulations
	$(function(){
		$('#shopRetailers').each(function() {
			$('.retailerLogo:last').css({'border-bottom':'none'});
		});
		$('#productGallery li').eq(3).addClass('last');
	});


	$(function(){
		$.emailFriendValidator = $("#emailAFriendForm").validate({
			rules: {
				senderEmail:    { required: true, email: true },
				receiverEmail:  { required: true, email: true },
				emailSubject: "required",
				emailMessage: "required"
			},
			messages: {
				emailSubject: {
					required: "Escolha o assunto"
				},
				emailMessage: {
					required: "Informe uma mensagem."
				},
				senderEmail: {
					required: "O e-mail do remetente é obrigatório.",
					email: "E-mail inválido. Por favor, tente novamente."
				},
				receiverEmail: {
					required: "O e-mail do destinatário é obrigatório.",
					email: "E-mail inválido. Por favor, tente novamente."
				}
			},
			errorPlacement: function(error, element) {
					error.appendTo(element.next());
			},
			// // set this class to error-labels to indicate valid fields
			// success: function(label) {
			// // set &nbsp; as text for IE
			// label.visibility = 'hidden';
			// },
			// specifying a submitHandler prevents the default submit, good for the demo
			submitHandler: function() {
				//alert('Before call');
				emailAFriend();
			}
		});
	});

	function emailAFriend() {
		var fromEmail	= jQuery('#senderEmail').val();
		var toEmail		= jQuery('#receiverEmail').val();
		var subject		= jQuery('#emailSubject').val();
		var message		= jQuery('#emailMessage').val();
		var copyMe		= jQuery('#emailCopyMe').val();
		var url			= jQuery('#emailLink').val();

		jQuery.get("mail.php?acao=recommend", { 
			fromEmail: fromEmail, toEmail: toEmail, message: message, subject: subject, copyMe: copyMe, url: url },
			function(data){
				jQuery('#emailBox').fadeOut();
				jQuery('#emailConfirm').fadeIn();
				jQuery('#emailConfirm .emailConfirmResultado').html(data);
		});
	}

})(jQuery);

// Standard popup window open
function openWin() {
    var url, width = 500, height = 200, menu = "";
    args = arguments;
    url = args[0];
    if (args.length > 1) {
        width = args[1];
    }
    if (args.length > 2) {
        height = args[2];
    }
    if (args.length > 3) {
        if (args[3]) {
            menu = ",menubar=yes,toolbar=yes";
        }
    }
    var newwin = window.open(url, 'newWin' + Math.floor(Math.random() * 1000), 'width=' + width + ',height=' + height + ',scrollbars,resizable' + menu);
}

function externalLink(url) {
    openWin(url,'800','600');
    siteTracker._trackPageview(url);

    $("#TB_closeWindowButton").click();
}

function toolTip() {
	$('a.Hover').mouseover(function() {
		var popup_div = $(this).attr('rel');
		
		var tipPos = $(this).position();		
		var popx = tipPos.left;
		var popy = tipPos.top;
		
		var wordWidth = $(this).width();
		var wordHeight = $(this).height();
		
		leftVal = (popx + wordWidth)-10+"px";
		topVal = popy-(wordHeight*2)+"px";
			
		$(popup_div).css({left:leftVal,top:topVal}).show();
		return false;	
	});
	$('a.Hover').mouseout(function() {
		var popup_div = $(this).attr('rel');
		$(popup_div).hover(function() {
			$(this).show();
		}, function() {
			$(this).hide();
		});
		$(popup_div).hide();
	});
}
