/*
 * $Id KUSHNERStudios 
 *
 * (c)0ded by Serguei Aiatoulin
 *			  Roman Borisoglebskiy
 *
 * serge@aloudstudio.com
 *
 */

$(function() {
	var slider = $('.l-slide-container');

	/**
	* Next button click
	*/
	$('.b-next').click(function(e) {
		e.preventDefault();
		
		// Get current and next elem
		var current = slider.find('.b-slide.active');
		var next_elem = current.next().length == 0 ? slider.find('.b-slide:first') : current.next();
		
		// Remove all active classes and add active class to current elem
		slider.find('.b-slide.active').removeClass('active');
		next_elem.addClass('active');
		
		update_hm(next_elem.attr('id').replace('slide-', ''));
		
		animate_slider(current, next_elem, current.next().length == 0 ? false : true );
		
	});

	/**
	* Previous button click
	*/
	$('.b-prev').click(function(e) {
		e.preventDefault();
		
		// Get current and next elem
		var current = slider.find('.b-slide.active');
		var prev_elem = current.prev().length == 0 ? slider.find('.b-slide:last') : current.prev();
		
		// Remove all active classes and add active class to current elem
		slider.find('.b-slide.active').removeClass('active');
		prev_elem.addClass('active');

		update_hm(prev_elem.attr('id').replace('slide-', ''));
		
		animate_slider(current, prev_elem, current.prev().length == 0 ? true : false );
	});

	/**
	* Update Window.Location.Hash and top menu
	*/
	var update_hm = function(id) {
		$('.b-header-menu > li').removeClass('active');
		$('.b-header-menu > li > a[href="#' + id + '"]').parent().addClass('active');
		
		window.location.hash = id;
	}
	
	/**
	* Animation slider logic
	*/
	function animate_slider(current, elem, right) {
	
	   if ($(current).attr("id")==$(elem).attr("id")){
	       return;
	   }
		// Get title for animation
		var stitle = $(elem).find('.b-slide-title'),		
			// Get right position of title
			pos_right = parseInt(stitle.css('right')),
			// Current viewport slide Front picture and Title
			cur_front = $(current).find('.b-slide-front'),
			cur_front_left = cur_front.css('left'),
			cur_title = $(current).find('.b-slide-title'),
			cur_title_right = cur_title.css('right');
		
		// Hide title
		stitle.css('right', stitle.width() * -1 );
		
		cur_front.animate({ 'left': 2000 * (right ? -1 : 1)}, 600);
		cur_title.animate({ 'right': 2000 * (right ? 1 : -1)}, 600);

		// Animate
		slider.animate({'left': $(elem)[0].offsetLeft * -1 }, 900,function(){
			// Restore right css values
			cur_front.css('left', cur_front_left);
			cur_title.css('right', cur_title_right);
			
			// Animate stitle
			stitle.animate({'right':pos_right + 100}, 200, function() { 
				stitle.animate({'right':pos_right}, 200, function() {});
			});
		});
	}

	/**
	* Expand bottom bar
	*/
	
	$(".b-bottom-expand-r").bind("click",function(){
	   $(".b-bottom-expand").trigger("click");
	   return false;
	});
	$('.b-bottom-expand').click(function(e) {
		e.preventDefault();
		$(this).toggleClass('expanded');
		if ($(this).hasClass('expanded')) { $(this).html('Click to hide:'); }
		else { $(this).html('Click to expand:'); }
		var bar = $('.l-bottombar');
		if (bar.height() == 40) { bar.css('height', 'auto'); }
		else { bar.css('height', '40px'); }
	});
	
	/**
	* Close content panel
	*/
	$('.b-content-close').click(function(e) {
		e.preventDefault();
		$('.b-content-panel').fadeOut();
		$('.b-prev').fadeIn();
		$('.b-next').fadeIn();
		$('.g-content-link').removeClass('active');
	});

	/**
	* manipulate external change
	*/

	$.address.externalChange(function(){
		var _external_url = window.location.hash;
		var _changed_url = _external_url.split("/");

		//console.log(_changed_url[0]);
		if(typeof _changed_url[0] !== 'undefined')
		{
			if(_changed_url[0].length>1)
			{
				if(typeof _changed_url[1] !== 'undefined')
				{
				
					$('.b-prev').fadeOut();
					$('.b-next').fadeOut();
					$('.g-content-link').removeClass('active');
					$('.b-header-menu .b-header-submenu li a[href="' + _external_url + '"]').addClass('active');
					
					load_content(_external_url, function() { $('.b-content-panel').fadeIn(); });
				}
				else
				{
					$('.b-content-panel').fadeOut();
					$('.b-prev').fadeIn();
					$('.b-next').fadeIn();
					$('.g-content-link').removeClass('active');
					
					$("ul.b-header-menu li").removeClass("active");
					$('a.g-area-link[href="'+_external_url+'"]').parent('li').addClass('active');
					
					
					var current = slider.find('.b-slide.active');
					var elem = slider.find('.'+_external_url.replace('#', ''));

					if (current_slider!=elem){

						current.removeClass('active');
						elem.addClass('active');
						animate_slider(current, elem, elem.index() > current.index() ? true : false );

					}
					else
					{
						current_slider = elem;
					}
					
					//console.log("first page");
				}
			}
		}
	});

	/**
	* Open content panel
	*/
	
	$('.g-content-link').live("click",function(e) {
	
		//e.preventDefault();
        $("html").scrollTop(0);		
		// Show control buttons
		$('.b-prev').fadeOut();
		$('.b-next').fadeOut();

		//$('.b-content-panel').fadeOut();
		
		// Add active class to link 
		$('.g-content-link').removeClass('active');
		$('.b-header-menu .b-header-submenu li a[href="' + $(this).attr('href') + '"]').addClass('active');
		$(this).addClass('active');
		
		
		//Our "callback" function to load content (from different sources)
		//console.log($(this));
		load_content($(this), function() { $('.b-content-panel').fadeIn();});
		
		// Show content panel
		
		$(".b-content").scrollTop(0);
	});

	/**
	* Area link click for Slider
	*/	
	
	current_slider = 0;
	$('.g-area-link').click(function() {
		var current = slider.find('.b-slide.active');
		var elem = slider.find('.'+$(this).attr('href').replace('#', ''));

		if (current_slider!=elem){
    		$(".b-content-close").click();
    		
    		current.removeClass('active');
    		elem.addClass('active');
    		
    		$('.b-header-menu > li.active').removeClass('active');
    		$('.b-header-menu > li > a[href="' + $(this).attr('href') + '"]').parent().addClass('active');

    		animate_slider(current, elem, elem.index() > current.index() ? true : false );
		}	
        current_slider = elem;
	});
	
	/**
	* Begin 
	*/
	(function() {
		var a_hash = [];
		if (window.location.hash.length > 0) { var a_hash = window.location.hash.split('/'); }

		if (a_hash.length > 0) {
			$('.b-header-menu > li.active').removeClass('active');
			
			$('.b-header-menu > li > a[href="' + a_hash[0] + '"]').click();
			
			if (typeof a_hash[1] !== 'undefined') { 
			    var _obj = $('a.g-content-link[href="' + window.location.hash + '"]'); 
			    if ($(_obj).size()>0){
                    $(_obj).click(); 
			    }
			    else{
			         if (a_hash[a_hash.length-1].match("|")){
			             $("body").append("<a href='"+window.location.hash+"' class='hndl_load g-content-link' style='display:none;'></a>");
			             $(".hndl_load").trigger("click");
			         }
			    }
			}
		}

	})();
	
});

