/*GET QUERY ANCHORS*/
function getQueryAnchors(url){
	/*URL*/
	if(url == undefined){
		url = window.location.href;
	}
	
	/*VARS*/
	var anchors = url.split('#');
	if(anchors){
		return anchors[1];
	}
}

/*IMG COMPLETE*/
function imgComplete(img, callback){
	var intImgComplete = setInterval(function(){
		if($(img)[0].complete){
			/*CALLBACK*/
			if(callback != undefined) callback();
			
			/*CLEAR INTERVAL*/
			clearInterval(intImgComplete);
		}
	}, 100)
}

/*NEXT IMG*/
function nextImg(obj, imgs, imgView, callback){
	imgComplete($(obj).eq(imgView).attr('src', imgs[imgView]), function(){
		/*CALLBACK*/
		if(callback != undefined){
			callback(obj, imgView);
		}
		
		/*NEXT IMG*/
		if(imgView != imgs.length - 1){
			nextImg(obj, imgs, imgView + 1, callback);
		}
	})
}

/*MODAL*/
var urlModal;

function openModal(url, callback){
	$('button, input:button, a').blur();
	
	/*URL MODAL*/
	urlModal = url;
	
	$('.view_modal, .bg_modal, .load').remove();
	
	/*CREATE ELEMENTS*/
	$('body').prepend('<div class="bg_modal"></div><div class="view_modal"><img src="img/load.gif" width="88" height="78" alt="Carregando..." class="load" /></div>');
	
	/*OPACITY*/
	$('.bg_modal').css('opacity', 0);
	
	var arrayScrollTopMax = [$('html')[0].scrollTop, $('body')[0].scrollTop];
	var arrayScrollHeightMax = [$('html')[0].scrollHeight, $('body')[0].scrollHeight];
	
	var scrollTopMax = arrayScrollTopMax.sort()[1];
	var scrollHeightMax = arrayScrollHeightMax.sort()[1];
	
	$('.view_modal').css('top', scrollTopMax  + 'px');
	
	/*SELECTS HIDE IE6*/
	if($.browser.msie && $.browser.version <= 6){
		$('select').css('visibility', 'hidden');
	}

	/*SHOW BACKGROUND*/
	$('.bg_modal').height(scrollHeightMax + 'px');
	$('html').css('overflow-y', 'hidden');
	
	$('.bg_modal').fadeTo('normal', 1, function(){
		$('.view_modal').load(url, function(){
			/*CALLBACK*/
			if(callback != undefined){
				callback();
			}
			
			/*REMOVE LOAD*/
			$('.load').remove();
		
			/*MODAL HIDE*/
			$(this).fadeIn('show');
		
			/*CLOSE MODAL*/
			$("a[rel='close_modal']").click(function(){
				closeModal();
				return false;
			})
			
			/*GOOGLE ANALYTICS*/
			if(typeof(urchinTracker) == 'function'){
				urchinTracker(urlModal);
			}
			
			/*INIT*/
			if(typeof(initToModal) == 'function'){
				initToModal();
			}
		})
	})
}

/*CLOSE MODAL*/
function closeModal(callback){
	$('.bg_modal, .view_modal').fadeOut('hide', function(){
		$('.view_modal, .bg_modal').remove();

		/*SHOW SELECT IE6*/
		if($.browser.msie && $.browser.version <= 6){
			$('select').css('visibility', 'visible');
		}
		
		$('html').css('overflow-y', 'scroll');
		
		/*CALLBACK*/
		if(callback != undefined){
			callback();
		}
	})
}

/*INIT MODAL*/
function initModal(){
	$("a[rel='modal']").click(function(){
		$(this).blur();
		openModal($(this).attr('href').replace('#', ''));
		
		return false;
	})
}

/*INIT*/
function init(){
	/*INIT MODAL*/
	initModal();
	
	/*VARS*/
	var speed = 250;
	var itemHeightMin = '32px';
		
	$(document).click(function(){
		$('.header li').removeClass('selected');
		$('.header li a.drop').parent().animate({height: itemHeightMin}, speed);
	})
	
	$('.header li a.drop').click(function(){
		var item = $(this).parent();
		var list = $(this).parent().find('ul');
		var itemHeight = item.height() + 'px';
		var listHeight = list.height() + 45 + 'px';
		
		$('.header li a.drop').parent().animate({height: itemHeightMin}, speed);
		$('.header li').removeClass('selected');
		
		if(itemHeight == itemHeightMin){
			item.animate({height: listHeight}, speed);
			item.addClass('selected');
		}
		
		return false;
	})
	
	if(getQueryAnchors() != undefined && getQueryAnchors().indexOf('modal') != -1){
		openModal(getQueryAnchors());
	}
}

