var defaultViewMode = "full";
var bg = null;
var bgimg = null;
var bgmask = null;
var menu = null;
var mainbody = null;
var showingNews = false;
var showingLancamentos = true;
var mouse = {x:0, y:0};
var scrolledLeft = 0;

$(document).ready(function(){
	bg 		 = $("#bg");
	bgimg 	 = $("#bg #bgimg");
	bgmask   = $("#bgMask");
	menu     = $("#menu");
	mainbody = $("#mainInnerBody");
	ImageViewMode(defaultViewMode);
	
	var elProLancamentos = $("#rodape .lancamentosLine .listaLancamentos .listaLancamentosOverflow");
	elProLancamentos.width(elProLancamentos.children().length*166);
	
	$("#listaLinhasProdutos .produtoLinha A").each(function(i, el){
		$(el)
			.bind('mouseenter', function(e){
				$("#listaLinhasProdutos .produtoLinha A")
					.not(e.currentTarget)
					.find("IMG")
					.stop(true)
					.animate({
						opacity: 0.3
					}, 132, "easeInOutBack");
			})
			.bind('mouseleave',function(e){
				$("#listaLinhasProdutos .produtoLinha A")
					.find("IMG")
					.animate({
						opacity: 1
					}, 132, "easeInOutBack");
			})
	});
	
	
	$("#maskLinha").bind('click', function(e){
		$("#listaLinhasProdutos .linhaProduto[rel='1']").trigger('click');
	});
	
	$("#listaLinhasProdutos .linhaProduto").each(function(i, el){
		$(el).bind('click', function(e){
			if($(this).attr('rel') == 0){
				try{
					$("#listaLinhasProdutos .linhaProduto[rel='1']").trigger('click');
				}catch(e){}
			}
			var status = $(this).attr('rel') == '1';
			
			try{
				var nLin = Math.round($(this).parent().find(".produtoLinha A").children().length / 4.5);
				var nPro = $(this).parent().find(".produtoLinha A").children().length;
//				$("#maskLinha").toggle("fade");
				$(this)
					.parent()
					.find(".produtoLinha")
					.stop(true)
					.animate({
						width: (status)?'0px':(nPro < 6)?(nPro*155)+'px':'908px',
						height: (status)?'0px':(nLin*90)+'px',//'181px',
						padding: (status)?'0px':'10px'
					}, 512, "easeOutBack");
			}catch(e){
			}
			
			$(this).attr('rel', ($(this).attr('rel') == 1)?0:1);
		});
	});
	
	if((elProLancamentos.children().length*165) < $(window).width()){
		$("#btDir").hide();
	}
	
	$("#btEsq").bind('click', function(e){
		scrolledLeft--;
		$("#rodape .lancamentosLine .listaLancamentos")
			.scrollTo('.produtos:eq('+scrolledLeft+')', 1000, {offset:0, easing:'easeInOutBack' });
		if(scrolledLeft <= 0){
			$("#btEsq").fadeOut();
		}
	});
	
	$("#btDir").bind('click', function(e){
		scrolledLeft++;
		$("#rodape .lancamentosLine .listaLancamentos")
			.scrollTo('.produtos:eq('+scrolledLeft+')', 578, {
				offset:0, 
				easing:'easeInOutBack',
				onAfter: function(){
					$(".produtos").each(function(i, el){
						$(el).find(".produtoDetalhe").css('left', $(el).position().left);
					});
				}
			});
		if(scrolledLeft > 0){
			$("#btEsq").fadeIn();
		}
		
		if(scrolledLeft+1 >= elProLancamentos.children().length){
			$("#btDir").fadeOut();
		}
	});
	
	$("#btNewsletterBind").bind("click", function(e){
		var t = $("#btNewsletter")
			.animate({
				width:(!showingNews)?'587px':'160px'
			})
			.find(".wgContainerNewsletterBox");
			if(!showingNews){
				t.css("display","block");
			}
			t.animate({
				width:(!showingNews)?'417px':'0px'
			},700,'easeOutBack', function(){
				showingNews = !showingNews;
				if(!showingNews){
					t.css("display","none");
				}
			});
	});

	$(window).resize(function() {
		FullScreenBackground($("#bg IMG")); //scale bg image
	});
	
	LargeImageLoad(bgimg);
	
	if(jQuery.browser.msie){
//		LargeImageLoad(bgimg);
		$(".compartilharEmail").css('paddingTop', '8px');
		$(".compartilharEmail").css('paddingLeft', '42px');
		$(".compartilharEmail").css('paddingRight', '15px');
	}
	
	bgimg.load(function() {
		LargeImageLoad($(this));
	});
	
	$(".produtos").each(function(i, el){
		$(el)
		.bind('mouseenter', function(e){
			$(".produtos")
				.not(e.currentTarget)
				.stop(true)
				.animate({ opacity: 0.5 }, 530, 'easeOutBack');
			
			var lf = $(e.currentTarget).offset().left;
			var eF = $(e.currentTarget);
			if((eF.offset().left - 117) > 0){
				lf = lf - 35;
				var posX = (eF.position().left - 35) + 250;
				if(posX > $(window).width()){
					lf -= (posX - $(window).width());
				}
			}
			$(e.currentTarget)
					.find(".produtoDetalhe")
					.stop(true)
					.animate({
						width:'250px',
						height:'326px',
						marginTop:'-350px',
						left: lf
					}, 350, 'easeOutBack');
		})
		.bind('mouseleave', function(e){
			var lf = $(e.currentTarget).offset().left
			$(".produtos")
				.stop(true)
				.animate({ opacity: 1 }, 530, 'easeOutBack');
			$(e.currentTarget)
				.find(".produtoDetalhe")
				.stop(true)
				.animate({
					width:'0px',
					height:'0px',
					marginTop:'-125px',
					left: lf+35
				}, 550, 'easeOutBack');
		});
	});
	
	$('.lancamentosLine .listaLancamentos').animate({
		height: (showingLancamentos)?'0px':'121px'
	}, 713, (showingLancamentos)?"easeInBack":"easeOutBack", function(){
		showingLancamentos = !showingLancamentos;
	});
	
	/*$('.lancamentosLine > h2').bind('click', function(){
		$('.lancamentosLine .listaLancamentos').animate({
			height: (showingLancamentos)?'0px':'121px'
		}, 713, (showingLancamentos)?"easeInBack":"easeOutBack", function(){
			showingLancamentos = !showingLancamentos;
		});
	})*/
	
	pageIn();
	
	$('.lancamentosLine .produtos').each(function(i,el){
		$(this).attr('link', $(this).attr('href'));
		$(this).attr('href',"#");
		$(this).bind('click', function(e){
			loadPage(e.currentTarget);
			//$("#maskLinha").trigger('click');
			//pageOut($(this));
		});
	});
	
	$("A[data='menu']").each(function(i,el){
		$(this).attr('link', $(this).attr('href'));
		$(this).attr('href',"#");
		$(this).bind('click', function(e){
			loadPage(e.currentTarget);
		});
	});
	
	$("A[data='anim_control']").each(function(i,el){
		$(this).attr('link', $(this).attr('href'));
		$(this).attr('href',"#");
		$(this).bind('click', function(e){
			loadPage(e.currentTarget);
			$("#listaLinhasProdutos .linhaProduto[rel='1']").trigger('click');
			//pageOut($(this));
		});
	});
	
	$("#NewsletterBox_nome").Watermark("Seu Nome","#562a6b");
	$("#NewsletterBox_email").Watermark("Seu Email","#562a6b");
	
});

