$(function() {
	// create the animation imgs
	var i = 0, amIhome = 1;
	$('.block').each( function(i, e) {
		var $this = $(e), start = 15, top = 0, multi = 0;
		$this.append('<div class="bgr" style="background-position:  -'+( i * 215 )+'px 0"></div>');
		reposition(true);
	})
	
	// animatiile backgroundurilor profi
	function niceBgrs( mod ) {
		if (mod)
		$('.block').hover(
			function() { 
				var $this = $(this);
				$this.find('div.bgr').stop().animate({ 'top': 0 },150,'easeInQuad');
				$('.block').not($this).find('h3.title').stop().animate({ 'opacity' : 0.4 }, 100, 'easeOutQuad');
			},
			function() { 
				$(this).not('#current').find('div.bgr').stop().animate({ 'top': -360 }, 150,'easeOutQuad');
				$('.block').find('h3.title').stop().animate({'opacity': 1}, 150, 'easeInQuad');
			}
		)
		else
			$('.block').unbind('hover')
	}
	niceBgrs(true)
	
	$('.block').click( function() {
		//console.log('am dat click!')
		var $this = $(this), whichToLoad = $this.attr('rel').substr(4), toLoad = whichToLoad+'.html #loading-content > *';
		document.location.hash = whichToLoad;
		$('.block').not($this).hide(250);
		$this.attr('id', 'current');
		$this.animate({'top' : '41px', 'left' : 0, 'margin' : 0}, 350, 'easeInOutBack');
		ajaxInfo(true);
		$('#loading-content').load(toLoad,'',function () {
				ajaxInfo(false);
				$('#loading-content').css({ 'left':'-600px', 'display' : 'block', 'zIndex' : '-1' })
				.animate(
					{ 'left' : 20 },
					250,
					'easeInOutBack', function() { $(this).css({'zIndex' : 20 }); }
				);//slideDown(250, 'easeInQuad')
				//initGoHome(); // nu mai e nevoie, am folosit delegate
				//$('#the-content').jScrollPane();
				dynamicPagesAppend();
			})
		amIhome = 0;
	});
	
	$('#footer a').click( function(e) {
		e.preventDefault();
		var timer = 50, $this = $(this);
		var wait = setInterval(function() {
				if( !$('.block').is(':animated') ) {
					clearInterval(wait);
					$('div[rel=nav_' + $this.attr('href').substr(0, $this.attr('href').length - 5)
									 + ']').click().find('.bgr').css({'top':0});
				}
			}, timer);
		return false;
	});
	
	// asa-zisul cursor :)
	setInterval('$("#flick").fadeToggle(500)', 800)
	
	function reposition( first ) {
		//lock(true);
		$('.block').each( function(i, e) {
			var $this = $(e), start = 30, top = 0, multi = 0, tileSize = 217;
			if (i >= 3) {
				multi = i - 3; start = 190; top = '70px';
			} else multi = i;
			
			if (first)
				$this.not('#current').css( 
					{
						'position': 'absolute', 
						'left' : start + (multi*tileSize*1.0)+'px', 
						'top' : top ,
						'margin' : '1px'
					}
				);
			else 
				$this.css({'margin' : '1px'}).animate( 
					{
					'left' : start + multi*tileSize*1.0+'px', 
					'top' : top
					}, 'slow', 'easeOutQuad' 
				);
			
		});
		//lock(false);
	}
	
	function dynamicPagesAppend() {
		if ($('#mapContainer').length) { // pentru pagina de contact
			$('#mapContainer').css({'cursor':'pointer'});//dialog({ autoOpen: false })
			if (!$('#temp-dialog').length)
				dialog = $('<div id="dialog" style="display:hidden"></div>').appendTo('body');
			$('#mapContainer').click( function() { 
				dialog.load('/new/static/contact/mapBig.html', function() {
					dialog.dialog({ hide: 'fade', modal: true, show: 'fade', width: '655px', resizable: 'false' });
				});
			});
			
		}
	}
	
	// initGoHomeButton
	function initGoHome() {
	$('#loading-content').delegate('#back', 'click', function() {
		$myDiv = $('#current');
		$('#loading-content').fadeOut(150, 'easeOutQuad', function() {
			$('#loading-content').css({'left':'220px', 'paddingLeft':'20px'});
		})
		document.location.hash = '#';
		$myDiv.find('div.bgr').css({ 'top': -360 })
		$('.block').show(250);
		reposition(false)
		$myDiv.removeAttr('id')//.css({'top':0, 'position':'relative'})
		amIhome = 1;
	})};
	initGoHome();
	
	// relocation from x.html to #x
	function redirectFromHTML() {
	 if( !location.hash && (location.hash.substr(1,1) != '#' && location.hash.substr(1,1) != '/') ) {
		var arr_request = location.pathname.split('/'), new_request = '', i;
		for( i = 1; i < arr_request.length - 1; i++ )
			new_request += arr_request[i] + '/';
		new_request += '#' + arr_request[i].substr(0, arr_request[i].length - 5);
		location.replace(location.protocol+'//'+location.hostname+'/'+new_request);
     }
	}
	redirectFromHTML();
	
	// hash navigation
	function loadContent() {
		var hash = window.location.hash.substr(1),
			href = $('#footer ul li a').each(function(){
			var href = $(this).attr('href');
			if(hash==href.substr(0,href.length-5))  {
				$('.block[rel=nav_'+hash+']').click()
			}
		});	
	}
	loadContent();
	
	function lock( mod ) {
		if (mod) 
			$('#dummy').css({'display':'block'});
		else {
			var wait = setInterval(function() {
				if( !$('.block').is(':animated') ) {
					clearInterval(wait);
					$('#dummy').css({'display':'none'});
				}
			}, 100);
		}
	}
	
	function ajaxInfo( mod ) {
		var display = mod ? 'block' : 'none';
		$('#ajaxload').css({'display' : display});
	}
	//$(window).bind('hashchange', loadContent );

})

/*  google analytics */

var _gaq=[['_setAccount','UA-20969535-2'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1;
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
