var baseHref;
if (location.href.indexOf('www') == -1) {
	baseHref = 'http://costeapartners.ro/';
} else {
	baseHref = 'http://www.costeapartners.ro/';
};
	
var windowWidth;
if ( $(window).width() > 1000 ) {
	windowWidth = $(window).width();
} else {
	windowWidth = 1000;
};

var pageMargin = (windowWidth - 1000) / 2;
var horizontalTransitionDistance = windowWidth + 1998;
var verticalTransitionDistance = $('#home').height();
var scrollPosition;

var paintingShow = false;

layout = {
	transitionInterval: undefined,
	doNotMove: true,
	cache: [],
	direction: undefined,
	pozitionezAbsolut : function(){
		$('#home').css({
			'left' : horizontalTransitionDistance + pageMargin + 'px'
		});
		$('#right').css({
			'width' : windowWidth + 'px',
			'left' : horizontalTransitionDistance + windowWidth + 'px'
		});
		$('#left').css({
			'width' : windowWidth + 'px',
			'left' : 0
		});
		$('#nav').css({
			'width' : horizontalTransitionDistance*2 + windowWidth + 'px'
		});
	},
	clearSides: function(){
		// chestia asta se executa de fiecare data cind inceteaza miscarea
//		if(location.hash.replace('#', '') == 'index' && this.tagName.toLowerCase() == 'html') $('#left, #right').empty();		
		if(location.hash.replace('#', '') == 'index') $('#left, #right').empty();
	},
	checkSlide : function(){
		if(!layout.doNotMove) {
			layout.doNotMove = true;
			layout.slide();
		}
	},
	slide: function(){
		// setam inaltimea continutului din viewport
		var currentContentHeight;
		if ( layout.direction == 'index' ) {
			currentContentHeight = 1360;	
		} else {
			currentContentHeight = $('#' + layout.direction + ' .page').height() + 120;
		};
		if ( currentContentHeight > $(window).height() ) {
			$('.page, #nav').css({'height' : currentContentHeight + 'px'}); 
		} else { 
			$('.page, #nav').css({'height' : $(window).height() + 'px'}); 			
		}; 
		// nu putem sa bagam un "if" in $.animate(), asa ca facem altfel ca sa evitam asteptarile inutile		
		var coord = config[layout.direction];
		var slider = '#nav';
		if ( window.pageYOffset == 0 ) {
			$(slider).animate({
				left : coord.coordX + 'px'									
			}, 2000, 'linear', layout.clearSides);
		} else {
			$('html, body').animate({
				scrollTop : coord.coordY
			}, 500, 'linear', function(){
				$(slider).animate({
					left : coord.coordX + 'px'								
				}, 2000, 'linear', layout.clearSides);
			});
		};
	},
	set: function(state) {
		layout.direction = states[state];
		remotePage = baseHref + state + '.html .page';
		newTitle = 'Costea - ' + state.replace('-', ' ');
		var _direction = layout.direction;
		layout.doNotMove = false;
		if(state != 'index') {
			if(layout.cache[state] == '' || layout.cache[state] == undefined) {
				layout.doNotMove = true;
				$('#' + _direction).load(remotePage, function(){
					pregatescLinkuri();
					layout.cache[state] = $('#' + _direction).html();
					layout.doNotMove = false;
				});
			}
			else $('#' + _direction).html(layout.cache[state]);
		}
		document.title = newTitle;
	}
};

function pregatescLinkuri(){
	$('a.painting, a#logo').each(function(){
		$(this).attr('title', '');
		currentHref = $(this).attr('href');
		if ( currentHref.indexOf('#') == -1 ) {
			newHref = '#' + currentHref.replace('.html', '');
			$(this).attr('href', newHref);
		};
	});
	$('a.out').each(function(){
		$(this).attr('target', '_blank');
	});
}

function ticker(nume){
	if( !$('.box#current-lawyer-box .profile#' + nume).hasClass('current') ) {
		$('.box#current-lawyer-box .current').fadeOut(700).removeClass('current');
		$('.box#current-lawyer-box .profile#' + nume).fadeIn(700).addClass('current');
	};
};

config = {
	left : {coordX : 0, coordY : 0},
	right : {coordX : -1*(horizontalTransitionDistance*2),	coordY : 0},
	index : {coordX : -1*(horizontalTransitionDistance), coordY : 0}
}


var states = {
	'index': 'index',
	'practice-areas': 'right',
	'the-firm': 'right',
	'our-people': 'left',
	'links': 'left'
}

var doNotMove = true;

