;(function($, window) {
	/** Settings **/
			
	// List of background images to use, the default image will be the first one in the list
	/*
	var backgrounds = [
		'images/backgrounds/placeholder.jpg'
	],
	*/
	// Background options - see documentation
	backgroundOptions = {
		
	},
	
	// Twitter username
	twitterUsername = 'sabrewebdesign',
		
	// Number tweets to show, set to 0 to disable
	tweetCount = 5,
	
	// The text inside the search field
	searchBoxText = 'Search',
	
	// The text inside the view map button when map is visible
	hideMapButtonText = 'Hide map';
	
	/** End settings **/
	
	Cufon.replace('h1, h2, h3, h4, h5');
	
	$('html').addClass('js-enabled');
		
	$(document).ready(function() {
		$.fullscreen(
			$.extend(backgroundOptions, {
				backgrounds: window.backgrounds || backgrounds,
				backgroundIndex: window.backgroundIndex
			})
		);
		
		// Initialise the menu
		$('ul.sf-menu').superfish({ speed: 0 });		
		
		// Bind the search button to show/hide the search field
		$('#search-button').click(function() {
			$('.search-container').fadeToggle();
			return false;
		});
				
		// Bind the social button to show/hide the social icons box
		$('#social-pop-out-trigger').click(function() {
			var $allBoxes = $('.footer-pop-out-box');
			if ($allBoxes.is(':animated')) {
				return false;
			}
					
			var $thisBox = $('.social-pop-out-box');
			if ($thisBox.is(':visible')) {
				$thisBox.slideUp();
			} else {
				if ($allBoxes.is(':visible')) {
					$allBoxes.filter(':visible').slideUp(function() {
						$thisBox.slideDown();
					});
				} else {
					$thisBox.slideDown();
				}
			}
			
			return false;
		});
		
		// Bind the Twitter button to show/hide the Twitter feed box
		$('#twitter-pop-out-trigger').click(function() {
			var $allBoxes = $('.footer-pop-out-box');
			if ($allBoxes.is(':animated')) {
				return false;
			}
					
			var $thisBox = $('.twitter-pop-out-box');		
			if ($thisBox.is(':visible')) {
				$thisBox.slideUp();
			} else {
				if ($allBoxes.is(':visible')) {
					$allBoxes.filter(':visible').slideUp(function() {
						$thisBox.slideDown();
					});
				} else {
					$thisBox.slideDown();
				}
			}
			
			return false;
		});
				
		// Bind any links with the class 'scroll-top' to animate the scroll to the top
		var scrollElement = 'html, body';
		$('html, body').each(function () {
		    var initScrollTop = $(this).attr('scrollTop');
		    $(this).attr('scrollTop', initScrollTop + 1);
		    if ($(this).attr('scrollTop') == initScrollTop + 1) {
		        scrollElement = this.nodeName.toLowerCase();
		        $(this).attr('scrollTop', initScrollTop);
		        return false;
		    }    
		});
		
		$('a.scroll-top').click(function () {			
			if ($(scrollElement).scrollTop() > 0) {
				$(scrollElement).animate({ scrollTop: 0 }, 1000);
			}			
			return false;
		});
		
		var $heightSaver 	= $('<div id="height-saver"></div>');
		var menuClicado 	= 0;
		// Bind the navigation to change the page hash
		$('#horz-nav-ul a, .ajax-link').live('click', function() {
			menuClicado = 1;
			if (!$(this).hasClass('non-link') && !$(this).hasClass('non-ajax')) {
				window.location.hash = $(this).attr('href');
			}
			if (!$(this).hasClass('non-ajax')) {
				return false;
			}
			
		});
		
		$('a').delegate('.ajax-link', 'click', function() {
			menuClicado = 1;
			window.location.hash = $(this).attr('href');
			return false;
			/*
			if (!$(this).hasClass('non-link') && !$(this).hasClass('non-ajax')) {
				window.location.hash = $(this).attr('href');
				return false;
			}
			if (!$(this).hasClass('non-ajax')) {
				return false;
			}
			*/
			
		});
		$('a.non-link').live('click', function() {
			window.location.hash = location.hash;
			return false;
		});
		$('a.non-ajax').live('click', function() {
			window.location.hash = location.hash;
			return false;
		});
		
		// Callback for the .load() function, this will be loaded when the new page content has been loaded
		var ajaxCallback = function() {
			// Refresh the cufons
			Cufon.refresh();
			
			// Bind the view map button to slide down / up the map
			var $viewMapButton = $('.view-map'),
			$mapImg = $('.hidden-map'),
			$contactInfoWrap = $('.contact-info-wrap'),
			viewMapButtonText = $('.view-map').text();
			
			$viewMapButton.click(function() {
				if (!$mapImg.add($contactInfoWrap).is(':animated')) {
					if (!$mapImg.hasClass('map-visible')) {
						$contactInfoWrap.slideUp(600, function() {
							$mapImg.slideDown(600, function() {
								$mapImg.addClass('map-visible');
								$viewMapButton.text(hideMapButtonText);
							});
						});
					} else {
						$mapImg.removeClass('map-visible').slideUp(600, function() {
							$contactInfoWrap.slideDown(600, function() {
								$viewMapButton.text(viewMapButtonText);
							});
						});
					}
				}
				return false;
			});
			
			// Make the form inputs and search fields clear value when focused
			$('#search-input').toggleVal({ populateFrom: 'custom', text: searchBoxText });
			$('.toggle-val').toggleVal({ populateFrom: 'label', removeLabels: true });
			
			// Create the gallery rollover effect
			$('li.one-portfolio-item a').append(
				$('<div class="portfolio-hover"></div>').css({ opacity: 0, display: 'block' })
			).hover(function() {
				$(this).find('.portfolio-hover').stop().fadeTo(400, 0.6);
			}, function() {
				$(this).find('.portfolio-hover').stop().fadeTo(400, 0.0);
			});
			
			// Gallery
			$('.serene-portfolio a.portfolio-thumb-link').serene({
				onOpen: $.fullscreen.unbindKeyboard,
				onClose: $.fullscreen.bindKeyboard
			});
			
			// Contact form
			$('form.iphorm').iPhorm();
			
			// Tooltip settings
			if ($.isFunction($.fn.qtip)) {
				$('.iphorm-tooltip').qtip({
					content: {
						text: false
					},
					style: {
						tip: 'leftMiddle',
						name: 'light',
						border: {
							radius: 4,
							width: 4,
							color: '#070707'
						},
						backgroundColor: '#0D0D0C',
						color: '#BBBBBB',
						padding: 6,
						lineHeight: '17px'
					},
					position: {
						corner: {
							target: 'rightMiddle',
							tooltip: 'leftMiddle'
						}
					}
				});
			}
			
			// Changes subject to a text field when 'Other' is chosen
			var subjectHtml = $('.subject-input-wrapper').html();	
			$('#subject').live('change', function () {		
				if ($(this).val() == 'Other') {
					$('.subject-input-wrapper').empty();
					newHtml = $('<input name="subject" type="text" id="subject" value="" />');
					$('.subject-input-wrapper').html(newHtml);
					$cancelOther = $('<a>').click(function () {
						$('.subject-input-wrapper').empty();
						$('.subject-input-wrapper').append(subjectHtml);
						$(this).remove();
						return false;
					}).attr('href', '#').addClass('cancel-button').attr('title', 'Cancel');
					newHtml.after($cancelOther);
				}
			});
			
			// Fade in the content
			$('.content-inner').fadeIn(800);
			
			// Remove the temporary height div
			$heightSaver.css('height', 0).remove();
		};
		
		// Bind the function to be run when the hash changes
		$(window).bind('hashchange', function() {
			
			var hash 	= window.location.hash.substr(1),
			headerInner = $('#header-outer').html(),
			headerLogo	= '<a href="index.php" class="ajax-link"><img src="template/images/logo-off.png" style="float:right;" /></a>',
			wdTotal		= $('#header-outer').width();
			
			if(menuClicado == 1){
			
				//console.log(hash.length);
				//!prettyPhoto.php
				//console.log(hash);
				if( hash.length != 0 ){
					(hash == 'default')? $('#header-inner').fadeIn() : $('#header-inner').fadeOut();
				}else{
					$('#header-inner').fadeIn();
					 hash = 'default';
				}

			}else if( hash.length != 0 && hash != 'default' ){
				
				$('#header-inner').fadeOut();
				
			}
			//console.log(hash);
			//$('#header-outer').animate({marginLeft: -wdTotal+'px'}, 2000);
			
			if (hash) { // && (hash != '!prettyPhoto')
				// Add a div of the same height to prevent scrollbar flicker
				$('.content-inner').fadeOut().hide(0, function() {
					$heightSaver.remove().css('height', $('.content-inner').height()).appendTo($('.content-outer'));
					
					//$('#header-outer').fadeOut().hide(0, function(){
						//$('.content-outer').load(hash + ' .content-inner', ajaxCallback);
					//});
					
					//LOAD PAG
					//alert(hash + ' -> hash |||| ' + window.location.hash.substr(1) +' -> window');
					//url = location.href;
					//hashtagLink = (url.indexOf('!')) ? true : false;
					hashtagTest = (hash.indexOf('prettyPhoto') != -1 ) ? true : false;
					//alert(hash.indexOf('prettyPhoto'));
					var PAG_LOAD = '';
					if(hashtagTest)
					{ 
						//alert(hashtagTest + ' ---> ' + hash + "\n" + hash.indexOf('#!') + "\n" + hash.substring(hash.indexOf('#'),-hash.length));
						//location.hash = "!prettyPhoto";
						PAG_LOAD = hash.substring(hash.indexOf('#'),-hash.length);
					}
					else
					{
						PAG_LOAD = (hash.length < 4) ? 'default' : hash;
					}
						//alert(PAG_LOAD);
					
					$('.content-outer').load(PAG_LOAD + '.php .content-inner', ajaxCallback);
					
				});
				
			} else {
				$('.content-inner').fadeIn(800);
			}
		});
		
		// Fire the hashchange event manually
		$(window).hashchange();
	}); // End (document).ready
	
	$(window).load(function() {
		// Load the Twitter feed
		if (twitterUsername && tweetCount > 0) {
			(function() {
				var t = document.createElement('script'); t.type = 'text/javascript'; t.src = 'http://twitter.com/statuses/user_timeline/' + twitterUsername + '.json?callback=twitterCallback2&count=' + tweetCount;
				var h = document.getElementsByTagName('head')[0]; h.appendChild(t);
			})();
		}
	}); // End (window).load	

	// Any images to preload
	window.preload([
        'template/images/nav-a-bg1.png',
 		'template/images/search1.png',
 		'template/images/minimise1.png',
 		'template/images/2-col-hover.png',
 		'template/images/3-col-hover.png',
 		'template/images/4-col-hover.png',
 		'template/images/5-col-hover.png',
 		'template/images/6-col-hover.png',
 		'template/images/grid-hover.png',
 		'template/images/opacity-80-rep.png'
	]);
/********************************* =============================== ***************************************/
	$(window).load(function() {

		/**********************/
		// Custom sorting plugin // --- No Need to edit this ---\\
		(function($) {
		  $.fn.sorted = function(customOptions) {
			var options = {
			  reversed: false,
			  by: function(a) { return a.text(); }
			};
			$.extend(options, customOptions);
			$data = $(this);
			arr = $data.get();
			arr.sort(function(a, b) {
			  var valA = options.by($(a));
			  var valB = options.by($(b));
			  if (options.reversed) {
				return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
			  } else {		
				return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
			  }
			});
			return $(arr);
		  };
		})(jQuery);

		//---------------------------------------- PW ------------------------------\\
		
		  var read_button = function(class_names) {
			var r = {
			  selected: false,
			  type: 0
			};
			for (var i=0; i < class_names.length; i++) {
			  if (class_names[i].indexOf('selected-') == 0) {
				r.selected = true;
			  }
			  if (class_names[i].indexOf('segment-') == 0) {
				r.segment = class_names[i].split('-')[1];
			  }
			};
			return r;
		  };

		var $filter 	= $('#carta a');
		var $portfolio 	= $('#list');
		var $data 		= $portfolio.clone();
		
		// attempt to call Quicksand on every click event handler 
		$filter.live('click', function(e) {
			var $button_container 	= $(this).parent();
			var button_properties 	= read_button($button_container.attr('class').split(' '));      
			var selected 			= button_properties.selected;
			var button_segment 		= button_properties.segment;
			
			$('#carta li').removeClass('selected-0').removeClass('selected-1').removeClass('selected-2').removeClass('selected-3');
			$button_container.addClass('selected-' + button_segment);
			
			filterBlog($(this).attr('title'));
			return false;
		
		});
		
		/**********************/
	
	});
/********************************* =============================== ***************************************/
	
	$('.nav-blog').live('click', function(){
		filterBlog($(this).attr('title'));
	});
	
})(jQuery, window);

