jQuery(window).load(function() {
	jQuery("#status").fadeOut();
	jQuery("#preloader").delay(350).fadeOut("slow");
});


jQuery.fn.slideFadeToggle  = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};


jQuery(function() {
	
	jQuery(window).scroll(function() {
		if(jQuery(this).scrollTop() != 0) {
			jQuery('#toTop, #backtotop').fadeIn();	
		} else {
			jQuery('#toTop, #backtotop').fadeOut();
		}
	});
	
	jQuery('#toTop').click(function() {
		jQuery('body,html').animate({scrollTop:0},800);
		return false;
	});	
	
});

jQuery(document).ready(function(){
	
	var $options = jQuery('#options');
	
	jQuery('#nav-button').click(function() {
		$options.slideFadeToggle();		
		$(this).find(".fa").toggleClass("fa-close fa-bars");
	});
/*
	if( jQuery(window).width() < 959){
		jQuery('#options li a').click(function() {
			$options.hide();
		});
	}

	jQuery(window).resize(function() {
		if ( jQuery(window).width() < 959) {
			$options.hide();
			jQuery('#options li a').click(function() {
				$options.hide();
			});
		}
	});
*/
	jQuery(window).resize(function() {
		if ( jQuery(window).width() > 959) {
			$options.show();
			jQuery('#options li a').click(function() {	$options.show();	});
		}
	});			
 
 
	jQuery('.title-wrap, .subtitle-wrap').not('.flexslider .title-wrap, .flexslider .subtitle-wrap').each(function() {
		var $this = jQuery(this);
		
		$this.data('wrapping', $this.width());
		$this.css('width', 0);
	});

	jQuery('.images').not('.flexslider .images').bind('mouseenter', function() {
		jQuery(this).find('.title-wrap').stop().each(function() {
			jQuery(this).animate({	width: jQuery(this).data('wrapping') }, 150);
		});
		jQuery(this).find('.subtitle-wrap').stop().each(function() {
			jQuery(this).delay(250).animate({ width: jQuery(this).data('wrapping')	}, 150);
		});
	});

	jQuery('.images').not('.flexslider .images').bind('mouseleave', function() {
		jQuery(this).find('.title-wrap').stop().each(function() {
			jQuery(this).animate({	width: 0 }, 0);
		});
		jQuery(this).find('.subtitle-wrap').stop().each(function() {
			jQuery(this).animate({	width: 0 }, 0);
		});
	});

	jQuery('.element').hover(function() {
		jQuery(this).find('.hidden-infos').stop().fadeToggle(150);
	});

	jQuery('.movable-content.not-permalink').click(function() {
		jQuery(this).stop().animate({"margin-left": "300px"	}, 200 );
		return false;
	});

	jQuery('.movable-content.not-permalink').parent().bind('mouseleave', function() {
		jQuery(this).find('.movable-content').animate({
			"margin-left": "0px"
		}, 200 );
	});

	if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
		jQuery('body').addClass('touch')
	}
	
	jQuery('#options > #filters > li').not('.touch #options li').append('<div class="menu-line" />');

	jQuery('#options li').not('.touch #options li').bind('mouseover', function() {
		jQuery(this).find('.menu-line').stop().animate({"width": "100%"	}, 150 );
	});

	jQuery('#options li').not('.touch #options li').bind('mouseout', function() {
		jQuery(this).find('.menu-line').stop().animate({"width": "0"}, 150 );
	});

	jQuery('.button').hover(function() {
		jQuery(this).animate({backgroundColor:'#ddd'}, 150);
	}, function() {
		jQuery(this).animate({backgroundColor:'#d6d6d6'}, 250);
	});
	
	var chimg = $(".chimg").attr("src");
	
	jQuery(".dates .row").hover(function() {
		$(this).css({"background-color":"#e2e2e2"});
		$(".chimg").attr("src",$(".col3 img",this).attr("src"));
	}, function() {
		$(this).css({"background-color":"#fff"});
		$(".chimg").attr("src",chimg);
	});
	
	if(!jQuery(".slides li").size()) $(".fs").remove();
	
	jQuery(".aliashome .quote-block").removeClass("border");
	
	jQuery("a.popup, a[rel=gal]").fancybox({
		type:'image',
		closeClick : true,
		helpers : {
			overlay : {
				locked : false
			}
		},
		afterLoad : function() {		
			this.title = $(this.element).attr("title");
			//this.title = (this.title ? '' + this.title + '<br />' : '') + 'Image ' + (this.index + 1) + ' of ' + this.group.length;
		} 
	});
	
	/*
	jQuery(".video-popup").fancybox({
		'width'				: '70%',
		'height'			: '80%',
		'autoScale'			: false,
		'type'				: 'iframe',
		closeClick : true,
		helpers : {
			overlay : {
				locked : false
			}
		}     
	});
	*/
});



