/*
'  Taggus - www.taggus.com.br
'  Sessão:  Desenvolvimento para Internet
'  Tipo:    Programação de Interface
'  Projeto: Portal Leão Jr
'  Cliente: Spirit Comunicação (www.spiritweb.com.br)
'  Desenv.: Fabio de Assis (fabio@taggus.com.br)
'  Data:    06/2010
*/

(function($) {
	$.fn.SSProduct = function(options) {
		
		$.fn.SSProduct.defaults = {
			width: 1000,
			height: 500,
			objList: $(this).children("ul"),
			objItem: $(this).children("ul").children("li"),
			objRight: "#btnRight",
			objLeft: "#btnLeft",
			itemWidth: 490,
			itemHeight: 490,
			itemMargin: 5,
			OffSetX: true	// Define se o slide funciona Horizontal (true) ou Vertical (false)
		};
		var e = $.extend({}, $.fn.SSProduct.defaults, options);
		
		$(this).css({
			height:e.height,
			overflow: "hidden",
			position:"relative",
			width:e.width
		});
		
		e.objList.css({
			position:"absolute",
			width: ((e.itemMargin * 2) + e.itemWidth) * e.objItem.length
		});
		
		// Tamanho do Obj		e.width
		var listWidth = e.objList.width();
		// Quantidade items		e.objItem.length
		var itemWidth = (e.itemMargin * 2) + e.itemWidth;
		var	itemVisuais = e.width / ((e.itemMargin * 2) + e.itemWidth);
		
		e.objItem.css({
			backgroundColor: "#FFC",
			display: (e.OffSetX == true)?("inline"):("block"),
			float: (e.OffSetX == true)?("left"):("none"),
			height: e.itemHeight,
			margin: e.itemMargin,
			width: e.itemWidth
		});
		
		
		$(e.objRight).hover(function(){
			e.objList.animate({
				left:-(listWidth-(itemWidth * itemVisuais))
			}, 5000);
		}, function(){
			e.objList.stop();
			if((-parseInt(e.objList.css("left")) % itemWidth) < (itemWidth / 2)) {
				e.objList.animate({
					left:parseInt(parseInt(e.objList.css("left")) / itemWidth) * itemWidth
				}, 250);
			} else {
				e.objList.animate({
					left:-((-parseInt(parseInt(e.objList.css("left")) / itemWidth) + 1) * itemWidth)
				}, 250);
			}
		});
		
		
		$(e.objLeft).hover(function(){
			e.objList.animate({
				left:0
			}, 5000);
		}, function(){
			e.objList.stop();
			if((-parseInt(e.objList.css("left")) % itemWidth) < (itemWidth / 2)) {
				e.objList.animate({
					left:parseInt(parseInt(e.objList.css("left")) / itemWidth) * itemWidth
				}, 250);
			} else {
				e.objList.animate({
					left:-((-parseInt(parseInt(e.objList.css("left")) / itemWidth) + 1) * itemWidth)
				}, 250);
			}
		});
		
	};
	
})(jQuery);



/* Navegacao entre paginas */
function AjaxGetPage(idFile,Ancor,idObject){
	
	$("#dvLoad").fadeIn("slow", function(){
		
		$.ajax({
			cache: true,
			type: "POST",
			url: idFile + ".php",
			data: "File=True",
			success: function(data){
				$("#Main").css({display:"block"});
				$(idObject).html(data);
				$("#dvLoad").fadeOut("slow", function(){ if (Ancor != "") GetAncor(Ancor); });
				StartScriptsOnTheFly(idFile);
				$(idObject).removeClass().addClass(idFile);
				//pageTracker._trackPageview(idFile);
				}, 
			error: function(){
				AjaxGetPage("home",Ancor,idObject);
				},
			dataType: "html"
		});
	});

	GlobalSession = idFile;
	GlobalAncor = Ancor;
}

function ReplaceHref(obj){
	var $link = obj; 
	var href = $link.attr('href');

	href = href.replace("#", "?"); 
	href = href.replace("index.php?", ""); 
	href = href.replace("File=", "");

	if(href.slice(0,1)=="?") href=href.replace("?","");

	$link.attr('href',href.replace(href,'#'+href));

	var count = href.indexOf("?");
	if (count == "-1") count = href.length;
	var Ancor = href.slice(count + 1);
	href = href.slice(0,count);

	AjaxGetPage(href,Ancor,"#Content");
}

function GetAncor(ancora){
	alert(ancora);
}

function ScrollWindow(id){
	window.scrollTo(0,document.getElementById(id).offsetTop);
}