/*INIT TO MODAL*/
function initToModal(){
	if($('.view_modal .container_modal .content_modal .credit ul')){
		$('.view_modal .container_modal .content_modal .credit ul').css({width: $('.view_modal .container_modal .content_modal .credit ul').width() + 'px', float: 'none'});
	}
	
	carousel({obj: '.carousel_container .view_image.carousel', dimension: 'horizontal', scroll: 1, size: 1, animation: true, timer: false, loop: false, speed: 500});
	
	var arrayTotalImgs = [];
	var totalImgs = $('.carousel_container .view_image.carousel').parent().find('.carousel_nav li img');
	
	for(var i = 0; i < totalImgs.length; i++){
		arrayTotalImgs.push($('.view_modal .container_modal .content_modal .view_image ul li img').eq(i).attr('src'));
	}
	
	nextImg($('.view_modal .container_modal .content_modal .view_image ul li img'), arrayTotalImgs, 0, function(obj, imgView){
		obj.eq(imgView).show();
	})
	
	$('.carousel_container .view_image.carousel').parent().find('.carousel_nav li').eq(0).addClass('selected');
}

/*CAROUSEL*/
function carousel(objParam){
	/*VARS*/
	var obj = objParam.obj;
	var dimension = objParam.dimension;
	var scroll = objParam.scroll;
	var size = objParam.size;
	var animation = objParam.animation;
	var timer = objParam.timer;
	var loop = objParam.loop;
	var speed = objParam.speed;

	if($(obj).is(':visible')){
		/*ARRAY POS CAROUSEL*/
		var pos = [];
		
		/*VIEW ARRAY POS*/
		var view = 0;
		
		/*OBJ CAROUSEL*/
		var obj = $(obj);
		
		/*OBJ ADDCLASS DIMENSION*/
		obj.addClass(dimension);
		
		/*OBJCURRENT CAROUSEL*/
		var objCurrent = obj.find('.carousel_current');
		
		/*UL CAROUSEL*/
		var ul = objCurrent.find('ul:first');
		
		/*LI CAROUSEL*/
		var li = ul.find('li');
		
		/*NAV*/
		var objNav = obj.parent().find('.carousel_nav li');
		
		/*LI*/
		li.css({float: 'left'});
		var liTotal = li.length;
		var liWidth = li[0].offsetWidth;
		var liHeigth = li[0].offsetHeight;
		
		/*WIDTH | HEIGHT UL*/
		var ulWidth = liWidth * liTotal;
		var ulHeight = liHeigth * liTotal;
	
		/*SCROLL TOP | LEFT*/
		var scrollLeft = liWidth * scroll;
		var scrollTop = liHeigth * scroll;
	
		/*WIDTH | HEIGTH OBJ CURRENT*/
		var objCurrentWidth = liWidth * size;
		var objCurrentHeight = liHeigth * size;
		
		/*BT NEXT*/
		var next = obj.find('.bt_next, .bt_proximo');
		var prev = obj.find('.bt_prev, .bt_anterior');
		
		/*VERIFICATION QUANT*/
		var quant = Math.ceil(liTotal/scroll);
		
		if(quant <= 0) var quant = 2;
		
		/*HORIZONTAL*/
		if(dimension == 'horizontal'){
			objCurrent.css({width: objCurrentWidth + 'px', overflow: 'hidden'});
			ul.css({width: ulWidth + 'px', height: liHeigth + 'px', overflow: 'hidden'});
			
			for(var i = 0; i < quant; i++){
				pos.push(-scrollLeft * i);
			}
		}
		
		/*VERTICAL*/
		if(dimension == 'vertical'){
			objCurrent.css({height: objCurrentHeight + 'px', overflow: 'hidden'});
			ul.css({height: ulHeight + 'px'});
			li.css({clear: 'both'});
			
			for(var i = 0; i < quant; i++){
				pos.push(-scrollTop * i);
			}
		}
		
		/*DISABLE NEXT | DISABLE PREV*/
		if(liTotal == size){
			next.addClass('disable');
			prev.addClass('disable');
		}
		
		disableNav = function(){
			next.removeClass('disable');
			prev.removeClass('disable');
			
			if(view <= 0){
				prev.addClass('disable');
			}
			
			if(view >= pos.length - 1){
				next.addClass('disable');
			}
		}
		
		disableNav();
		
		/*NEXT*/
		next.click(function(){
			/*VERIFICATION*/
			if((scroll > size && view != pos.length - (-(pos.length - size))) || (scroll < size && view != pos.length - size) || (scroll == size && view != pos.length - 1)){
				view += 1;
				
				/*DIMENSION*/
				if(animation == undefined || animation == true || animation == 'true'){
					if(dimension == 'horizontal'){
						if(pos[view] != (objCurrentWidth - ulWidth)) ul.animate({marginLeft: pos[view]}, speed);
						else ul.animate({marginLeft: (objCurrentWidth - ulWidth)}, speed);
							
					}else{
						if(pos[view] != (objCurrentHeight - ulHeight)) ul.animate({marginTop: pos[view]}, speed);
						else ul.animate({marginTop: (objCurrentHeight - ulHeight)}, speed);
					}
					
				}else{
					if(dimension == 'horizontal'){
						if(pos[view] >= (objCurrentWidth - ulWidth)) ul.css({marginLeft: pos[view]}, speed);
						else ul.css({marginLeft: (objCurrentWidth - ulWidth)}, speed);
								
					}else{
						if(pos[view] >= (objCurrentHeight - ulHeight)) ul.css({marginTop: pos[view]}, speed);
						else ul.css({marginTop: (objCurrentHeight - ulHeight)}, speed);
					}
				}
				
			}else{
				if(timer != undefined && timer == true || loop != undefined && loop == true || loop == 'true'){
					clearInterval(intTimer);
					view = -1;
					next.click();
				}
			}
			
			disableNav();
			
			objNav.removeClass('selected');
			objNav.eq(view).addClass('selected');
			
			return false;
		})
		
		/*PREV*/
		prev.click(function(){
			/*VERIFICATION*/
			if(view != 0){
				view -= 1;
	
				/*DIMENSION*/
				if(animation == undefined || animation == true || animation == 'true'){
					if(dimension == 'horizontal') ul.animate({marginLeft: pos[view]}, speed);
					else ul.animate({marginTop: pos[view]}, speed);
				
				}else{
					if(dimension == 'horizontal') ul.css({marginLeft: pos[view]}, speed);
					else ul.css({marginTop: pos[view]}, speed);
				}
			}else{
				if(loop != undefined && loop == true || loop == 'true'){
					if(scroll < size) view = pos.length - (size - 1);
					else view = pos.length;
	
					prev.click();
					
				}
			}
			
			disableNav();
			
			objNav.removeClass('selected');
			objNav.eq(view).addClass('selected');
						
			return false;
		})
		
		/*AUTO CAROUSEL*/
		if(!isNaN(Number(timer)) && timer != undefined && timer != false && timer != 'false' || typeof(timer) == 'number' && timer != undefined && timer != false && timer != 'false'){
			var intTimer;
			
			function timerCarosel(){
				intTimer = setInterval(function(){
					next.click();
				}, timer);
			}
			
			function clearTimerCarousel(){
				clearInterval(intTimer);
				timerCarosel();
			}
	
			timerCarosel();
			
			/*CLICK */
			next.click(function(){
				clearTimerCarousel();
			})
			
			prev.click(function(){
				clearTimerCarousel();
			})
		}
		
		/*CAROUSEL NAV*/
		objNav.find('a').click(function(){
			var objNavView = $(this).parent();
			objNav.removeClass('selected');	
			objNavView.addClass('selected');
			
			for(var i = 0; i < objNav.length; i++){
				if(objNav.eq(i).attr('class') == 'selected'){
					view = i;
					ul.animate({marginLeft: pos[view]}, speed);
				}
			}
			
			disableNav();
			
			return false;
		})
	}
}

/*ONLOAD*/
$(function(){
	init();
})