$(document).ready(function(){
	
	

	show_page();
	set_active_page();

	init_move();

	$('#navigation a').click(show_page);
	$('#navigation li').hover(
		function(){
			$(this).find('strong').animate({'right': 0 });
		},function(){
			if($(this).hasClass('active')) return;
			$(this).find('strong').animate({'right': -150 });
	});
	
	var time_interval;
	$(window).scroll(function(){
		window.clearInterval(time_interval);
		time_interval = window.setTimeout(function(){
			set_active_page();
		}, 100);
		
	});
	move_objects();
	$(window).scroll(move_objects);
	$(window).resize(move_objects);

	// carousels
	$('.history-slider ul').jcarousel({
		scroll: 1,
		initCallback: function(carousel){
			$('.history-nav a.next').click(function(){ carousel.next(); return false; });
			$('.history-nav a.prev').click(function(){ carousel.prev(); return false; });	
		},
		buttonNextHTML: null,
		buttonPrevHTML: null
	});


	$('.photos-holder ul').jcarousel({
		scroll: 1,
		initCallback: function(carousel){
			var num_slides = $('.photos-holder li').length;
			for(i=0; i < num_slides; i++) {
				$('.photos-nav').append('<a href="#"></a>');
			}

			$('.photos-nav a').click(function(){
				var index = $(this).parent().find('a').index(this);
				carousel.scroll(index+1);
				return false;
			});
		},

		itemFirstInCallback: function(carousel, item, idx, state) {
			$('.photos-nav a').removeClass('active');
			$('.photos-nav a').eq(idx-1).addClass('active');
		},

		buttonNextHTML: null,
		buttonPrevHTML: null,
		wrap: 'both'
	});

	$('#logo a').click(function(){
		var header_m = $('#header .m');
		var num = header_m.length;

		header_m.each(function(){
			var m = $(this);
			m.css('top', parseInt(m.attr('otop')) ) ;
		});
		move_objects();
	});

	// fancybox
	$(".photos-holder a").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	300, 
		'speedOut'		:	100, 
		'overlayShow'	:	false
	});


});

function show_page() {
	window.setTimeout(function(){
		var id = window.location.hash.toString().replace('#', '', 'gi');
		if(id=='') return;
		var div = $('#' + id + '-container');
		var top = div.offset().top;

		$('html,body').animate({'scrollTop': top}, 'linear');

	}, 100);
}

var last_rel = '';

function set_active_page(){

	var positions = [];
	var scroll_t = $(window).scrollTop();
	var rel = '';

	$('.section').each(function(){
		var t = $(this).offset().top;
		var h = $(this).outerHeight();
		if( scroll_t >= t && scroll_t < t+h ) {
			rel = $(this).attr('id').replace('#', '', 'gi').replace('-container', '', 'gi');
			return;
		}
	});

	var active_li = $('#navigation li.active');
	
	if( rel != last_rel ) {
		last_rel = rel;
		active_li.removeClass('active').trigger('mouseleave');

		if(rel != '') {
			var new_li = $('#navigation a[href="#' + rel + '"]').parent();
			new_li.addClass('active').trigger('mouseenter');
		}
	}
}

function init_move(){
	$('.m').each(function(){
		
		if( $(this).css('position') != 'absolute' ) {
			
		}
		var t = parseInt( $(this).css('top').replace('px', '', 'gi') );
		t = isNaN(t) ? 0 : t;
		
		$(this).attr('otop', t);
	});
}

function move_objects() {
	var m = $('.m');
	var o = [];
	var scroll_t = $(window).scrollTop();
	var win_h = $(window).height();
	var win_t = scroll_t+win_h/2;
	var scroll_c = scroll_t + win_h/2;
	
	m.each(function(){
		var t = $(this).offset().top;
		var h = $(this).outerHeight();
		var this_c = t+h/2;

		// get only the visible elements
		if ( t+h >= scroll_t && scroll_t+win_h > t) {
			o.push($(this));
		}
	});
	for(var i in o) {
		move(o[i]);
	}
}

function move(o) {
	var o = $(o);
	var scroll_t = $(window).scrollTop();
	var win_h = $(window).height();
	var h = o.outerHeight();
	var dtop = win_h + scroll_t - o.offset().top;
	var speed = get_speed(o.attr('class'));

	var o_cof = speed != 1 ? (speed * scroll_t+1) / 1000  : 1;
	var top = -1 * (dtop-100) * 0.07 * o_cof + 40;
	top += parseInt(o.attr('otop'));
	
	o.stop(true, true).animate({'top': top});
}

function get_speed (class_name) {
	var classes = class_name.split(' ');
	for(var i in classes) {
		if( classes[i].indexOf('speed') != -1 ) {
			return parseInt(classes[i].replace('speed-', '', 'gi'));
			break;
		}
	}
	return 1;
}

// Google map
var map_html = '<div class="mapinfo"><h4><a href="index.html">2create studio</a></h4><p>Varna, Bulgaria,<br />Western Industrial Area,<br />Business Center Granite</p></div>';	

function load_map() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));

		map.setCenter(new GLatLng(43.219165, 27.873252), 16);
		map.setUIToDefault();

		var marker = new GMarker(new GLatLng(43.219165, 27.873252));
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(map_html); });
		
	}
}