function loadPage(el){
	urlLink = $(el).attr('link');
	$("#maskLinha").fadeIn();
	$("#loading_page").fadeIn();
	$("#bodyInnerContent").fadeOut();
	$.ajax({
		url: urlLink,
		async: true,
		cache: true,
		success: function(data, textStatus){
			$("#bodyInnerContent").html(data);
			$("#bodyInnerContent").fadeIn();
			$("#maskLinha").fadeOut();
			$("#loading_page").fadeOut();
	    }
	});
}

$(document).mousemove(function(e){
    mouse = {x:e.pageX, y:e.pageY}
 }); 

function pageIn(){
	//bgmask.fadeIn("fast");
	bodyIn(function(){
		menuIn();
		setTimeout(function(){
			lancamentosIn();
		}, 321);
	});
}


function pageOut(el){
	setTimeout(function(){
		lancamentosOut();
	}, 350)
	
	menuOut(function(){
		bodyOut();
		bgimg.fadeOut("slow", function(){
			setTimeout(function(){
				window.location.assign(el.attr('link'));
			}, 500);
		})
	});
}

function bodyIn(nextFc){
	mainbody.fadeIn("fast");
	if(nextFc){
		setTimeout(function(){
			nextFc();
		}, 400);
	}
}

function bodyOut(){
	mainbody.fadeOut("fast");
}

var inMenuEl = 7;
var intervalMenuInit = null;
function menuIn(){
	menu.find("LI").each(function(i, el){
		var options = {};
		$(el).toggle( "fold", options, 150*i, function(){
			inMenuEl--;
		});
	});
	var options = {};
	$("#btNewsletter").toggle("drop", options, 450);
	$("#addMidias").toggle("drop", options, 420);
	linhasIn();
	
	intervalMenuInit = setInterval(function(){
		if(inMenuEl == 0){
			clearInterval(intervalMenuInit);
			initMenu();
		}
	}, 100);
}

