// -- CUSTOM FUNCTIONS AND EFFECTS --

// -- NO CONFLICT MODE --
var $s = jQuery.noConflict();

$s(function(){

	// -- IMAGE PRELOADER --
	target = $s(".cycle_slider li, .loadify, .custom_embed, .ar_thumb, .img_thumb, .mf_thumb, .mslides li, .thumb_big, .thumb_medium img");
	images = target.find("img");
	counter = 0;
	i=0;
	loaded = [];
	nextDelay = 0;
	images.each(function(){
		if( $s(this).parent().length == 0 )
			$s(this).wrap("<span class='preload' />");
		else
			$s(this).parent().addClass("preload");
		loaded[i++] = false;
	});
	images = $s.makeArray(images);

	timer = setInterval(function() {
		if( counter >= loaded.length )
		{
			clearInterval(timer);
			return;
		}
		for( i=0; i<images.length; i++ )
		{
			if( images[i].complete )
			{
				if( loaded[i] == false )
				{
					loaded[i] = true;
					counter++;
					nextDelay = nextDelay + 100;
				}
				$s(images[i]).css("visibility","visible").delay(nextDelay).animate({opacity:1}, 300,
				function(){
					$s(this).parent().removeClass("preload");
				});
			}
			else
			{
				$s(images[i]).css( {"visibility":"hidden", opacity:0} );
			}
		}
	}, 100 );
});


// -- DOCUMENT.READY --
$s(document).ready(function(){

	// -- NAVIGATION MENU --

	$s('.nav1 ul, .nav_top ul').css({display: "none"});
	$s('.nav1 li, .nav_top li').hover(function(){
		$s(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(200);
	},
	function(){
		$s(this).find('ul:first').css({visibility: "visible",display: "none"});
	});


	// -- ACCORDION --

	$s('h5.handle').click(function() {
		$s(this).next().slideToggle(300);
		$s(this).toggleClass("activehandle");
		return false;
	}).next().hide();


	// -- FAQ --

	$s('h5.question').click(function() {
		$s(this).next().slideToggle(300);
		$s(this).toggleClass("activeques");
		return false;
	}).next().hide();


	// -- TOGGLE --

	$s('h5.toggle').click(function() {
		$s(this).next().slideToggle(300);
		$s(this).toggleClass("activetoggle");
		return false;
	}).next().hide();


	// Change the HTML5 data-rel attribute to rel

	$s('a[data-rel]').each(function() {
		$s(this).attr('rel', $s(this).data('rel'));
	});

	// -- PRETTYPHOTO --

	$s("a[rel^='prettyPhoto[group1]'], a[rel^='prettyPhoto[group2]'], a[rel^='prettyPhoto[inline]']").prettyPhoto({
		animation_speed: 'fast',
		slideshow: 5000,
		autoplay_slideshow: false,
		opacity: 0.80,
		show_title: false,
		theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		overlay_gallery: false,
		social_tools: false
	});


	// -- THUMBNAIL HOVER EFFECT --

	jQuery.fn.thumbHover = function(options) {
	   var settings = jQuery.extend({ xOffset: 0, yOffset: 0 }, options);
		$s(this).each(function()
		{
			var images = $s(this).contents("img");
			if (images.length > 0)
			{
				var zoom = $s('<div class="zoom"></div>').prependTo($s(this));
				var icons = $s(this).find('.thumb_controls');
				$s(zoom).css({opacity: "0"});
				$s(icons).css({opacity: "0"});
				$s(this).hover(function()
					{
						var x = images.width();
						var y = images.height();
						var xy =  images.position();
						$s(zoom).css({width:x, height:y, top:xy.top + settings.xOffset, left:xy.left + settings.yOffset});
						$s(icons).css({top:y/2 - 10, left:x/2 - 19 });
						$s(zoom).stop().animate({"opacity": "0.5"}, 200);
						$s(icons).stop().animate({"opacity": "1"}, 200);
					}, function() {
						$s(zoom).stop().animate({"opacity": "0"}, 200);
						$s(icons).stop().animate({"opacity": "0"}, 200);
					}
				);
			}
		});
		return this;
	 };
	$s(".ar_thumb, .img_thumb, .mf_thumb").thumbHover({ xOffset: 0, yOffset: 0 });


	// -- TOP OF PAGE --

	$s('.top_btn').hide();
	$s(window).scroll(function () {
		if( $s(this).scrollTop() > 100 ) {
			$s('.top_btn').fadeIn(300);
		}
		else {
			$s('.top_btn').fadeOut(300);
		}
	});

	$s('.top_btn a').click(function(){
		$s('html, body').animate({scrollTop:0}, 500 );
		return false;
	});


	// -- BOX CLOSE BUTTON --

	$s(".box").each(function(){
		$s(this).append('<span class="hide_box"></span>');
			$s(this).find('.hide_box').click(function(){
			$s(this).parent().hide();
		});
	});


	// -- SOCIAL ICONS FADE --

	$s('ul.social32 li a, ul.social16 li a').hover(function(){
		$s(this).stop().animate({opacity: "0.8"}, 100 );
	}, function() {
		$s(this).stop().animate({opacity: "1"}, 100 );
	});

	$s('.cycle_nav').css({opacity:"0"});
	$s('.show_desc, .slide_date').hide();

}); // END DOCUMENT.READY


// -- WINDOW.LOAD
$s(window).load( function() {
	$s('.show_desc, .slide_date').fadeIn();
	$s('.slider, .slider_medium, .slider_large').hover(function(){
		$s(this).find('.cycle_nav').stop().animate({opacity: "1"}, 200 );
	}, function() {
		$s(this).find('.cycle_nav').stop().animate({opacity: "0"}, 200 );
	});
});
// -- END WINDOW.LOAD --
