/*
	Projeto - Robert Oatley (Vineyards)
	Developer(s):
		+ Fernando Basilio (fernando.basilio@hellointeractive.com.br)
*/

var intAutoSlideClick = null;
var intAutoSlide = null;
var intLanding = null;

// Return GET params
function returnParam(url,paramName){
	for(var qs = url.split("&"), i=0; i < qs.length; i++)
		if(qs[i].indexOf(paramName)>=0)
			return qs[i].split("=")[1];
	return false;
}

// Flash Calls
function openLink(url) {
	if(!url.match('http')) {
		switch(url) {
			case "example": window.location = 'http://www.example.com.br/'; break;
			case "exampl2": doSomething(); return true; break;
			default: return false;
		}
	}else
		window.location = url;
	return true;
}

// Popup handler
function openPopup(u,n,w,h,o,c) {
	var l = t = 18;
	if (c) {
		l = (screen.availWidth-w) / 2;
		t = (screen.availHeight-h) / 2;
	}
	p = window.open(u,'pop_'+n,'left='+l+',top='+t+',width='+w+',height='+h+',scrollbars='+((o)?o:'1'));
}

// bg site flash
function bgSite(){
	var so = new SWFObject("/swf/bgSite.swf", "bgSite", "100%", "100%", "7", "");
	so.addParam("quality", "high");
	so.addParam("wmode", "transparent");
	so.write("container");
}

// centralizeElement
function centralizeElement(obj){
	if ($("body").height() > $("#content").height()) {
		$(obj).css({top: '50%', marginTop: -($(obj).height()/2) + 'px'});
	}else {
		$(obj).css({marginTop: '0px', top: '0px'});
	}
}


// next Photo
var viewPhoto;
var idNextPhoto;

function nextPhoto(fotos, area){

	if(viewPhoto >= $(fotos).length-1){
		viewPhoto = 0;
	}else{
		viewPhoto += 1;
	}

	/*
	console.log('view - ' + viewPhoto);
	console.log('next - ' + idNextPhoto);
	console.log(' ');
	*/

	$('dl dd').removeClass('on');
	$('.nav' + area + ' dd').eq(viewPhoto).addClass('on');				

	$('.boxImgContent' + area + ' img').animate({opacity: 0 }, 1000, function(){

		$('.boxImgContent' + area + ' img').attr('src',fotos[viewPhoto]);
		$('.boxImgContent' + area + ' img').attr('alt',viewPhoto + 1);

		$('.boxImgContent' + area + ' img').animate({
				opacity: 1 
			}, 1000, function(){
				$('.boxImgContent' + area).css('background','url(' + fotos[idNextPhoto] + ')');
			}
		);
	})

	if(idNextPhoto == $(fotos).length-1){
		idNextPhoto = 0;
	}else{
		idNextPhoto += 1;
	}

}

// timerSlide
function timerSlide(timer, fotos, area){
	viewPhoto	= 0;
	idNextPhoto	= 1;
	intAutoSlide = setInterval(function(){
		nextPhoto(fotos, area);
	}, timer);
}


// nextPhotoHome
var vPhoto = 0;

function nextPhotoHome(){

	var idNextPhoto = parseInt(vPhoto) + 1;
		//console.log(vPhoto);
	
	$('.boxImgContentHome img').animate({opacity: 0 }, 1000, function(){
		$('.boxImgContentHome img').attr('src','/img/imgHome'+vPhoto+'.jpg');
		$('.boxImgContentHome img').attr('alt',vPhoto);

		if(idNextPhoto == 4){
			idNextPhoto = 0;
		}

		$('.boxImgContentHome img').animate({
				opacity: 1 
			}, 1000, function(){
				$('.boxImgContentHome').css('background','url(/img/imgHome'+idNextPhoto+'.jpg)');
			}
		);
	});
	if(vPhoto == 3){
		vPhoto = 0;
	}else{
		vPhoto += 1;
	}
}

// slideHome
function slideHome(timer){
	intAutoSlide = setInterval(function(){
		nextPhotoHome();
	}, timer);
}