jQuery(window).load(function($){
	
	function loadFlexHover(){
		jQuery('.flexslider').hover(function() {
			jQuery(this).find('.caption, .overlay2').stop().animate({"opacity": "1"	}, 150 );
		}, function() {
			jQuery(this).find('.caption, .overlay2').stop().animate({"opacity": "0" }, 150 );
		});
	}

	function ssloadIsotope(){
		var $container = jQuery('#container'),
		isotopeOptions = {},
		defaultOptions = { filter: '.all',   sortBy: 'original-order',   sortAscending: true,   layoutMode: 'masonry'		};
		
		$container.isotope({
			itemSelector : '.element',
			resizable: false,
			masonry: { columnWidth: $container.width() / 12 }
		});
		  
		jQuery(window).smartresize(function(){
			$container.isotope({
				masonry: { columnWidth: $container.width() / 12 }
			});
		});
	  
		var $optionSets = jQuery('#options').find('.option-set'),
			isOptionLinkClicked = false;
		
		function changeSelectedLink( $elem ) {
			$elem.parents('.option-set').find('.selected').removeClass('selected');
			$elem.addClass('selected');
		}
	  
		$optionSets.find('a[href^="#filter"]').click(function(){
		    var $this = jQuery(this);
		    if ( $this.hasClass('selected') ) {	return;  }
		    changeSelectedLink( $this );
		
		    var href = $this.attr('href').replace( /^#/, '' ),
		    option = jQuery.deparam( href, true );
		    jQuery.extend( isotopeOptions, option );
		    jQuery.bbq.pushState( isotopeOptions );
		    isOptionLinkClicked = true;
		    return false;
		});
	
	    var hashChanged = false;
	
		jQuery(window).bind( 'hashchange', function( event ){
			var hashOptions = window.location.hash ? jQuery.deparam.fragment( window.location.hash, true ) : {},
		    aniEngine = hashChanged ? 'best-available' : 'none',
		    options = jQuery.extend( {}, defaultOptions, hashOptions, { animationEngine: aniEngine } );
			$container.isotope( options );
			isotopeOptions = hashOptions;
			
			if ( !isOptionLinkClicked ) {
				var hrefObj, hrefValue, $selectedLink;
				for ( var key in options ) {
					hrefObj = {};
					hrefObj[ key ] = options[ key ];
					hrefValue = jQuery.param( hrefObj );
					$selectedLink = $optionSets.find('a[href="#' + hrefValue + '"]');
					changeSelectedLink( $selectedLink );
				}
			}
		
			isOptionLinkClicked = false;
			hashChanged = true;
		}).trigger('hashchange');
	}
	
	//CALL ISOTOPE DEPENDING ON FLEXSLIDER Existance
	if ( jQuery('.flexslider')[0] ) {
		jQuery('.flexslider').flexslider({
			animation: localize_data.slider_animation,
			slideshowSpeed: localize_data.slide_time,
			animationSpeed: localize_data.animation_time,
			pauseOnHover: true,
			start: function(slider){
				ssloadIsotope();
				loadFlexHover();
			}
		});
		
		var fli=1;
		var tfli = jQuery(".hhero li").not(".clone").size();
		jQuery(".hhero li").not(".clone").each(function() {
			jQuery(".caption p",this).html(fli+" <span>of</span> "+tfli);
			fli++;
		});
	} else {
		ssloadIsotope();
	}	

	jQuery(".cform input[type=text], .cform textarea").each(function () {
		jQuery(this).attr("placeholder",jQuery("#t_"+jQuery(this).attr("id")).text());
	});
	
	jQuery(".pdflink").tooltipster({
		theme: 'tooltipster-shadow', 
		content: 'Loading...',
		functionBefore: function(origin, continueTooltip) {
			if(origin.data('imgid')!="") {
				continueTooltip();
				var imgstr = jQuery('<img src="/img/'+origin.data('imgid')+'" alt="" />');
				origin.tooltipster('content', imgstr);
			}
		}
	});
});
