/**
 *  jQuery Fixed Position Plugin
 *  @requires jQuery v1.3
 *  http://www.socialembedded.com/labs
 *
 *  Copyright (c)  Hernan Amiune (hernan.amiune.com)
 *  Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
 * 
 *  Version: 1.0
 */
 
(function($){ $.fn.fixedPosition = function(options){

    var defaults = {
	  vpos: null,    // posible values: "top", "middle", "bottom". if it is null the original position is taken
	  hpos: null     // posible values: "left", "center", "right". if it is null the original position is taken
	};
	
    var options = $.extend(defaults, options);
	
	return this.each(function(index) {
		
		
		var $this = $(this);
		$this.css("position","absolute");
		
		if(options.vpos === "top"){
		    $this.css("top","0");
		}
		else if(options.vpos === "middle"){
		    $this.css("top",((parseInt($(window).height())/2)-($(this).height()/2))+"px");
		}
		else if(options.vpos === "bottom"){
		    $this.css("bottom","0");
		}
		
		if(options.hpos === "left"){
		    $this.css("left","0");
		}
		else if(options.hpos === "center"){
		    $this.css("left",((parseInt($(window).width())/2)-($(this).width()/2))+"px");
		}
		else if(options.hpos === "right"){
		    $this.css("right","0");
		}
		
		var top = parseInt($this.offset().top);
		var left = parseInt($this.offset().left);
		$(window).scroll(function () {
			$this.css("top",top+$(document).scrollTop()).css("left",left+$(document).scrollLeft());
		});
	});
  
}})(jQuery);


var JQ = jQuery.noConflict();
var BOX_LOGIN_STATUS = false;
var ampliaImagem;

/* Embed SWF */
var flashvars = {};
var params = {
	wmode: 'transparent'
};

function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

JQ(function($){
	/* Login */
	$('#box-login .label').click(function(){
		if(BOX_LOGIN_STATUS){
			BOX_LOGIN_STATUS = false;
			$('#box-login .label').fadeOut();
			var callback = function(){
				$('#box-login .label').fadeIn('fast');
			}
			$('#box-login').animate({height: 30}, 800);
			$('#box-login .box-form').animate({top: -107},500,callback);
			$('#box-login .box-form .final-links').fadeOut('slow');
			$('#box-login .box-form .bt-sair').fadeOut('slow');
		}
		else{
			BOX_LOGIN_STATUS = true;
			$('#box-login .label').fadeOut();
			var callback = function(){
				$('#box-login .label').fadeIn('fast');
			}
			$('#box-login').animate({height: 137}, 400);
			$('#box-login .box-form').animate({top: 0},500,callback);
			$('#box-login .box-form .final-links').fadeIn('slow');
			$('#box-login .box-form .bt-sair').fadeIn('slow');
		}
	});
	
	/* Menu de filtragem do topo */
	$('#top-menu .nivel-1 ul li a').click(function(){
		$('#top-menu .nivel-2').slideDown('fast');
		$('#top-menu .nivel-2 ul').hide();
		$('#top-menu .nivel-2 ul.'+$(this).attr('class')).fadeIn();
		return false;
	});
	
	/* Menu lateral */
	/*
	$('.vertical-nav li.level1 > a').click(function(){
		if($(this).next('ul').length){
			$(this).next('ul').toggle();
			return false;
		}
	});
	*/
	
	/* Fotos do produto */
	$('.mais-fotos .container-fotos').jCarouselLite({
		btnNext: '.seta-r',
		btnPrev: '.seta-l',
		circular: false,
		visible: 6
	});
	$('.poucas-fotos .seta-l, .poucas-fotos .seta-r').css({opacity: .3});
	
	
	$('a[rel^="prettyPhoto"]').prettyPhoto();
	if($('body:first').hasClass('catalog-product-view')){
		$('.img-item-galeria').click(function(){
			var imagem = $(this).attr('rel');
			$('.imagem-principal img').attr('src', imagem);
			return false;
		});
	}
			
	$('.container-modelos').jCarouselLite({
		btnNext: '.proximas',
		btnPrev: '.anteriores',
		circular: false,
		visible: 7
	});
	
	/* Aba de informações do produto */
	$('.abas-produto ul li').click(function(){
		$('.abas-produto ul li').removeClass('ativo');
		$(this).addClass('ativo');
		
		var box = $(this).attr('id').split('bt-');
		box = box[1];
		$('.conteudo-abas-produto .aba').hide();
		$('.conteudo-abas-produto .aba-' + box).show();
	});
});


var abrePopReview = function(url){
	return window.open(url,'form_avaliacao','width=570,height=500,scrollbars=1');
	/*
	self.scrollTo(0,0);
	
	if(!$('popup-review')){
		var overlay = JQ('<div id="overlay-review"></div>');
		var container = JQ('<div id="popup-review"><div id="top-pop-review"></div><div class="content"></div><div id="bottom-pop-review"></div></div>');
		container.append('<div class="loader"></div>');
		container.append('<a href="javascript: fechaPopReview()" class="bt-fechar"></a>');
		
		var pageSize = getPageSize();
		overlay.css({height: pageSize[1]});
		
		overlay.css({opacity: 0.5});
		
		JQ('body:eq(0)').append(overlay);
		JQ('body:eq(0)').append(container);
		
		JQ('#popup-review .content').load(url, function(){
			JQ('#popup-review .loader').hide();
		});
	}
	*/
}

var setNotaReview = function(id, star){
	$('nota').value = id;
	var valor = (-177 + (star * 36)) - 2;
	JQ('#popup-review .rating-box-layer').css({backgroundPosition: valor + 'px 0'});
}

var fechaPopReview = function(){
	JQ('#overlay-review').remove();
	JQ('#popup-review').remove();
}


var IW = {
	version: '0.1',
	utils: {
		trim: function(string){
			return string.replace(/^\s+|\s+$/g,"");
		},
		implode: function(string, array){
			var result;
			for(var i=0; i<array.length; i++){
				result = result + string + array[i];
			}
			return result;
		}
	}
}