// slideFoto 
function slideFoto(){
	window.setTimeout(function(){
		$('.boxImgContent').animate({
				width: '282px'
			}, 500, function(){
				$('.boxContent').animate({
					//opacity: '1',
					right: '0'
				}, 500, function(){
					$('.boxImgContent img').animate({
						opacity: '0'
					});
				});
			}
		);
	}, 2000);
	clearInterval(intAutoSlide);
}

// slideText
function slideText(){
	$('.boxImgContent').animate({
			width: '282px'
		}, 500, function(){
			$('.navSubVineyards').css('display','block');
			$('.boxContent').animate({
				//opacity: '1',
				right: '0'
			}, 500);
		}
	);

}


// animaHome
function animaHome(){
	$('.boxImgContentHome').animate({
			height: '372px'
		}, 1000, function(){
			$('.boxImgContentHome').css('background','url(/img/imgHome.jpg)');
			
			var intLanding = setInterval(function(){
				$('.boxImgContentHome img').css('cursor','pointer');
				$('.boxImgContentHome img').animate({opacity: 0 }, 1000, function(){
					slideHome(3000);
					clearInterval(intLanding);
				});	
			},6000);
			// 15000
			
			$('.boxImgContentHome img').click(function(){
				var urlHome = $(this).attr('alt')
				switch(urlHome){
					case "0":
						$.get("/heritage/contentHeritage.aspx",{idNav: "heritage"},function(data) {
							$(".contentItem").empty().html(data);
						});
						//urlHome = "/heritage/contentHeritage.aspx";
						break
					case "1":
						$.get("/heritage/contentHeritage.aspx",{idNav: "heritage"},function(data) {
							$(".contentItem").empty().html(data);
						});
						//urlHome = "/heritage/contentHeritage.aspx";
						break
					case "2":
						$.get("/wines/contentWines.aspx",{idNav: "wines"},function(data) {
							$(".contentItem").empty().html(data);
						});
						//urlHome = "/wines/contentWines.aspx?idNav=wines";
						break
					case "3":
						$.get("/vineyards/contentVineyards.aspx",{idNav: "vineyards"},function(data) {
							$(".contentItem").empty().html(data);
						});
						//urlHome = "/vineyards/contentVineyards.aspx?idNav=0";
						break
					default:
						return false;
						break;
					}
				$(this).css('cursor','default');
				clearInterval(intAutoSlide);
				$('.boxImgContentHome').remove();
				$('#loader').show();
				//$('.contentItem').load(urlHome);
				
				
			});
		}
	);
}

// animaHomeVine
function animaHomeVine(images){
	clearInterval(intAutoSlide);
	timerSlide(3000, images,"Vineyards");

	$(".boxImgContent img").attr("src",images[0]);
	$(".boxImgContent").css("background","url(" + images[1] + ")");

	$(".boxImgContentVineyards img").click(function(){
		$(this).css("cursor","default");
		clearInterval(intAutoSlide);
		$(".contentItem").empty();
		$(".contentItem").load("/vineyards/contentVineyards.aspx?idNav="+$(this).attr("alt"));
	});

	$(".navVineyards dt").addClass("on");
	$(".navVineyards dd").eq(0).addClass("on");
}


// animaHomeHeritage
function animaHomeHeritage(fotos){
	clearInterval(intAutoSlide);
	var viewPhoto = 0;
	timerSlide(3000, fotos, "Heritage");

	$(".boxImgContentHeritage img").attr("src","/img/imgHeritageLanding.jpg");
	$(".boxImgContentHeritage").css("background","url(/img/imgHeritage0.jpg)");

	$(".boxImgContentHeritage img").click(function(){
		//alert($(this).attr("alt"));
		$(this).css("cursor","default");
		clearInterval(intAutoSlide);
		$(".contentItem").empty();
		$(".contentItem").load("/heritage/contentHeritage.aspx?idNav="+$(this).attr("alt"));
	});

	$(".navHeritage dt").addClass("on");
	$(".navHeritage dd").eq(0).addClass("on");
}