function menuOut(nextFc){
	menu.find("LI").reverse().each(function(i, el){
		var options = {};
		$(el).toggle( "fold", options, 200*i ,function(){
			inMenuEl++;
		});
	});
	var options = {};
	$("#btNewsletter").toggle("drop", options, 450);
	$("#addMidias").toggle("drop", options, 420);
	linhasOut();
	
	intervalMenuInit = setInterval(function(){
		if(inMenuEl == 7){
			clearInterval(intervalMenuInit);
			nextFc();
		}
	}, 100);
}

function linhasOut(){
	$("#listaLinhasProdutos .linhaProduto").reverse().each(function(i, el){
		if($(el).attr('rel') == '1'){
			$(el).trigger('click');
		}
		$(el).toggle( "fold", {}, 150*i);
	});
}


function linhasIn(){
	$("#listaLinhasProdutos .linhaProduto").each(function(i, el){
		$(el).toggle( "fold", {}, 135*i);
	});
}

function lancamentosOut(){
	var options = {};
	$("#rodape .lancamentosLine").toggle( "fold", options, 150);
	setTimeout(function(){
		$("#rodape").toggle( "fold", options, 150);
	}, 340)
}

function lancamentosIn(){
	var options = {};
	$("#rodape").toggle( "fold", options, 150);
	
	setTimeout(function(){
		$("#rodape .lancamentosLine").toggle( "fold", options, 150, function(){
			$(".produtos").each(function(i, el){
				$(el).find(".produtoDetalhe").css('left', $(el).position().left);
			});
		});
	}, 540)
}

var oldBG = null;
function LargeImageLoad($this){
	//$preloader.fadeOut("fast"); //hide preloader
	$this.removeAttr("width").removeAttr("height").css({ width: "", height: "" }); //lose all previous dimensions in order to rescale new image data
	bg.data("originalImageWidth",$this.width()).data("originalImageHeight",$this.height());
	if(bg.data("newTitle")){
		$this.attr("title",bg.data("newTitle")); //set new image title attribute
	}
	FullScreenBackground($this); //scale new image

	if(oldBG != null){
		oldBG.fadeOut("slow");
	}
	$this.fadeIn("slow"); //fadein background image
	oldBG = $this;
}


//Image scale function
function FullScreenBackground(theItem){
	if(theItem == null){
		return;
	}
	var winWidth=$(window).width();
	var winHeight=$(window).height();
	var imageWidth=theItem.width();
	var imageHeight=theItem.height();
	if(defaultViewMode !="original" ){ //scale
		theItem.removeClass("with_border").removeClass("with_shadow"); //remove extra styles of orininal view mode
		var picHeight = imageHeight / imageWidth;
		var picWidth = imageWidth / imageHeight;
		if(defaultViewMode != "fit"){ //image view mode: full
			if ((winHeight / winWidth) < picHeight) {
				theItem.css("width",winWidth).css("height",picHeight*winWidth);
			} else {
				theItem.css("height",winHeight).css("width",picWidth*winHeight);
			};
		} else { //image view mode: fit
			if ((winHeight / winWidth) > picHeight) {
				theItem.css("width",winWidth).css("height",picHeight*winWidth);
			} else {
				theItem.css("height",winHeight).css("width",picWidth*winHeight);
			};
		}
		//center it
		theItem.css("margin-left",((winWidth - theItem.width())/2)).css("margin-top",((winHeight - theItem.height())/2));
	} else { //no scale
		//add extra styles for orininal view mode
		theItem.addClass("with_border").addClass("with_shadow");
		//set original dimensions
		theItem.css("width",$bg.data("originalImageWidth")).css("height",$bg.data("originalImageHeight"));
		//center it
		theItem.css("margin-left",((winWidth-theItem.outerWidth())/2)).css("margin-top",((winHeight-theItem.outerHeight())/2));
	}
}

//image view mode function - full or fit
function ImageViewMode(theMode){
	FullScreenBackground(bgimg); //scale bg image
	//re-style button
	if(theMode=="full"){
	} else if(theMode=="fit") {
	} else {
	}
}


function AddScript(url, object){
    if (object != null){
        // add script
        var script   = document.createElement("script");
        script.type  = "text/javascript";
        script.src   = "path/to/your/javascript.js";
        document.body.appendChild(script);

        // remove from the dom
        document.body.removeChild(document.body.lastChild);
        return true;
    } else {
        return false;
    };
};

function DeleteObject(UnusedReferencedObjects) {
    delete UnusedReferencedObjects;
}

jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
}; 
