// JavaScript Document
$(function() {
	jQuery.jQueryRandom = 0;
	jQuery.extend(jQuery.expr[":"],
	{
		random: function(a, i, m, r) {
			if (i == 0) {
				jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
			};
			return i == jQuery.jQueryRandom;
		}
	});
	// control the submenu
	
	
	// check if is home, sets and initiates home exclusive variables and functions
	if ($('body.home').length) {		   
		// functions to rezise and change home background image
		// declare variable for background source
		var backgroundSource = $("#bg-list img:random").attr('src').replace("-20x20", "");
		//function to resize background (uses JP Given'' jQuery Easy Background Resizeplugin at http://johnpatrickgiven.com/jquery/background-resize/
		var backgroundResize = function(){
			$("body").ezBgResize({
				img : backgroundSource, // Relative path example.  You could also use an absolute url (http://...).
				opacity : 1, // Opacity. 1 = 100%.  This is optional.
				center  : false // Boolean (true or false). This is optional. Default is true.
			});
		};
		// call the backgroundResize function for the first time
		backgroundResize();
		//function to change background - calls backgroundResize at the end
		var changeBackground = function (){
			backgroundSource = $(this).attr('src').replace("-20x20", "");
			backgroundResize();
		};
		// binds the function to the switchers
		$("#bg-list img").live("click",changeBackground);
		
	};
	
	// controls the hover effect on images thumbs on category archives
	
	function pageHighlightHoverOn() {
		$(this).find('span').css('background-color','#22424e');
	};
	function pageHighlightHoverOff() {
		$(this).find('span').css('background-color','transparent');
	};
		//if ($('body.category').length){
		$(".page-highlights-item").hover(pageHighlightHoverOn,pageHighlightHoverOff);
		//};
	//sets  and initiantes all gallery vars and function if in a Gallery Page
	function checkGallery() {
		// check if is a gallery page
		galleryEligible = $('.single-gallery').size();
		if (galleryEligible != 0){
			//init main scrollable
			//var galeria = $("#galeria").scrollable({easing:"linear", keyboard:false, disabledClass: "invisivel", api:true});
			
			// active gallery and thumbs indexes
			var itemIndex = $('#gallery .gallery-item').filter(":first").attr('id').replace("gallery-item-", "");
			var thumbIndex = $('#gallery .gallery-item').filter(":first").attr('id').replace("gallery-thumbs-item-", "");
			var itemLenght = $('#gallery .gallery-item').size();
			var maxItemLenght = itemLenght - 1;
			var imagemAtualIndex = itemIndex;
			var imagemAtualNumero = imagemAtualIndex + 1;
			var imagemProximaIndex = imagemAtualIndex + 1;
			var imagemAnteriorIndex = imagemAtualIndex - 1;
			var imagemNovaIndex = imagemAtualIndex;
			var	imagemAtualId = '#gallery-item-'+imagemAtualIndex+'';
			var	imagemNovalId = '#gallery-item-'+imagemProximaIndex+'';
			var	thumbAtualId = '#gallery-thumbs-item-'+imagemAtualIndex+'';
			var	thumbNovalId = '#gallery-thumbs-item-'+imagemProximaIndex+'';
			// check the number of itens on Thumbs list 
			var thumbsLength = $('#gallery-thumbs-images li').length;
			var thumbsMaxOffset = (thumbsLength * 60) - 900;
			var thumbsOffset = 0;
			var imageLabel = $('#gallery .gallery-item').filter(':first').find('img').attr('title');
			$('#image-label').text(imageLabel);
			$('#total-images').text(itemLenght);
			
			// fade all gallery items to zero (invisible)
			$('#gallery .gallery-item').fadeTo(0,0);
			$('#gallery .gallery-item').css('position','absolute');	
			// fade the first image to 1 (visible) again
			$('#gallery .gallery-item').filter(':first').fadeTo(1000,1);
			// function to move to new image
			function changeImage (imagemAtualIndex, imagemNovaIndex){
				if (imagemNovaIndex == 0 && imagemAtualIndex == 0) {
					imagemAtualId = '#gallery-item-'+imagemAtualIndex+'';
					thumbAtualId = '#gallery-thumbs-item-'+imagemAtualIndex+'';
					$('#gallery .gallery-item').css('display','none');
					$(imagemAtualId).css('display','block');
				}
				else {
					imagemAtualId = '#gallery-item-'+imagemAtualIndex+'';
					imagemAtualNumero = parseInt(imagemNovaIndex) + 1;
					imagemNovaId = '#gallery-item-'+imagemNovaIndex+'';
					thumbAtualId = '#gallery-thumbs-item-'+imagemAtualIndex+'';
					thumbNovalId = '#gallery-thumbs-item-'+imagemProximaIndex+'';
					imageLabel = $(imagemNovaId).find('img').attr('title');
					$(imagemAtualId).fadeTo(500,0,function() {
														   // don't forget to change the display using a callback
														   // or else the link anchor and other elements off the last image will be on
														   // on top of the previous imagens even when it's not show
														   // and if you don't use as callback, the transition animation will not be smooth
      $(imagemAtualId).css('display','none');
    });
					$(imagemNovaId).fadeTo(1000,1,function() {
      $(imagemNovaId).css('display','block');
    });
					$('#active-image').text(imagemAtualNumero);
					$('#image-label').text(imageLabel);
					if (imagemNovaIndex > 0) {
						$(".anterior").removeClass("invisivel");
					};
					if (imagemNovaIndex == maxItemLenght) {
						$(".proximo").addClass("invisivel");
					};
					if (imagemNovaIndex == 0) {
						$(".anterior").addClass("invisivel");
					};
					if (imagemNovaIndex != maxItemLenght) {
						$(".proximo").removeClass("invisivel");
					};
				};
			};
			changeImage(imagemAtualIndex, imagemNovaIndex);
			// function to switch active image
			var activeImageSwitch =  function (imagemAtualIndex, imagemNovaIndex) {
				if ($(this).hasClass("active")) {
					thumbIndex = $(this).parent().attr('id').replace("gallery-thumbs-item-", "");
					imagemAtualIndex = thumbIndex;
					imagemProximaIndex = imagemAtualIndex + 1;
					imagemAnteriorIndex = imagemAtualIndex - 1;
				}
				else {
					imagemAtualIndex = $('img.active').parent().attr('id').replace("gallery-thumbs-item-", "");
					thumbIndex = $(this).parent().attr('id').replace("gallery-thumbs-item-", "");
					imagemNovaIndex = thumbIndex;
					changeImage(imagemAtualIndex, imagemNovaIndex);
					itemIndex = parseInt(thumbIndex);
					$("img.active").css('border','1px solid #000');
					$("img.active").removeClass('active');
					$(this).addClass("active");
					$(this).css('border','1px solid #0099ff');
					imagemAtualIndex = thumbIndex;
				};
			};
			// call the activeImageSwitch function
			$("#gallery-thumbs-images img").live("click",activeImageSwitch);

			// functions to control next and previous button and indexes on gallery
			
			//function to control next image
			var imagemProxima = function () {
				imagemAtualIndex = itemIndex;
				$("img.active").css('border','1px solid #000');
				$("img.active").removeClass('active');
				imagemNovaIndex = parseInt(imagemAtualIndex + 1);
				$('#gallery-thumbs-item-' + imagemNovaIndex + ' img').addClass('active');
				$('#gallery-thumbs-item-' + imagemNovaIndex + ' img').css('border','1px solid #0099ff');
				changeImage(imagemAtualIndex, imagemNovaIndex);
				itemIndex = imagemNovaIndex;
			};
			//function to control previous image
			var imagemAnterior = function () {
				imagemAtualIndex = itemIndex;
				$("img.active").css('border','1px solid #000');
				$("img.active").removeClass('active');
				imagemNovaIndex = parseInt(imagemAtualIndex - 1);
				$('#gallery-thumbs-item-' + imagemNovaIndex + ' img').addClass('active');
				$('#gallery-thumbs-item-' + imagemNovaIndex + ' img').css('border','1px solid #0099ff');
				changeImage(imagemAtualIndex, imagemNovaIndex);
				itemIndex = imagemNovaIndex;
			};

			// call functions to control next and previous button and indexes on gallery
			$('.proximo').live("click",imagemProxima);
			$('.anterior').live("click",imagemAnterior);
			
			// functions to controls Thumbs behavior
			
			if (thumbsLength > 15) {
				$('.thumbs-proximo').removeClass('invisivel');
			}
			else {
				$('#gallery-thumbs-images').css('display','table');
				$('#gallery-thumbs-images').css('width','auto');
				$('#gallery-thumbs-images').css('margin','0 auto');
				
			};
			// assigns .active class and white border to first thumbnail on page load
			$(thumbAtualId).children('img').css('border','1px solid #0099ff');
			$(thumbAtualId).children('img').addClass('active');
			
			// move the thumbs left or right
			
			//function to control next thumb scroll
			var thumbsProxima = function () {
				if (thumbsOffset < thumbsMaxOffset){
					thumbsOffset = thumbsOffset + 300;
					thumbsLeft = '-' + String(thumbsOffset);
					$('#gallery-thumbs-images').animate({marginLeft:thumbsLeft});
					if (thumbsOffset == thumbsMaxOffset) {
						$('.thumbs-proximo').addClass('invisivel');
					}
					if (thumbsOffset > 0) {
						$('.thumbs-anterior').removeClass('invisivel');
					}
				};
			};
			//function to control previous thumb scroll
			var thumbsAnterior = function () {
				if (thumbsOffset > 0){
					thumbsOffset = thumbsOffset - 300;
					thumbsLeft = '-' + String(thumbsOffset);
					$('#gallery-thumbs-images').animate({marginLeft:thumbsLeft});
					if (thumbsOffset < thumbsMaxOffset) {
						$('.thumbs-proximo').removeClass('invisivel');
					}
					if (thumbsOffset == 0) {
						$('.thumbs-anterior').addClass('invisivel');
					}
				};
			};

			// call functions to control next and previous button and indexes on thumbs
			$('.thumbs-proximo').live("click",thumbsProxima);
			$('.thumbs-anterior').live("click",thumbsAnterior);
			

			//menu on/off switcher on Gallery pages 
			
			// set attributes according to switch state
			var menuSwitch = function() {
				switchState = $('.switch-on').size();
				if ( switchState == 0) {
					$('#menu-switcher').animate(
						{height:0},300, function(){
							$('#menu-switcher').css('visibility','hidden');
							$('#menu-switcher').addClass('switch-on');
							$('#menu-switcher').removeClass('switch-off');
							$('#menu-switcher').css('left','-52px');
							$('#menu-switcher').css('top','568px');
							$('#menu-switcher').css('height','30px');
							$('#menu-switcher').css('visibility','visible');
							$('#main-menu').animate(
								{left:0, top:62, height:'toggle'}, 700
							);
						}
					);
					$('#main-menu a').fadeTo('slow',1);
					$('header').animate(
						{height:'toggle'}, 700, function(){
							$('.contact').fadeTo('fast',1);
							$('h1').fadeTo('fast',1);
						}
					);
				}
				else {
					$('.contact').fadeTo('fast',0);	
					$('h1').fadeTo('fast',0);										  
					$('#main-menu a').fadeTo('slow',0);
					$('#menu-switcher').css('visibility','hidden');
					$('#main-menu').animate(
						{left:0,top:0,height:'toggle'}, 700, function(){
							$('#menu-switcher').addClass('switch-off');
							$('#menu-switcher').removeClass('switch-on');
							$('#menu-switcher').css('left','30px');
							$('#menu-switcher').css('top','0');
							$('#menu-switcher').css('height','0');
							$('#menu-switcher').css('width','45px');
							$('#menu-switcher').css('background','#22424d url(http://cassiovasconcellos.com.br/en/wp-content/themes/cassiovasconcellos/img/bg-menu-textura-03.png) repeat left top');
							$('#menu-switcher').css('visibility','visible');
							$('#menu-switcher').animate(
								{ height:30}, 300
							);
						}
					);
					$('header').animate(
						{height:'toggle'}, 700
					);
				};
			};
			// call the switcher function
			$("#menu-switcher").live("click",menuSwitch);

			// set attributes according to gallery thumbs handler state
			var thumbsSwitch = function() {
				switchState = $('.thumbs-on').size();
				if ( switchState == 0) {
					$('#gallery-thumbs').animate(
						{bottom:0}, 1000, function() {
							$('#gallery-thumbs-handler').addClass('thumbs-on');
							$('#gallery-thumbs-handler').removeClass('thumbs-off');
							$('#gallery-thumbs-handler').css('background-position','center -84px');
						}
					);
				}
				else {
					$('#gallery-thumbs').animate(
						{bottom:-90}, 1000, function() {
							$('#gallery-thumbs-handler').addClass('thumbs-off');
							$('#gallery-thumbs-handler').removeClass('thumbs-on');
							$('#gallery-thumbs-handler').css('background-position','center -5px');
						}
					);
				};
			};
		// function to check if there are any of the gallery images avaiable for sell and put a seal informing that
		var fotospotAvailable = function (){
			var fotospotAvaiableURL = '';
			var fotospotDestinationURL = '';
			var fotospotAvaiableTXT = '';
			var arrayLenght = $('.fotospot-list ul li').length;
			$('.fotospot-list ul li').each(function(index) {
				fotospotAvaiableURL = $(this).find('.fotospot-url-gallery').text();
				fotospotDestinationURL = $(this).find('.fotospot-url-destination').text();
				fotospotAvaiableURL = 'img[src="'+ fotospotAvaiableURL + '"]';
				fotospotAvaiableTXT = "<span class='fotospot-seal'><a href='" + fotospotDestinationURL + "' target='_blank' title=' Foto dispon&iacute;vel para fenda na Fotospot (abre em nova janela)'>Dispon&iacute;vel para venda na Fotospot<img src='http://cassiovasconcellos.com.br/wp-content/themes/cassiovasconcellos/img/logo-foto-spot-17px.png' /></a></span>";
				$(fotospotAvaiableURL).parent().append(fotospotAvaiableTXT);
			});
		};
		fotospotAvailable();
			// call the switcher function
			$("#gallery-thumbs-handler").live("click",thumbsSwitch);
			var t;
			$(document).delay(1300).queue(function(){ 
				menuSwitch();
				$(this).dequeue(); 
			});
			$(document).delay(2700).queue(function(){ 
				switchState = $('.thumbs-on').size();
				if ( switchState != 0) {
					thumbsSwitch();
				};
				$(this).dequeue(); 
			});
		}
		
		
	};// ends Gallery pages vars and functions
	//execute checkGallery
	checkGallery();
	// function to display contact info in a specific field
	var contactClick = function(){
		if ($('.vcard')) {
		var contactInfo = $(this).find('span').text();
		if (contactInfo == 'contato@cassiovasconcellos.com.br') {
			$('.contact-info-container').html('<a href="mailto:contato@cassiovasconcellos.com.br">contato@cassiovasconcellos.com.br</a>');
			$('.contact').css('background-position','10px -42px');
		}
		else if (contactInfo == '+55 11 8426-6686') {
			$('.contact').css('background-position','10px -84px');
			$('.contact-info-container').text(contactInfo);
		};
		}
	};
	$('.vcard ul a').live("click",contactClick);
	
	
	function contactHoverOn() {
		var contactInfo = $(this).find('span').text();
		if (contactInfo == 'contato@cassiovasconcellos.com.br') {
			$('.contact').css('background-position','10px -42px');
		}
		else if (contactInfo == '+55 11 8426-6686') {
			$('.contact').css('background-position','10px -84px');
		};
	};
	function contactHoverOff() {
			$('.contact').css('background-position','10px 0px');
	};
		//if ($('body.category').length){
		$(".vcard a").hover(contactHoverOn,contactHoverOff);
	
	
	
});
