function MediaBox($)
{
	$.fn.MediaBox = function (options)
	{
		var defaults = {
			liItems : "li",
			listCont : "#mediaList",
			showCont : "#DestMedia",
			imgMask : ".imgThumb",
			animVel: 100,
			autoStart: true,
			intervalItem: 5
		}
	$.fn.MediaBox.options = $.extend(defaults, options);
	$.fn.MediaBox.changeItem = function ($mediaShow, item){
		$mediaShow.animate({"opacity": 0}, $.fn.MediaBox.options.animVel, function (){
			$mediaShow.html(item);
			$mediaShow.animate({"opacity": 1}, $.fn.MediaBox.options.animVel);
		})
	}
	return this.each(function (){
		var $this = $(this);
		
		var $mediaShow = $($.fn.MediaBox.options.showCont, $this);
		var $listItens = $($.fn.MediaBox.options.listCont, $this);
		var $itens = $($.fn.MediaBox.options.liItems, $listItens);
		
		$itens.click(function (){
			$itens.removeClass("selected");
			$(this).addClass("selected");
			$.fn.MediaBox.changeItem( $mediaShow ,  $(this).html() );
			return false;
		})
		var interval = "";
		if($.fn.MediaBox.options.autoStart == true)
		{
			interval = setInterval(function (){
				$nextClick = $("li.selected", $listItens).next();
				if($nextClick[0]){
					$nextClick.click();
				} else {
					$($.fn.MediaBox.options.liItems+":first", $listItens).click();
				}
			}, ($.fn.MediaBox.options.intervalItem*1000));
		}
		$($.fn.MediaBox.options.liItems+":first", $listItens).click();
	})
	}
}
MediaBox(jQuery);
function AjaxedForms($)
{
	$.fn.AjaxedForms = function (options){
		
		$.fn.AjaxedForms.determineDataType = function (action)
		{
			var regExp = new RegExp("([a-z0-9]+)$", "i");
			
			if(regExp.exec(action) != null)
			{
				var format = regExp.exec(action)[1];
				format = format.toLowerCase();
				
				switch(format)
				{
					case "html":
						return "html";
					break;
					case "xml":
						return "xml";
					break;
					case "json":
						return "json";
					break;
					case "js":
						return "script";
					break;
				}
			}
			return "html";
		}
		
		return this.each(function (){
			$(this).submit(function (){
				var method = String($(this).attr("method"));
				method = method.toUpperCase();
				var urlTarget = $(this).attr("action");
				var Type = $.fn.AjaxedForms.determineDataType(urlTarget);
				var dados = $(this).serialize();
				var form = $(this);
				$.ajax({
					url: urlTarget,
					dataType: Type,
					type: method,
					data: dados,
					success: function (data){
						form.parent().html(data);
					}
				})
				return false;
			})
		});
	}
}
AjaxedForms(jQuery);
// Accordion
function Accordion($){
	$.fn.Accordion = function (options)
	{
		var defaults = {
			tith4 : "h4.openClose",
			titSelector : "h4.openClose a",
			parentClass : "div",
			divClass : "div.openCloseDiv",
			animationTime: "fast"
 		}
		$.fn.Accordion.options = $.extend(defaults, options);
		return this.each(function (){
			$this = $(this);
			var h4s = $($.fn.Accordion.options.tith4, $this);
			var divs = $($.fn.Accordion.options.divClass, $this);
			$($.fn.Accordion.options.titSelector, $this).click(function (){
				var h4 = $(this).parent();
				h4s.removeClass("open");
				h4.addClass("open")
				divs.hide($.fn.Accordion.options.animationTime);
				h4.next($.fn.Accordion.options.divClass).show($.fn.Accordion.options.animationTime);
				return false;
			})
			$($.fn.Accordion.options.titSelector+":first", $this).click();
		})
	}
}
Accordion(jQuery);
function Tabs($)
{
	$.fn.Tabs = function ( options )
	{
		var defaults = {
			TabsCont : "ul.tabs",
			tabedCont : ".tabsContent",
			selectedClass : "selected"
		}
		$.fn.Tabs.options = $.extend(defaults, options);
		
		return this.each(function (){
			$this = $(this);
			var $TabsCont = $($.fn.Tabs.options.TabsCont, $this);
			var $Cont = $($.fn.Tabs.options.tabedCont, $this);
			
			$("li a", $TabsCont).click(function (){
				var target = $(this).attr("href");
				$("li", $TabsCont).removeClass($.fn.Tabs.options.selectedClass);
				$(this).parents("li").addClass($.fn.Tabs.options.selectedClass);
				$(".tab", $Cont).hide();
				$(target, $Cont).show();
				return false;
			})
			$("li a:first", $TabsCont).click();
		})
	}
}
Tabs(jQuery);
function AjaxedCity($)
{
	$.fn.AjaxedCity  = function (options)
	{
		var defaults = {
			url : "/cities/index.html?uf=%s%",
			target : ""
		}
		$.fn.AjaxedCity.Options = $.extend(defaults, options);
		
		return this.each(function (){
			$this = $(this);
			
			var $uf = $($.fn.AjaxedCity.Options.target)
			$uf.change(function (){
				
				var uf = $(this).val();
				var str = String($.fn.AjaxedCity.Options.url);
				str = str.replace("%s%", uf);

				$this.load(str);
			})
			
		})
	}
}
AjaxedCity(jQuery);
function SliderGal($)
{
	$.fn.SliderGal = function (options)	
	{
		var defaults = {
			sliderMask : "div.photoSlider",
			slider : "ul.slider",
			sliderItens: "li",
			sliderItemBtn: "a",
			photoFull: "div.photoFull",
			prevBtn: "a#prevPhotos",
			nextBtn: "a#nextPhotos"
		}
		$.fn.SliderGal.Options = $.extend(defaults, options);
		$.fn.SliderGal.gotoPos = function ($slider, stage, widt)
		{
			var wdt = (((stage*widt)*(-1))+20);
			$slider.animate({left: wdt+"px"}, 500);
		}
		return this.each(function (){
			var $this = $(this);
			// Elements
			var $mask = $($.fn.SliderGal.Options.sliderMask, $this);
			var $slider = $($.fn.SliderGal.Options.slider, $this);
			var $sliderItens = $($.fn.SliderGal.Options.sliderItens, $slider);
			var $sliderItemBtn = $($.fn.SliderGal.Options.sliderItemBtn, $sliderItens);
			var $photoFull = $($.fn.SliderGal.Options.photoFull, $this);
			// Buttons
			var $prevBtn = $($.fn.SliderGal.Options.prevBtn, $this);
			var $nextBtn = $($.fn.SliderGal.Options.nextBtn, $this);
			
			// Adjust Slider Width
			var itensWidth = $sliderItens.outerWidth({includeMargin: true});
			var itensCount = $sliderItens.length;
			if(itensCount == 0){
				$this.css("display", "none");
			}
			
			var totalWdt = itensWidth *  itensCount;
			$slider.css("width", totalWdt+"px");
			
			if($mask.width() > totalWdt)
			{
				$prevBtn.css("display", "none");
				$nextBtn.css("display", "none");
			} else {
				var maxDif = Math.ceil(totalWdt / $mask.width());
				
				$prevBtn.click(function (){
					var leftPos = $slider.css("left");
					leftPos = Number(leftPos.replace("px", ""));
					if(leftPos < 0)
					{
						leftPos = leftPos * (-1);
					}
					var currentDif = Math.ceil((leftPos - 20) / $mask.width());
					if(currentDif-1 >= 0)
					{
						currentDif--;
						$.fn.SliderGal.gotoPos($slider, currentDif, $mask.width());
					}
					return false;
				})
				$nextBtn.click(function (){
					var leftPos = $slider.css("left");
					leftPos = Number(leftPos.replace("px", ""));
					var currentDif = Math.ceil((leftPos - 20) / $mask.width());
					if(maxDif >= currentDif+1)
					{
						currentDif++;
						$.fn.SliderGal.gotoPos($slider, currentDif, $mask.width());
					}
					return false;
				})
				
			}
			
			$sliderItemBtn.click(function (){
				var urlImg = $(this).attr("href");
				var desc = $(this).attr("title");
				
				var html = "<img src=\"/img/blank.gif\" style=\"background: url("+urlImg+") no-repeat center center;\" /><p> Descrição: "+desc+"</p>";
				$photoFull.html(html);
				return false;
			})
			$($.fn.SliderGal.Options.sliderItemBtn+":first", $sliderItens).click();
		})
	}
}
SliderGal(jQuery)
function PopUpContainner($)
{
	$.fn.PopUpContainner = function (options)	
	{
		var defaults = {
			
		}
		$.fn.SliderGal.Options = $.extend(defaults, options);
		return this.each(function (){
			$this = $(this);
			var contID = $this.attr("rel");
			var $pop = $("#"+contID);
			$this.click(function (){
				$pop.css("display", "block");
				return false;
			})
			// Btn Close 
			$(".btnClose", $pop).click(function (){
				$pop.css("display", "none");
				return false;
			})
			
		})
	}
}
PopUpContainner(jQuery);
$(document).ready(function (){
	$(".inputLabelInside").each(function (){
		$(this).focus(function (){
			var id = $(this).attr("id");
			var label = $("label[for="+id+"]").html();
			if($(this).val() == label)
			{
				$(this).val("");
			}
		})
		$(this).blur(function (){
			var id = $(this).attr("id");
			var label = $("label[for="+id+"]").html();
			if($(this).val() == "")
			{
				$(this).val(label);
			}
		})
	})
	$("form.ajaxed").AjaxedForms();
	
	//  Menu 
	$('li','#menu-top').hover(function(){
		$('a',this).toggleClass('selected').next().slideToggle(200);
	});
	//  Menu Areas 
	$('li a','#menu-BF').hover(
		function(){
			$(this).stop().fadeTo(150,0.5);
		},
		function(){
			$(this).stop().fadeTo(150,1);
		}
	);
	
	// Page Background 
	$(window).resize(function (){
		if($("body").innerWidth() > 1100)
		{
			$("body").addClass("wdt1280");
		} else if($("body").hasClass("wdt1280")){
			$("body").removeClass("wdt1280");
		}
	})
	if($("body").innerWidth() > 1100)
	{
		$("body").addClass("wdt1280");
	}
	// Mediabox
	var $mediaBox = $("#mediabox");
	if($mediaBox[0])
	{
		$mediaBox.MediaBox();
	}
	var $results = $("div.resultsExpo");
	if($results[0]){
		$results.Accordion();
	}
	var $tabs = $(".tabed");
	if($tabs[0])
	{
		$tabs.Tabs();
	}
	if($("#address-city")[0])
	{
		$("#address-city").AjaxedCity({target: "#address-uf"});
	}
	if($("#filters-city")[0])
	{
		$("#filters-city").AjaxedCity({target: "#filters-uf"});
	}
	var infos = $("a.informacoes");
	if(infos[0])
	{
		infos.click(function(){
			var $li = $(this).parents("li");
			$(".lista-informacoes", $li).css("display", "block");
			return false;
		})
	}
	var jumpList = $("#jumpGalList");
	if(jumpList[0])
	{
		jumpList.change(function (){
			var data = $(this).val();
			if(data != "")
			{
				var str = "/%/galerias.html";
				str = str.replace("%", data);
				document.location = str;
			}
		})
	}
	var jumpList2 = $("#jumpGalList2");
	if(jumpList2[0])
	{
		jumpList2.change(function (){
			var channel = $("#jumpGalList").val();
			var data = $(this).val();
			if(data != "")
			{
				var str = "/"+channel+"/galeria/"+data+".html";
				document.location = str;
			}
		})
	}
	var allPhotos = $(".allPhoto");
	if(allPhotos[0])
	{
		allPhotos.SliderGal();
	}
	//  Aumentar e diminuir Fontes
	var $fontPlus = $("#FontGreater");
	var $fontMinor = $("#FontMinor");
	$fontPlus.click(function (){
		var fontSize = $("#internal-news").css("font-size");
		fontSize = Number(fontSize.replace("px", ""));
		fontSize++;
		$("#internal-news").css("font-size", fontSize+"px");
		return false;
	})
	$fontMinor.click(function (){
		var fontSize = $("#internal-news").css("font-size");
		fontSize = Number(fontSize.replace("px", ""));
		fontSize--;
		$("#internal-news").css("font-size", fontSize+"px");
		return false;
	})
	
	// Custom Forms with Ajax
	$("#FormSendFriend").AjaxedForms();
	$("#FormSendError").AjaxedForms();
	
	// Pop-up Trigger
	 $(".popTrigger").PopUpContainner()
	
})