//landingHomeHeritage
/*
function landingHomeHeritage(fotos){
	$("dl dd").removeClass("on");
	$('.imgContent').show();
	$('.imgContent').css("opacity","0");	

	$('.boxImgContentHeritage img').animate({
			opacity: 1 
		}, 1000, function(){
			var intLanding = setInterval(function(){
				$('.boxImgContentHeritage').css('background','url(' + fotos[0] + ')');
				$('.boxImgContentHeritage img').animate({opacity: 0 }, 1000, function(){
					animaHomeHeritage(fotos);
					clearInterval(intLanding);
				});	
			},5000);
		}
	);	

}
*/



//landingHomeHeritage
function landingHomeHeritage(fotos){
	$("dl dd").removeClass("on");
	$('.imgContent').show();
	$('.imgContent').css("opacity","0");	

	$('.boxImgContentHeritage img').animate({
			opacity: 1 
		}, 1000, function(){
			$('.boxImgContentHeritage').css('background','url(/img/imgHeritageLandingText.jpg) no-repeat');

			var intText = setInterval(function(){
				$('.boxImgContentHeritage img').animate({opacity: 0 }, 1000, function(){
					$('.boxImgContentHeritage img').attr('src', '/img/imgHeritageLandingText.jpg');
					$('.boxImgContentHeritage img').css('opacity', '100');
					$('.boxImgContentHeritage img').show();
					$('.boxImgContentHeritage').css('background','url('+fotos[0]+') no-repeat');
	
					var intLanding = setInterval(function(){
						$('.boxImgContentHeritage img').animate({opacity: 0 }, 1000, function(){
							animaHomeHeritage(fotos);
							clearInterval(intLanding);
						});					
					},6000);
					
				});	

				clearInterval(intText);
			},1000);

		}
	);	

}

//landingHomeVineyards
function landingHomeVineyards(fotos){
	$("dl dd").removeClass("on");
	$('.imgContent').show();
	$('.imgContent').css("opacity","0");	

	$('.boxImgContentVineyards img').animate({
			opacity: 1 
		}, 1000, function(){
			$('.boxImgContentVineyards').css('background','url(/img/imgVineyardsLandingText.jpg) no-repeat');

			var intText = setInterval(function(){
				$('.boxImgContentVineyards img').animate({opacity: 0 }, 1000, function(){
					$('.boxImgContentVineyards img').attr('src', '/img/imgVineyardsLandingText.jpg');
					$('.boxImgContentVineyards img').css('opacity', '100');
					$('.boxImgContentVineyards img').show();
					$('.boxImgContentVineyards').css('background','url('+fotos[0]+') no-repeat');
	
					var intLanding = setInterval(function(){
						$('.boxImgContentVineyards img').animate({opacity: 0 }, 1000, function(){
							animaHomeVine(fotos);
							clearInterval(intLanding);
						});					
					},6000);
					
				});	

				clearInterval(intText);
			},1000);

		}
	);	

}



// MODAL ------------------------------------------------------------------------------------------
//closeModal
function closeModal() {
	/*HIDE MODAL*/
	$('.view_modal').fadeTo('fast', 0, function() {
		$(this).remove();
	});
	
	/*HIDE BACKGROUND*/
	$('.bg_modal').fadeTo('fast', 0, function() {
		$(this).remove();

		/*SHOW SELECT*/
		$('select').css('visibility', 'visible');
	});
}