tooltip = {
	create : function(){
		var tooltip = '<div id="tooltip"></div>';
		$('body').append(tooltip);
		$('body').mousemove(function(e){
			var marginLeft = e.pageX + 20;
			var marginTop = e.pageY - 50;											
			$('#tooltip').css({
				'left': marginLeft + 'px', 
				'top': marginTop + 'px'
			});
		});
		return this;
	},
	change : function(){
		$('a.painting, a#logo').live('mouseover', function(){
			newClass = $(this).attr('id');
			$('#tooltip').attr('class', newClass);
		});
		$('img.painting').live('mouseover', function(){
			newClass = $(this).attr('class');
			$('#tooltip').attr('class', newClass);
		});		
		$('a.painting, a#logo, img.painting').live('mouseout', function(){
			$('#tooltip').attr('class', '');
		});		
	}
};

painting = {
	overlay : '#overlay',
	elm : '#overlay img',
	text : '#overlay p',
	load : function(sursaTablou, dateTablou) {
//		var _image = new Image();
//		_image.src = src;
//		_image.alt = 'Big picture';
//		$('#overlay').append(_image);
		var numeAutor = dateTablou.split(' - ')[0];
		var numeTablou = dateTablou.split(' - ')[1];
		$('#overlay').append('<img src="'+ sursaTablou +'" alt="Big picture">');
		$('#overlay').append('<p><span>' + numeTablou + '</span><br>' + numeAutor + '</p>');
		return this;
	},
	position : function(parent){
		var left;
		if ( parent.attr('id') == 'right') {
			left = parseInt(parent.css('left')) + 1998 + 'px';
		} else {
			left = parent.css('left');
		};
		$(this.overlay).css({
			'height' : $(document).height() + 'px',
			'width' : $(window).width() + 'px',
			'left' : left
		});
		$(this.elm).load(function(){
			var topDistance = ( $(window).height() - $(this).height() ) / 2 - 50;
			var pictureTop;
			if ( topDistance < 0 ) { pictureTop = 0; } else { pictureTop = topDistance; };
			$(this).css({
				'top' : pictureTop + 'px',
				'left' : ( $(window).width() - $(this).width() ) / 2 + 'px'
			});
			$('#overlay p').css({
				'top' : pictureTop + $(this).height() + 5 + 'px',
				'left' : ( $(window).width() - 300 ) / 2 + 'px'		
			});
		});		
	},
	show : function(){
		scrollPosition = window.pageYOffset;
		window.scrollTo(0, 0);
		$(this.overlay).slideDown();
		paitingShow = true;
				
	},
	hide : function(){
		$(this.overlay).slideUp().html('');
		window.scrollTo(0, scrollPosition);
		paintingShow = false;
	}
};

$(function(){
	// facem modificarile necesare
	$('body').addClass('js-on');
	layout.pozitionezAbsolut();
	pregatescLinkuri();
	
	$(window).resize(function(){
		if ( $(window).width() > 1000 ) {
			windowWidth = $(window).width();
		} else {
			windowWidth = 1000;
		};
		console.log(windowWidth)
		pageMargin = (windowWidth - 1000) / 2;
		horizontalTransitionDistance = windowWidth + 1998;
		verticalTransitionDistance = $('#home').height();
		layout.pozitionezAbsolut();			
	});
	
	// ne punem pe index
	$('#nav').css({
		'left' : config.index.coordX + 'px'
	});

	$('img.painting').live('click', function(){
		var parent;
		if ( $(this).parents('.page').attr('id') == home ) {
			parent = $('.page#home');
		} else {
			parent = $(this).parents('.page').parent('div');
		};
		var sursaTablou = $(this).attr('longdesc').split(' | ')[0];
		var dateTablou = $(this).attr('longdesc').split(' | ')[1];		
		painting.load(sursaTablou, dateTablou).position(parent);
		painting.show();
		$(window).resize(function(){
			painting.position(parent);
			// din nou
//			topDistance = ( $(window).height() - $(this).height() ) / 2 - 50;
//			if ( topDistance < 0 ) { pictureTop = 0; } else { pictureTop = topDistance; };
			$('#overlay img').css({
//				'top' : pictureTop + 'px',
				'left' : ( $(window).width() - $('#overlay img').width() ) / 2 + 'px'
			});
			$('#overlay p').css({
//				'top' : pictureTop + $(this).height() + 5 + 'px',
				'left' : ( $(window).width() - 300 ) / 2 + 'px'		
			});			
		});
	});
	
	$('#overlay').live('click', function(){
		painting.hide();
	});

	$('a.ticker').live('click', function(){
		care = $(this).attr('rel');
		ticker(care);
		return false;
	});
	
	layout.transitionInterval = setInterval("layout.checkSlide()", 100);
	
//	if ( location.hash == '' || location.hash == undefined || states[location.hash.replace('#', '')] == undefined) location.href = '#index';

	// navigarea are history si se intampla in functie de location.hash
	$.History.bind(function(state) {
		if ( state == '' || state == undefined || states[state] == undefined) {
			location.href = '#index';
		}
		layout.set(state);
	})

	tooltip.create().change();

});
