function OsoboPortfolio()
{
	this.rewind = false;

	this.rewindDir = null;

	this.timer = null;

	this.interval = 10;

	this.boost = 3;

	this.ghostClones = {};

	this.div1 = null;
	this.div2  = null;

	this.div1 = $('div#projectsWrapper1');
	this.div2 = $('div#projectsWrapper2');

	this.projectList = new Array();

	this.tagFilter = null;

	this.prevBodyHeight = null;

	this.run = function ()
	{
		$('div.projectsControl a').click( function (e) {
			e.preventDefault();			
			var dir = $(this).attr('id').substr(8);
			osoboPortfolio.nextFrame(dir);
		});

		$('a.projectsControl').mouseover( function (e) {
			e.preventDefault();
			$(this).addClass('projectsControlHover');
		});

		$('a.projectsControl').mouseout( function (e) {
			e.preventDefault();
			$(this).removeClass('projectsControlHover');
		});

		$('div.project a').click( function (e) {
			e.preventDefault();
			osoboPortfolio.openProject($(this).parent('div.project'));
		});

		$('a#bigPortfolioEnough').click( function (e) {
			e.preventDefault();
			osoboPortfolio.closeProject();
		});

		$('div#bigPortfolio').click( function (e) {
			e.preventDefault();
			osoboPortfolio.closeProject();
		});

		$('div#allProjectsGhosts div.ghostProject').mouseover( function (e) {
			var id = $(this).attr('id').substr(12);
			osoboPortfolio.ghostClones[id] = $(this).clone(true);
			$(this).removeClass('ghostProject').addClass('project')
				   .replaceWith($('div#project' + id).clone(true));
		});
		
		$('div#allProjects div.project').mouseout( function (e) {
			var id = $(this).attr('id').substr(7);
			$(this).replaceWith(osoboPortfolio.ghostClones[id]);
		});

		$('div#bigProjectControls a').click( function (e) {
			e.preventDefault();
			osoboPortfolio.nextProject($(this).attr('id').substr(10));
		});

		if (this.tagFilter != null)
			this.openFirstProjectWithTag(this.tagFilter);
	}

	this.nextFrame = function (dir)
	{
		if (dir == 'Back')
			this.rewindDir = 1;
		else if (dir == 'Fwd')
			this.rewindDir = -1;
		else
		{
			alert('error: invalid dir ' + dir);
			return;
		}

		$('div.projectsWrapper').each( function (index, elem) {
			//console.log($(this).attr('id'));
			//alert($(this).attr('id'));
			var w = $('div.projects').width();
			var offset = parseInt($(this).css('left')) + w * osoboPortfolio.rewindDir;
			//alert('left: ' + $(this).css('left') + ' offset: ' + offset + ' w: ' + w + ' w0: ' + $(this).width());			
			if (offset <= 0 && Math.abs(offset) < $(this).width())
			{
				$(this).hide();
				$(this).find('div.project').addClass('projectBackgrIEHack');
				//alert('offset: ' + offset + ' w: ' + w + ' w0: ' + $(this).width());
				$(this).css('left', offset);
				$(this).fadeIn('normal', function () {
					$(this).find('div.project').removeClass('projectBackgrIEHack');
				});
			}
		});
	}

	this.openProject = function (project)
	{
		osoboMenu.enableRollovers(false);
		$('a#email').hide();
		$('div#osoboPhone2').hide();
		$('div#osoboCopyright').hide();
		$('span#bigPortfolioInfoVal').html('');
		$('div#osoboLetter').css('visibility', 'hidden');
		var bigWidth = parseInt(project.children('input[name="bigWidth"]')[0].value);
		var bigHeight = parseInt(project.children('input[name="bigHeight"]')[0].value);
		$('div#bigPortfolioWrapper').css('width', bigWidth)
									.css('height', bigHeight);
		var projId = project.children('input[name="id"]')[0].value;
		$('input#curProjectId').attr('value', projId);
		this.adjustProjectPosition();

		var fullHeight = parseInt($('div#bigPortfolioWrapper').css('height')) + 200;
		this.adjustViewportSize(fullHeight);
		$('div#superback').css('height', fullHeight).show();
		//if ($('body').height() < 700)
		//	$('html').css('overflow', 'scroll');
		//if (bigHeight + 140 >= $('body').height())
			$('html').css('overflow', 'scroll');
		$('div#bigPortfolio').show();
		$('img#bigPortfolioPreloader')
			.css('top', $(window).height() / 2 - ($('#bigPortfolioPreloader').height() / 2))
			.show();
		$('img#bigPortfolioImage').css('visibilty', 'hidden')
								  .attr('src', project.children('input[name="bigRelPath"]')[0].value);
	}

	this.nextProject = function (dir)
	{
		if (dir == 'Back')
			dir = -1;
		else
			dir = 1;
		var curId = parseInt($('input#curProjectId').attr('value'));
		var proj = null;
		for (var i = curId + dir; i <= $('div.project').length; i += dir)
		{
			var nextId = i;
			if ($('div#project' + nextId).length < 1)
				return;
			proj = $('div#project' + nextId);
			if (this.projectHasTag(proj, this.tagFilter))
			{
				proj = $('div#project' + nextId);
				this.closeProject(true, true);
				this.openProject(proj);
				break;
			}
		}
	}

	this.projectHasTag = function (project, tag)
	{
		if (!tag)
			return true;
		tag = tag.toLowerCase();
		var tags = project.children('input[name="tags"]').attr('value').replace(', ', ',').toLowerCase();
		var tagList = tags.split(',');
		for (var i = 0; i < tagList.length; i++)
			if (tagList[i] == tag)
				return true;

		return false;
	}

	this.openFirstProjectWithTag = function (tag)
	{
		for (var i = 1; i <= $('div.project').length; i++)
		{
			var proj = $('div#project' + i)
			if (proj.length < 1)
				return;
			if (this.projectHasTag(proj, tag))
			{
				this.openProject(proj);
				this.tagFilter = tag;
				return;
			}
		}
	}

	this.onBigPortfolioImageLoaded = function (e)
	{		
		//$('body').css('overflow', 'visible');
		//$('div#sumo').css('height', $(document).height());
		$('img#bigPortfolioPreloader').hide();
		//osoboPortfolio.adjustProjectPosition();
		$('img#bigPortfolioImage').fadeIn('normal');
		osoboPortfolio.updateBigProjectInfo();
		$('div#bigProjectControls').css('visibility', 'visible');
	}

	this.adjustProjectPosition = function ()
	{
		var x = ($('body').width() + $(document).scrollLeft()) / 2;
		var y = ($('body').height() + $(document).scrollTop()) / 2;
		var div = $('div#bigPortfolio');
		var top = y - div.outerHeight() / 2;
		if (top < 0)
			top = 10;
		div.css('left', x - div.outerWidth() / 2)
		   .css('top', top);	
	}

	this.adjustViewportSize = function (height)
	{
		this.prevBodyHeight = parseInt($('body').height());
		$('body').css('height', height);
	}

	this.closeProject = function (showControls, saveTagFilter)
	{
		$('body').css('height', this.prevBodyHeight);
		osoboMenu.enableRollovers(true);
		$('html').css('overflow', 'hidden');
		$('div#bigPortfolio').hide();
		$('img#bigPortfolioImage').hide();
		$('div#superback').hide();
		$('div#osoboLetter').css('visibility', 'visible');
		if (!showControls)
			$('div#bigProjectControls').css('visibility', 'hidden');
		if (!saveTagFilter)
			this.tagFilter = null;

		$('a#email').show();
		$('div#osoboPhone2').show();
		$('div#osoboCopyright').show();
	}

	this.updateBigProjectInfo = function ()
	{
		var proj = $('div#project' + this.getCurProjId());
		var desc = proj.children('input[name="description"]').attr('value');
		var copyright = proj.children('input[name="copyright"]').attr('value');
		var tags = proj.children('input[name="tags"]').attr('value');
		var info = '';
		if (desc.length > 0 || copyright.length > 0)
		{
			if (desc.length > 0)
			{
				info = info + desc;
				if (copyright.length > 0)
					info = info + ' / ';
			}

			if (copyright.length > 0)
				info = info + copyright;

			info = info + '<br />';
		}
		if (tags.length > 0)
			info = info + 'Ключевые слова: ' + '<span>' + tags + '</span>';
		$('span#bigPortfolioInfoVal').html(info);
	}

	this.getCurProjId = function ()
	{
		return $('input#curProjectId').attr('value');
	}
}

function isMouseInProjectsWrapper(e)
{	
	var div = $('div#allProjects');
	var xy1 = getOffset(div[0]);
	var x2 = xy1.left + div.width();
	var y2 = xy1.top + div.height()+ 100;
	return (e.clientX >= xy1.left && e.clientX <= x2 && e.clientY >= xy1.top && e.clientY <= y2);
}

var osoboPortfolio = null;
osoboPortfolio = new OsoboPortfolio();	

$(document).ready( function () {	
	osoboPortfolio.run();
	osoboMenu.addSkipFunction(isMouseInProjectsWrapper);
	//var div = $('div#allProjects');
	//$('div#debug2').text(div.css('left') + ' ' + div.css('top') + ' ' + div.css('margin-top'));
});