function StartScriptsOnTheFly(idFile){
	if(idFile == "home") {
		$(".SSProduct").SSProduct();
	}
	else if(idFile =="contato") {
		$("#ContactForm input#enviar").click(function(){ SendContactForm("#ContactForm", "#validate"); });
		
	}
	else if(idFile =="produtos") {
		$("#Produtos").jcarousel({
			wrap: "circular",
			scroll: 1
		});
		
	} else if (idFile == "prepare"){
		$("#prepare").jcarousel({
			wrap: "circular",
			scroll: 1
		});
			
		
	}else {
		$("#Produtos,#mini").jcarousel({
			wrap: "circular",
			scroll: 1
		});
		$("#voleiul").jcarousel({
			wrap: "circular",
			scroll: 1
		});
		$("#depoimentos ul").jcarousel({
			wrap: "circular",
			scroll: 1
		});
		$('.carroussel').jcarousel({
			wrap: "circular",
			scroll: 1
		});
		$(function () {
                        var tabContainers = $('div.tabs > div');
                        tabContainers.hide().filter(':first').show();
                        
                        $('div.tabs ul.tabNavigation a').click(function () {
                                tabContainers.hide();
                                tabContainers.filter(this.hash).show();
                                $('div.tabs ul.tabNavigation a').removeClass('selected');
								$('div.tabs div').removeClass('divselected');
                                $(this).addClass('selected');
								var id = $(this).attr('href');
								$(id).addClass('divselected');
                                return false;
                        }).filter(':first').click();
                });
	}
	if(idFile =="produto") {
		$('.Scroll').jScrollPane({scrollbarWidth:8});
		
		var actual = $('.prodText .tabNavigation a.selected').attr("href");
		actual = actual.replace("#","");
		
		$(".ContProd .t").css({display:"none"});
		
		$(".ContProd ." + actual).css({display:"block"});
		
		$('.prodText .tabNavigation a').click(function(){
					
			var novo = $(this).attr("href");
			novo = novo.replace("#","");
			
			$(".ContProd .t").css({display:"none"});
			
			$(".ContProd ." + novo).css({display:"block"});
			
			$('.prodText .tabNavigation a').removeClass("selected");
			$(this).addClass("selected");
					
			return false;
		})
		
	}
}

/*Validação de E-mail*/
function SendContactForm(ObjForm, idObject){
	var error = false;
	$(ObjForm).find(":input").each(function(i) {
		
		var Value = $(this).val().replace(/^\s+|\s+$/g,"");
		
		if($(this).attr("required") == "true") {
			if(Value == "") {
				$(this).css({backgroundColor:"#f47e32"});
				error = true;
			} else if($(this).attr("mask") == "email" && CheckEmail(Value) == false) {
				$(this).css({backgroundColor:"#f47e32"});
				error = true;
			} else if($(this).attr("mask") == "ddd" && CheckDDD(Value) == false) {
				$(this).css({backgroundColor:"#f47e32"});
				error = true;
			} else if($(this).attr("mask") == "tel" && CheckTel(Value) == false) {
				$(this).css({backgroundColor:"#f47e32"});
				error = true;
			}else if($(this).attr("mask") == "cep1" && CheckCEP1(Value) == false) {
				$(this).css({backgroundColor:"#f47e32"});
				error = true;
			} else if($(this).attr("mask") == "cep2" && CheckCEP2(Value) == false) {
				$(this).css({backgroundColor:"#f47e32"});
				error = true;
			} else {
				$(this).css({backgroundColor:"transparent"});
			}
		}
	});

	if(error == false){
		var query =  $(ObjForm).serialize();
		$('#enviar').attr({disable:'disable'});
		$.post($(ObjForm).attr("action"),query,function(data){ $(idObject).html(data); }, 'html');
	}
	$(ObjForm).submit(function(){ return false; });
}

function CheckEmail(Value){
	var er = RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	return er.test(Value);
}
function CheckDDD(Value){
	var er = RegExp(/^[0-9]{2,}/);
	return er.test(Value);
}
function CheckTel(Value){
	var er = RegExp(/^[0-9]{4,4}([\-])?[0-9]{4,4}/);
	return er.test(Value);
}
function CheckCEP1(Value){
	var er = RegExp(/^[0-9]{5}/);
	return er.test(Value);
}
function CheckCEP2(Value){
	var er = RegExp(/^[0-9]{3}/);
	return er.test(Value);
}

$(document).ready(function(){
	$("#ContactForm input").live("keyup", function(){
												   var NumFinal = $(this).attr("maxlength");
												   //var ObjIndex = $('input').index(this);
												   var charc = $(this).val().length;
												   if(charc >= NumFinal){ $(this).next('input').focus();
												   }
												   });	
	$(".twochar").live("focus",function() {
                $(this).attr("value","");
        });
		
		
	$("#Map area").click(function(){
		var continuar = confirm("Você será enviado para um outro site deseja continuar?");		
		if (!continuar) {
			return false;
		}
	});
		
});


