$(document).ready(function() {
	// Lista de dispositivos a serem redirecionados para vers?o mobile
	// utilizand o userAgent para verifica??o
	var devices = new Array('nokia','iphone','blackberry','sony','lg','htc_tattoo','samsung','symbian','symbianos','elaine','palm','series60','windows ce','android','obigo','netfront','openwave','mobilexplorer','operamini');
	var userAgent = navigator.userAgent.toLowerCase();
	var url_mobile = 'http://www.barastor.com.br/Mobile/';
	$(function(){
	
		// verifica a redireciona caso seja um dispositivo mobile
		if (mobiDetect(userAgent, devices))
			window.location.href = url_mobile;
	});
	
	mobiDetect = function(userAgent, devices) {
		for(var i = 0; i < devices.length; i++) {
			if (userAgent.search(devices[i]) > -1) {
				   return true;
			}
		}
		return false;
	}
				   
	/*//Ajusta posicao do menu e barra inferior
	var totalWidth = window.outerWidth;
	var wHeight = window.innerHeight;
	var totalMenuHeight = parseInt($('.menu ul li.logo-astor').css('height'));
	var barraHeight = parseInt($('.barra-inferior').css('height'));
	var totalHeight = window.outerHeight;
	
	function posicionaMenu(){
		if(totalWidth <= '1024' || totalHeight <= '863'){
			$('.menu').css('top',wHeight+'px');
			$('.barra-inferior').css('top',wHeight + totalMenuHeight - barraHeight + 10 + 'px')
		}else{
			$('.menu').css('top',wHeight - totalMenuHeight - barraHeight + 10 + 'px');
			$('.barra-inferior').css('top',wHeight - barraHeight +'px');
		}
	}
	
	posicionaMenu();
	
	$(window).resize(function(){
		var totalWidth = window.outerWidth;
		var totalHeight = window.outerHeight;
		var wHeight = window.innerHeight;
		var totalMenuHeight = parseInt($('.menu ul li.logo-astor').css('height'));
		var barraHeight = parseInt($('.barra-inferior').css('height'));
		
		function posicionaMenu(){
			if(totalWidth <= '1024' || totalHeight <= '863'){
				$('.menu').css('top',totalHeight+'px');
				$('.barra-inferior').css('top',totalHeight + totalMenuHeight - barraHeight + 50 + 'px')
			}else{
				$('.menu').css('top',wHeight - totalMenuHeight - (barraHeight + 10) + 'px');
				$('.barra-inferior').css('top',wHeight - barraHeight+'px');
			}
		}
		posicionaMenu();
	})*/
	
	//Background resize
    $("#background").each(function () {
		
		
        var image = $("img", this);
		
		$(".overlay", this).css("opacity", ".8");

        var originWidth = parseInt(image.css("width")), originHeight = parseInt(image.css("height"));

        var resizeBackground = function (e) {
            var bw = Math.max($("body").width(), 995), bh = $("body").height(), w, h;
            var rt = originWidth / originHeight;
            w = bw;
            h = bw / rt;
            if (h < bh) {
                h = bh;
                w = bh * rt;
            }

            image.width(w).height(h);
            image.css("top", -(h - bh) / 2);
            image.css("left", -(w - bw) / 2);

            image.parent().width(bw);
        }

        $(window).bind("resize", resizeBackground).resize();
        $(window).unload(function (e) {
            $(window).unbind("resize", resizeBackground);
        });
    });
		
	//Navegacao interna das paginas

		$('.painel-direita div:first').show()
		/*$('.painel-esquerda .menu li a').hover(function(){
			$(this).parent().addClass('active');
		},function(){
			$(this).parent().removeClass('active');
		})*/
		$('.painel-esquerda .menu li:first a:first').addClass('active');
		$('.painel-esquerda .menu li a:not(.active)').live('click',function(){
				$('.painel-esquerda .menu li a.active').removeClass('active');
				$(this).addClass('active');
				var liClass = $(this).parent().attr('class').split(" ")[0];
				var lastClass = $('.painel-esquerda .menu li.active').removeClass('active').attr("class").split(" ")[0];
				$(this).parent().addClass('active');
				$('.painel-direita div.'+lastClass).hide();
				$('.painel-direita div.'+liClass).show();
				var childView = $(this).attr("rel");
				if(childView) {
					$('.painel-direita div.'+liClass).attr("class", liClass+" "+childView);
					console.log(liClass+" "+childView)
				}
		});
	
	//Navegacao interna tela A casa
	
	$('.painel-direita a.sp').click(function(){
		$('.painel-direita .casa').css({
									   'backgroundImage':'url(img/img-teste/conteudo-casa.png)',
									   'backgroundRepeat':'no-repeat',
									   'backgroundPosition':'0 -1px'
									   });
		$('.painel-direita .casa .video-rj').hide();
		$('.painel-direita .casa .video-sp').show();
	});
	$('.painel-direita a.rio').click(function(){
		$('.painel-direita .casa').css({
									   'backgroundImage':'url(img/img-teste/conteudo-casa-rio.png)',
									   'backgroundRepeat':'no-repeat',
									   'backgroundPosition':'-28px -1px'
									   });
		$('.painel-direita .casa .video-sp').hide();
		$('.painel-direita .casa .video-rj').show();
	})
	
	/*//Cria o carrosel de destaques na home page
		$('.carrousel .banner-wrapper img:first').addClass('active-slide');
		var carrouselItens = $('.carrousel img').length;
		for (var i = 0; i < carrouselItens; i++){
			var htmlNav = $('<li><a href="javascript:void(0);" title="slide'+i+'"></a></li>')
			htmlNav.find("a").bind("click", {index:i}, function(e){
					
					var foo = e.data.index;
					if($.browser.msie && $.browser.version.substr(0,1) === '6'){
						$('.carrousel-nav li.active-slide').removeClass('active-slide');
						$(this).parent().addClass('active-slide');
						$('.carrousel img:visible').hide();
						$('.carrousel img').eq(foo).show();
					}else{
						$('.carrousel-nav li.active-slide').removeClass('active-slide');
						$(this).parent().addClass('active-slide');
						$('.carrousel img.active-slide').fadeToggle().removeClass('active-slide');
						$('.carrousel img').eq(foo).fadeToggle().addClass('active-slide');
					}												
				});
			$('.carrousel-nav').append(htmlNav);
			$('.carrousel-nav li').each(function(){
				var itemIndex = $(this).index();
				$(this).addClass('item-'+itemIndex);
				$(this).children('a').addClass('item-'+itemIndex)
			})

				$('.carrousel-nav li:first').addClass('active-slide');

		}*/
		
		$('.carrousel-mini .banner-wrapper img:first').addClass('active-slide');
		var carrouselItens = $('.carrousel-mini img').length;
		for (var i = 0; i < carrouselItens; i++){
			var htmlNav = $('<li><a href="javascript:void(0);" title="slide'+i+'"></a></li>')
			htmlNav.find("a").bind("click", {index:i}, function(e){
					clearInterval(temporizador);
					foo = e.data.index;
					
					if($.browser.msie && $.browser.version.substr(0,1) === '6'){
						$('.carrousel-nav li.active-slide').removeClass('active-slide');
						$(this).parent().addClass('active-slide');
						$('.carrousel-mini img:visible').hide();
						$('.carrousel-mini img').eq(foo).show();
					}else{
						$('.carrousel-nav li.active-slide').removeClass('active-slide');
						$(this).parent().addClass('active-slide');
						$('.carrousel-mini img.active-slide').fadeToggle().removeClass('active-slide');
						$('.carrousel-mini img').eq(foo).fadeToggle().addClass('active-slide');
					}
					temporizador = setTimeout(trocarDestaque, 5000);
				});
			$('.carrousel-nav').append(htmlNav);
			$('.carrousel-nav li').each(function(){
				var itemIndex = $(this).index();
				$(this).addClass('item-'+itemIndex);
				$(this).children('a').addClass('item-'+itemIndex)
			})

				$('.carrousel-nav li:first').addClass('active-slide');

		}
});
var foo = 0;

var temporizador = setTimeout(trocarDestaque, 5000);

function trocarDestaque(){
	foo = (foo + 1) < 3 ? (foo +1) : 0;
	
	$('.item-'+foo).click();
	}