// openModal
function openModal(url) {
	/*CREATE ELEMENTS*/
	$('body').append($('<div></div>').addClass('bg_modal')).append($('<div></div>').addClass('view_modal'));
	$('.bg_modal').width($('html')[0].scrollWidth).height($('html')[0].scrollHeight);

	/*OPACITY*/
	$('.view_modal').css('opacity', 0);
	$('.bg_modal').css('opacity', 0);
	
	/*SELECTS HIDE*/
	$('select').css('visibility', 'hidden');

	/*SHOW BACKGROUND*/
	$('.bg_modal').fadeTo('fast', 0.5, function() {
		$('.view_modal').load(url + '?' + (new Date()).getTime(), function() {
			/*REMOVE LOAD*/
			$('.load').remove();
			
			/*CENTRALIZE MODAL*/
			$('.view_modal').css({marginTop: -$('.view_modal').height()/2 + 'px', marginLeft: -$('.view_modal').width()/2 + 'px'});
			//$('.view_modal').css({marginLeft: -$('.view_modal').width()/2 + 'px'});
			
			
			/*MODAL HIDE*/
			$('.view_modal').fadeTo('fast', 1);
			
			/*CLOSE MODAL*/
			$("a[@rel~='modalclose']").click(function() {
				closeModal();
				return false;
			})

			$(".conteudo_modal dt").each(function(i) {
				$(this).find("a").click(function(){
					$('.conteudo_modal dd').hide();
					$('.conteudo_modal dd:eq('+i+')').show();
					return false;
				});
			});
		});
	});
	

	$('.bg_modal').click(function(){
		$('.view_modal').fadeTo('fast', 0, function() {
			$('contentModal').remove();
		});
//		/*HIDE BACKGROUND*/
		$('.bg_modal').fadeTo('fast', 0, function() {
			$(this).remove();
			/*SHOW SELECT*/
			$('select').css('visibility', 'visible');
		});
	});

}

// initModal
function initModal() {
	$("a[@rel~='modal']").click(function() {
		openModal($(this).attr('href'));
		window.scroll(0,0);
		return false;
	})
}




// validação
function checkEmail(obj) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj)) return true;
}

function checkBlank(obj){
	if ($(obj).val() != '') return true;
	else $(obj).focus();
}

function checkNaN(obj){
	if (isNaN($(obj).val()) == true) return true;
}



// startHome
function startHome(){
	$('.boxImgContentHome').hide();
	$('.landing_home').remove();
	$('.footer dl, .footer ul, .loading, .conteudo').show()

	var img = new Image();
	var img0 = new Image();
	var img1 = new Image();
	var img2 = new Image();
	var img3 = new Image();

	$(img).load(function () {
		$(this).hide();
		$('#divImg').append(this);
		$(this).show();

		if($(img).load){

			$(img0).load(function () {
				$(this).hide();
				$('#divImg0').append(this);
				$(this).show();

				if($(img0).load){
					
					$(img1).load(function () {
						$(this).hide();
						$('#divImg1').append(this);
						$(this).show();

						if($(img1).load){
							
							$(img2).load(function () {
								$(this).hide();
								$('#divImg2').append(this);
								$(this).show();

								if($(img2).load){

									$(img3).load(function () {
										$(this).hide();
										$('#divImg3').append(this);
										$(this).show();

										if($(img3).load){

											$('#loader').remove();
											$('.boxImgContentHome').show();
											$('.boxImgContentHome img').css('cursor','default');
											animaHome();
											$('.divImgagem').remove();
											
										}

									}).error(function () {
									}).attr('src', '/img/imgHome3.jpg');

								}

							}).error(function () {
							}).attr('src', '/img/imgHome2.jpg');
						}

					}).error(function () {
					}).attr('src', '/img/imgHome1.jpg');
				}

			}).error(function () {
			}).attr('src', '/img/imgHome0.jpg');
		}

	}).error(function () {
	}).attr('src', '/img/imgHome.jpg');

}

// ---

var robio = function(o) {

	$(".loader").ajaxStart(function(){
		$(this).show();
		$(".conteudo").hide();
	});
	
	$(".loader").ajaxStop(function(){
		$(this).hide();
		$(".conteudo").show();
		$('#pane2').jScrollPane({showArrows:true});
		if(!$('.jScrollPaneContainer').find('.jScrollArrowUp').is(':visible') || !$('.jScrollPaneContainer').find('.jScrollArrowDown').is(':visible')){
			$('.textScroll').css('background','none');
			$('.scroll-pane').css('overflow', 'visible');
		}
	});
	
	$.get("/heritage/contentHeritage.aspx",{idNav: $(o).attr('href')},function(data) {
		$(".contentItem").empty().html(data);
		$('.boxImgContentHome').remove();
	});
	return false;
}