$(function() {
	/*################################# LOGO ####################################### START */
	$('span.logo-off').fadeOut(2000, function(){
		$('span.logo-off').fadeIn(2000)
	});
		
	$('div.logo').bind({
		mouseenter: function(){
			$('span.logo-off').fadeOut(500);
		},
		mouseleave: function(){
			$('span.logo-off').fadeIn(1000);
		}
		/*,
		click: function(){
			document.location.href = 'index.php';
		}
		*/		
	});
	/*################################# LOGO ####################################### END */
	
	/*################################# MENU ####################################### START */
		$('#sdt_menu > li')
		.bind('mouseenter',function(){
			var $elem = $(this);
			$elem.find('img')
			.stop(true)
			.animate({
				'width':'170px',
				'height':'170px',
				'left':'0px'
			},400,'easeOutBack')
			.andSelf()
			.find('.sdt_wrap')
			.stop(true)
			.animate({'top':'140px'},500,'easeOutBack')
			.andSelf()
			.find('.sdt_active')
			.stop(true)
			.animate({'height':'170px'},300,function(){
				var $sub_menu = $elem.find('.sdt_box');
				if($sub_menu.length){
					var left = '170px';
					if($elem.parent().children().length == $elem.index()+1)
					left = '-170px';
					$sub_menu.show().animate({'left':left},200);
				}	
			});
		})
		.bind('mouseleave',function(){
			var $elem = $(this);
			var $sub_menu = $elem.find('.sdt_box');
			if($sub_menu.length)
			$sub_menu.hide().css('left','0px');
			
			$elem.find('.sdt_active')
			.stop(true)
			.animate({'height':'0px'},300)
			.andSelf().find('img')
			.stop(true)
			.animate({
			'width':'0px',
			'height':'0px',
			'left':'85px'},400)
			.andSelf()
			.find('.sdt_wrap')
			.stop(true)
			.animate({'top':'25px'},500);
		});
	/*################################# MENU ####################################### END */
	/*	
	$('#bem-vindo').hide(function(){
		setTimeout( function(){
			$('#bem-vindo').fadeIn(900)
		}, 3000);
	});
	*/
	setTimeout( function alignParceiros(){
		var wdt = '-' + ( $('#parceiros').width() / 2) + 'px';
		$('#parceiros').css({'left':'50%', 'margin-left': wdt}).fadeIn(2500);
		//alert(wdt);
	}, 2000);
	
	function t(){
		$('#like-facebook').html('<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=207920872590199&amp;xfbml=1"></script><fb:like href="www.portobrasilgrelhados.com.br" send="true" layout="button_count" width="450" show_faces="true" font="arial"></fb:like>');
	}
	
	
	
});

function filterBlog(f){
	//alert(f);
	
	if(f == 'all')
	{
		//console.log(1);
		$('#list li:hidden').show()
	}
	else
	{
		
		f= '.'+f;
		//console.log($('#list li:hidden').length);
		/*
		$('#list').find('li').each( function(i,e){
			console.log('==>'+  $(this).find("class").text() +"\n\r" + e);
		});
		*/
		
		if( $('#list li:hidden').length != 0)
		{
			$('#list li:hidden').slideDown(2000, function(){
				setTimeout(function(){
					$('#list li').not(f).slideUp(2500);
				}, 1500);
			});
		}
		else
		{
			$('#list li').not(f).slideUp(2500);
		}
		
		/*
		$('#list').find(filtro).each(function(i,e){
			console.log(filtro);
			$(this).slideDown(900);
		});
		*/
	}
}









