$(function(){
	// page elements
	$bodydetail			= $('#body-detail');
	$contentcontainer 	= $('#content-container');
	$pagecontainer 		= $('#page-container');

	
	var bms = {};
	bms.slider = {}
	bms.slider.delay = 5 * 1000;
	
	
	// get url from 'url(...)' css value
	//TODO:
	function getcssurl(str) {
		var urlexp 	= /^url\((.*)\)$/gi;
		var arrurl 	= urlexp.exec(str);urlexp.exec(str);
		if (typeof arrurl == 'object')
			if (arrurl !== null) //¬¬
				var url	= arrurl[1];
		return url || str;
	}
	
	
	// countdown
	var $countdown = $('#date-header .countdown');
	var total = $countdown.attr('total');

	if (total) {
		
		var $s = $('.seconds', $countdown);
		var $ss = $('span', $s);
		var $m = $('.minutes', $countdown);
		var $mm = $('span', $m);
		var $h = $('.hours', $countdown);
		var $hh = $('span', $h);
		var $d = $('.days', $countdown);
		var $dd = $('span', $d);
		
		if (typeof bms_json == 'object') {
			var json = bms_json;
		} else {
			var json = [];
			json['going'] = 'Est&aacute; rolando!';
		}
		
		var done 	= [];
		done['d'] 	= $d.is('.zero');
		done['h'] 	= $h.is('.zero');
		done['m'] 	= $m.is('.zero');
		done['s'] 	= $s.is('.zero');
		function countdown () {
			
			var s = parseInt($ss.html());
			
			if (s > 0) {
				s--;
			} else if (!done['m']){
				var m = parseInt($mm.html());
				if (m > 0) {
					m--;
					
					if (done['h']) {
						$m.addClass('zero');
						done['m'] = true;
					}
					
				} else if (!done['h']) {
					var h = parseInt($hh.html());
					if (h > 0) {
						h--;
					
					if (done['d']) {
						$h.addClass('zero');
						done['h'] = true;
					}
					
					} else if (!done['d']) {
						var d = parseInt($dd.html());
						d--;
						if (d >= 0) {
							$dd.html((d>0)?d:'0');
							if (typeof json['days'] != 'undefined') {
								var dhtml = ($d.html().toString()).replace(json['days']['days'],json['days']['day']);
								if (d === 1)
									$d.html( dhtml.replace(json['days']['days'],json['days']['day']) );
								else 
									$d.html( dhtml.replace(json['days']['day'],json['days']['days']) );
							}
						}
						if (d <= 0) {
							$d.addClass('zero');
							done['d'] = true;
							$dd.html(0);
						}
						var h = 24;
					}
					
					$hh.html((h>0)?h:'0');
					var m = 59;
				}
				
				$mm.html((m>0)?m:'0');
				var s = 59;
			} else { // m = 0 and s = 0
				// COUNT FINISHED!!!
				$s.addClass('zero');
				done['s'] = true;
				clearInterval($countdown.interval);
				$countdown.html('<span>'+json['going']+'</span>');
				$countdown.addClass('going');
				
				return;
			}
			$ss.html((s>0)?s:'0');
			
			total--;
			$countdown.attr('total', total);
			if (total < 0) clearInterval($countdown.interval);
		}
		
		
		
		countdown();
		$countdown.interval = setInterval(countdown, 1000);

	}


	if (typeof bgphoto == 'object') {
		
		var $bgphoto = $(bgphoto.html);
		$bgphoto.timeoutcount = 0;
		$contentcontainer.prepend( 
			$bgphoto
				.hide()
				.mouseover(function(){
					if ($bgphoto.timeoutcount > 2) {
						$bgphoto.timeout = setTimeout(function(){
							$pagecontainer.css({opacity: 0.001});
							$bgphoto.timeoutms = 300;
						}, $bgphoto.timeoutms || 0 );
					} else {
						$pagecontainer.css({opacity: 0.001});
						$bgphoto.timeoutcount++;
					}
				})
				.mouseout(function(){
					clearTimeout($bgphoto.timeout);
					$pagecontainer.css({opacity: 1});
				})
				.tooltip({
					track: true,
					delay: 0,
					showURL: false,
					opacity: 1,
					fixPNG: true,
					showBody: " - ",
					extraClass: "bgphoto",
					top: 13,
					left: -15
				})
				.click(function(){
					if ($(this).attr('href') != '#')
						return false;
				})
		);
		
		var urlexp = /^url\((.*)\)$/gi;
		//var urlexp = /.*/gi
		
		var arrbgurl 	= urlexp.exec($bodydetail.css('background-image'));
		var bgurl		= arrbgurl[1];
		
		var $img		= $(new Image());
		$img
			.load(function(){
				// bgphoto loaded
				$bgphoto.fadeIn('slow');
			})
			.attr('src', bgurl);
				
	}

	//TODO: front slider
	var slider_wait = 0; // don't slide if mouse is over $slider
	var $slider = $('#slider')
		.mouseover(function(){	slider_wait = 1; })
		.mouseout(function(){ 	slider_wait = 0; });
	
	var $slides = $('.slide', $slider);
	$($slides[0]).addClass('first');				// duh!
	$($slides[$slides.length-1]).addClass('last');	// duh!
	if ($slides.length > 1)
		sliderenqueue();
		
	function sliderenqueue() {
			$slides.each(function(n,slide){
				var $slide = $(slide);
				if ($slide.is(':visible')) {
					$last = '';
					 if ($slide.is('.last')) {
						$next = $($slides[0]);
						$last = $slide;
					}
					else
						$next = $slide.next();
					
					clearTimeout($slider.timeout);
					$slider.timeout = setTimeout(function(){
						slidernext($next,$last);
					}, bms.slider.delay || 5000);
					
					return;
				}
			});
	}
	function slidernext($next,$last) {
		var img 	= new Image();
		var bgurl	= $next.css('background-image');
		var url 	= getcssurl(bgurl);
		var $img	= $(new Image());
		
		if (url !== bgurl)
		$img
			.load(function(){
				var wait = 0;
				if (slider_wait == 1)
					wait = 1;
				else
					if ($('#jquery-lightbox').is(':visible'))
						wait = 1;
				if (!wait)
					sliderchangeto($next,$last);
				else {
					clearTimeout($slider.timeout);
					$slider.timeout = setTimeout(function(){
						slidernext($next,$last);
					}, bms.slider.delay || 5000);
				}
			})
			.attr('src', url);
	}
	function sliderchangeto($slide,$last) {
		clearTimeout($slider.timeout);
		$visibles = $('.slide:visible', $slider);
		$current = $($visibles[0]);
	
		//$clone = $slide.clone();
		//$slide.remove();
	
		if (!$last)
		$slide
			//.insertAfter($current)
			.fadeIn('slow', function(){
				sliderenqueue();
				$visibles.hide();
			});
		else {
			$slide.show()
			$last
				.fadeOut('slow', function(){
					sliderenqueue();
				});
		}

		//$current
			//.css({'z-index':2})
			//.css({'opacity':0.5});
			//.fadeOut('slow');

	}
	
	
	/// slider lightbox
	$sliderlinks = $('#slider.box .content .slide a');
	if ($sliderlinks.length) {
		$sliderlinks.lightBox({	
			overlayBgColor: '#000'
			, overlayOpacity: 0.8
			, imageLoading: 'http://example.com/images/loading.gif'
			, imageBtnClose: 'http://example.com/images/close.gif'
			, imageBtnPrev: 'http://example.com/images/prev.gif'
			, imageBtnNext: 'http://example.com/images/next.gif'
			, containerResizeSpeed: 350
			, txtImage: 'Imagem'
			, txtOf: 'de'
		});
	}
	
	// main-nav
	$('.page-item-111').removeClass('dropdown');
	$('#main-nav .dropdown').each(function(n,o){
		var $o		= $(o);
		var $sub 	= $($('ul',$o)[0]);
		var $a		= $($('a', $o)[0]);
		var $li		= $a.parent();
		
		$li.removeClass('page_item');
		
		$a
			.mouseover(function(){
				clearTimeout($o.timeout);
				$sub
					.show()
					.css({
						position:'absolute'
						,display:'block'
						,'z-index':'1000'
					});
			})
			.mouseout(function(){
				clearTimeout($o.timeout);
				$o.timeout = setTimeout(function(){$sub.hide();},500);
			});
			
		$o
			.mouseover(function(){
				clearTimeout($o.timeout);
			})
			.mouseout(function(){
				clearTimeout($o.timeout);
				$o.timeout = setTimeout(function(){$sub.hide();},500);
			})
	});
	
	
});
