/*
	Template Name: Enfolio
	Template URI: http://themeforest.net/user/cepreu
	Author: Cepreu
	Author URI: http://cepreu.net/
*/

function setHeaders()
{
	var headers = $$('.section-header');
	var headerheight = $$('.section_bg');
	var koordinaten = [];
	headers.each(function(item)
	{
		koordinaten.push(item.getCoordinates());
	});

	headerheight.each(function(item, key)
	{
		item.setStyle('top', koordinaten[key].top+'px');
	});
}

jQuery.noConflict();
jQuery(document).ready(function() {

	/* ------------------------------------------------
	   Set min-height property (portfolio section)
	------------------------------------------------ */
	var portfolioHeight = jQuery('#portfolio').height();

	/* ------------------------------------------------
	   Initialize portfolio thumbs hover
	------------------------------------------------ */
/*
	jQuery('#list a[rel^="prettyPhoto"]').cepreuPortfolioThumbsHover({
		defaultOpacity: 1,
		onMouseOverOpacity: 0.2,
		speed: 300,
		zoomImg: '/tl_files/theme/images/zoom.png'
	});
*/
	/* ------------------------------------------------
	   Add additional portfolio elements
	------------------------------------------------ */
	//jQuery('#list a[rel^="prettyPhoto"]').append('<span class="thumbPortfolio"></span>');

	/* ------------------------------------------------
	   Sort portfolio (Quicksand plugin) onClick
	------------------------------------------------ */
	var $preferences = {
		duration: 800,
		easing: 'easeInOutQuad',

		/* Adjusts the height of container to fit all the items, 'auto' for automatically adjusting before or after the animation (determined automatically),
		   'dynamic' for height adjustment animation, false for keeping the height constant. */
		adjustHeight: false,
		attribute: 'id',	// Attribute used to match items in collections. You can provide custom function to extract unique values.

		/* If you wish to integrate their visual enhancements (eg. font replacement), specify a function that refreshes or re-applies enhancement to
		   an item during the animation. */
		enhancement: 	function() {},

		/* Use scaling (CSS3 transform) animation. Requires to include this plugin - http://www.zachstronaut.com/posts/2009/08/07/jquery-animate-css-rotate-scale.html
		   to your project. Turned off automatically if you did not. */
		useScaling: true
	};

	var $list = jQuery('#list');
	var $data = $list.clone();
	var sorting1, sorting2 = null;

	function filterQuicksand()
	{
		if((sorting1 == 'all' || sorting1 == null) && (sorting2 == 'all' || sorting2 == null))
			var $filtered_data = $data.find('li');
		else if((sorting1 != 'all' || sorting1 != null) && (sorting2 == 'all' || sorting2 == null))
			var $filtered_data = $data.find('li.' + sorting1);
		else if((sorting1 == 'all' || sorting1 == null) && (sorting2 != 'all' || sorting2 != null))
			var $filtered_data = $data.find('li.' + sorting2);
		else if((sorting1 != 'all' || sorting1 != null) && (sorting2 != 'all' || sorting2 != null))
		{
			var $filtered_data = [];
			var $filter1 = $data.find('li.' + sorting1);
			$data.find('li.' + sorting1).each(function(key1, val1) {
				$data.find('li.' + sorting2).each(function(key2, val2) {
					if(val1 === val2)
						$filtered_data.push(val1);
				});
			});
		}

		$list.quicksand($filtered_data, $preferences, function() {
			jQuery('#list').height(''); // portfolio section layout fix
			setHeaders();
		});
	}

	var $controls1 = jQuery('#filter1');

	$controls1.each(function(i) {

		var $control = jQuery(this);
		var $buttons = $control.find('a');

		// onClick handler
		$buttons.bind('click', function(e) {
			var $button = jQuery(this);
			var $button_container = $button.parent();
			var button_properties = $button_container.attr('class');

			$buttons.parent().removeClass('selected');
			$button_container.addClass('selected');

			sorting1 = $button.attr('class');

			filterQuicksand();
			e.preventDefault();
		});
	});

	var $controls2 = jQuery('#filter2');

	$controls2.each(function(i) {

		var $control = jQuery(this);
		var $buttons = $control.find('a');

		// onClick handler
		$buttons.bind('click', function(e) {
			var $button = jQuery(this);
			var $button_container = $button.parent();
			var button_properties = $button_container.attr('class');

			$buttons.parent().removeClass('selected');
			$button_container.addClass('selected');

			sorting2 = $button.attr('class');

			filterQuicksand();
			e.preventDefault();
		});

	});


	if(window.location.search.substring(1))
	{
		var vars = window.location.search.substring(1).split("&");
		for(var i = 0; i < vars.length; i++)
		{
			var pair = vars[i].split('=');
			if(pair[0] == 'quicksand_group')
			{
				var group = 'a.group_'+unescape(pair[1]).toLowerCase();
				jQuery('#filter1 '+group+', #filter2 '+group).click();
			}
		}
	}

	/* ------------------------------------------------
	   Initialize Portfolio layout switcher
	------------------------------------------------ */
	jQuery('#layoutSwitcher a').cepreuPortfolioLayoutSwitcher({
		show_switcher: true,
		full_width_layout: false // set 'true' if you want full-width layout
	});

});

/* ------------------------------------------------
   Portfolio thumbs hover
------------------------------------------------ */
(function($) {
	$.fn.cepreuPortfolioThumbsHover = function(options) {

		// Default options
		options = $.extend({
			defaultOpacity: 1,
			onMouseOverOpacity: 0.2,
			speed: 300,
			zoomImg: '/tl_files/theme/images/zoom.png'
		}, options);

		var $target = $(this);

		$target.hover(function() {
			var $thumb = $(this).find('img');
			var $thumbZoom = $(this).find('span');

			$thumb.animate({
				opacity: options.onMouseOverOpacity
			}, options.speed);

			$thumbZoom.append('<img src="images/' + options.zoomImg + '" />');

		}, function() {
			var $thumb = $(this).find('img');
			var $thumbZoom = $(this).find('span');

			$thumb.animate({
				opacity: options.defaultOpacity
			}, options.speed);

			$thumbZoom.html('');
		});

	};

	/* ------------------------------------------------
	   Portfolio layout switcher
	------------------------------------------------ */
	$.fn.cepreuPortfolioLayoutSwitcher = function(options) {

		// Default options
		options = $.extend({
			show_switcher: true,
			full_width_layout: false
		}, options);

		var $switcher = $(this);
		var $target = $('.image-grid');

		if (!options.show_switcher) {
			$(this).css('display', 'none');
		};

		if (options.full_width_layout) {

			$switcher.addClass('full-width');
			$target.addClass('full-width');

			$switcher.toggle(function(){
				$('#list').height(''); // portfolio section layout fix

				$(this).removeClass('full-width');
				$target.fadeOut('fast', function() {
					$(this).fadeIn('fast').removeClass('full-width');
				});
			}, function () {
				$('#list').height('');

				$(this).addClass('full-width');
				$target.fadeOut('fast', function() {
					$(this).fadeIn('fast').addClass('full-width');
				});
			});

		} else {

			$switcher.toggle(function(){
				$('#list').height('');

				$(this).addClass('full-width');
				$target.fadeOut('fast', function() {
					$(this).fadeIn('fast', function() {
						setHeaders();
					}).addClass('full-width');
				});
			}, function () {
				$('#list').height('');

				$(this).removeClass('full-width');
				$target.fadeOut('fast', function() {
					$(this).fadeIn('fast', function(){
						setHeaders();
					}).removeClass('full-width');
				});
			});

		};

	};
})(jQuery);
