var doClosing = true;
var lastHash  = '';

function submitForm() {
	$('#frm').submit();
}

$(document).ready(function () {
	positionNews();
	$('#newsBackground').click(function() {
		$('#news').hide();
	});
	
	$('.projectLink').mouseover(function() {
		if ($(this).hasClass('notPub')) return;
		$('.projectThumb').hide();
		
		if ($(this).attr('rel') == 'opened') return;
		
		var image    = $(this).next('.projectThumb');
		var position = $(this).offset();
		var searchPosition = $('#search').offset();
		
		var finalTopPosition = position.top-image.height()+10;
		
		if (finalTopPosition < 140) finalTopPosition = 140;
		
		image.css('float', 'none').css('position', 'absolute').css('left', searchPosition.left).css('top', finalTopPosition).show();
	}).mouseout(function() {
		$('.projectThumb').hide();
	}).click(function() {
		if ($(this).hasClass('notPub')) return;

		if ($(this).attr('rel') == 'opened') {
			closeProject(this);
		} else {
			openProject(this);
		}
		
		return false;
	});

	$('.pageLink').click(function() {
		if ($(this).attr('rel') == 'opened') {
			closePage(this);
		} else {
			openPage(this);
		} 

		return false;
	});

	$('.projectDate ').click(function () {
		var selectedDate = $(this);
		
		$(this).nextUntil('.projectDate').each(function() {
			var subLink = $(this).children('a').first();
			if (selectedDate.attr('rel') == 'opened') {
				closeProject(subLink);			
			} else {
				if (subLink.attr('rel') != 'opened') openProject(subLink);
			}
		});
		
		if (selectedDate.attr('rel') == 'opened') {
			selectedDate.attr('rel', 'closed');
		} else {
			selectedDate.attr('rel', 'opened');		
		}
	});
	
	$('.toTop').live('click', function() {
  		window.scrollTo(0,0);
  		doClosing = false;
	});

	$('.dropFolder').click(function() {
		if ($(this).next('div').is(':visible')) {
			$(this).next('div').slideUp(300);
		} else {
			$(this).next('div').slideDown(300);		
		}
	});

	/*$(window).keydown(function(event) {
		if ((event.metaKey || event.ctrlKey) && event.keyCode == 80) { 
			$('.bloc').each(function() {
				if (!$(this).hasClass('textBl')) {
					$(this).attr('rel', $(this).width()+"-"+ $(this).height());
					$(this).css('width', $(this).width()/2).css('height', $(this).height()/2);
				
					$(this).children('img').attr('rel', $(this).children('img').width()+"-"+ $(this).children('img').height());
					$(this).children('img').css('width', $(this).children('img').width()/2).css('height', $(this).children('img').height()/2);
				
					$(this).children('.planThumb').children('img').attr('rel', $(this).children('.planThumb').children('img').width()+"-"+ $(this).children('.planThumb').children('img').height());
					$(this).children('.planThumb').children('img').css('width', $(this).width()).css('height', $(this).height());
				}
			});
		}
		
		setTimeout(function() {
			$('.bloc').each(function() {
				if (!$(this).hasClass('textBl')) {
					var sizes = $(this).attr('rel').split('-');
					$(this).css('width', sizes[0]+"px").css('height', sizes[1]+"px");
				
					try {
						var sizes = $(this).children('img').attr('rel').split('-');
						$(this).children('img').css('width', sizes[0]+"px").css('height', sizes[1]+"px");
					} catch (e) {}
				
					try {
						var sizes = $(this).children('.planThumb').children('img').attr('rel').split('-');
						$(this).children('.planThumb').children('img').css('width', sizes[0]+"px").css('height', sizes[1]+"px");
					} catch (e) {}
				}
			});
		}, 100);
	});*/
	
	$('.print').live('click', function() {
		var currentObj = $(this).parents('.openedProject').attr('id').replace(/project_/g, '');
		/*$('.openedProject').removeClass('openedProject').attr('rel', 'op');
		$(currentObj).addClass('openedProject');		
		
		$('.bloc').each(function() {
			if (!$(this).hasClass('textBl')) {
				if ($(this).children('img').get(0) != undefined || $(this).children('.planThumb').get(0) != undefined) {
					$(this).attr('rel', $(this).width()+"-"+ $(this).height());
					$(this).css('width', $(this).width()/2).css('height', $(this).height()/2);
				
					$(this).children('img').attr('rel', $(this).children('img').width()+"-"+ $(this).children('img').height());
					$(this).children('img').css('width', $(this).children('img').width()/2).css('height', $(this).children('img').height()/2);
				
					$(this).children('.planThumb').children('img').attr('rel', $(this).children('.planThumb').children('img').width()+"-"+ $(this).children('.planThumb').children('img').height());
					$(this).children('.planThumb').children('img').css('width', $(this).width()).css('height', $(this).height());
				}
			}
		});

  		window.print();

		$('.projectLine').each(function () {
			if ($(this).attr('rel') == 'op') {
				$(this).addClass('openedProject').attr('rel', '');
			}
		});
		
		$('.bloc').each(function() {
			if (!$(this).hasClass('textBl')) {
				var sizes = $(this).attr('rel').split('-');
				$(this).css('width', sizes[0]+"px").css('height', sizes[1]+"px");
				
				try {
					var sizes = $(this).children('img').attr('rel').split('-');
					$(this).children('img').css('width', sizes[0]+"px").css('height', sizes[1]+"px");
				} catch (e) {}
				
				try {
					var sizes = $(this).children('.planThumb').children('img').attr('rel').split('-');
					$(this).children('.planThumb').children('img').css('width', sizes[0]+"px").css('height', sizes[1]+"px");
				} catch (e) {}
			}
		});*/
		if (window.location.toString().search(/\/office\//) == '-1') {
			window.open("/print.php?projectId="+currentObj, "", "fullscreen=no,toolbar=no,status=no0,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=400,height=200,left="+(screen.width-400)/2+",top="+(screen.height-200)/2);
		} else {
			window.print();
		}
		
		doClosing = false;
	});
	
	$('.planThumb').live('mouseover', function(event) {
		var position = $(this).offset();
		$(this).children('a').show().css('position', 'absolute').css('left', event.pageX - position.left + 20).css('top', event.pageY - position.top + 20);
	});

	$('.planThumb').live('mousemove', function(event) {
		var position = $(this).offset();
		$(this).children('a').css('position', 'absolute').css('left', event.pageX - position.left + 20).css('top', event.pageY - position.top + 20);
	});

	$('.planThumb').live('mouseout', function() {
		$(this).children('a').hide();
	});
	
	$('.planThumb').live('click', function() {
  		showPlan($(this).children('a').attr('href'));
  		return false;
	});

	$('.planThumb a').live('click', function() {
  		showPlan($(this).attr('href'));
  		return false;
	});
	
	$('#plan').click(function() {
		$(this).hide();
		$('#content').fadeIn();
		$('#search').show();
		$(document).scrollTop(scrollTopValue);
	});
	
	$('.filter2').click(function() {
		$('#searchText').css('border-bottom', '1px solid black').css('color', 'black').val('');

		var hadClass = $(this).hasClass('selectedRed');
		if (hadClass) {
			$(this).removeClass('selectedRed');
		} else {
			$(this).addClass('selectedRed');		
		}
		
		searchSite(false);
		return false;		
	});
	
	$('.filter').click(function () {

		$('#searchText').css('border-bottom', '1px solid black').css('color', 'black').val('');

		var hadClass = $(this).hasClass('selectedRed');
		if ($(this).hasClass('type')) {
			$('.type').removeClass('selectedRed');
		} else {
			$('.status').removeClass('selectedRed');		
		}
		
		if (!hadClass) $(this).addClass('selectedRed');
		searchSite(false);
		return false;
	});
	
	$('#searchText').keyup(function() {
		searchSite(true);
	});
	
	$('#searchText').focus(function() {
		$(this).css('border-bottom', '1px solid white').css('color', 'red');
	}).keyup(function() {
			if ($(this).val() == "") {
				$(this).css('border-bottom', '1px solid black').css('color', 'black');
			} else {
				$(this).css('border-bottom', '1px solid white').css('color', 'red');			
			}
	});
	
	$('#searchTextOffice').keyup(function() {
		searchPage();
	});
	
	$('#searchTextOffice').focus(function() {
		$(this).css('border-bottom', '1px solid white').css('color', 'red');
	}).keyup(function() {
			if ($(this).val() == "") {
				$(this).css('border-bottom', '1px solid black').css('color', 'black');
			} else {
				$(this).css('border-bottom', '1px solid white').css('color', 'red');			
			}
	});

	
	$('.projectContent').live('click', function () {
		if (!doClosing) {
			doClosing = true;
			return;
		}
		
		closeProject($(this).parents('.projectLine').children('a').first());
		doClosing = true;
	});

	$('.pageContent').live('click', function () {
		if (!doClosing) {
			doClosing = true;
			return;
		}
		
		closePage($(this).parents('div').children('a').first());
		doClosing = true;
	});
	
	if (location.hash != '') {
		lastHash = location.hash;
		var data = location.hash.split("-");
		
		if(data[0] == '#project') {
			openProject($('#project_'+data[1]).children('a'));
		} else {
			openPage($('#page_'+data[1]).children('a'));
		}
	}
	
	$('#newsBloc a').click(function() {
		$('#news').hide();
	});
	
	setInterval(checkHash, 250);
});

function checkHash() {
	if (location.hash != lastHash) {
		var data = location.hash.split("-");
		
		if(data[0] == '#project') {
			openProject($('#project_'+data[1]).children('a'));
		} else if(data[0] == '#page') {
			openPage($('#page_'+data[1]).children('a'));
		}
		
		lastHash = location.hash;
	}
}

$(window).resize(function () {
	positionNews();
	
	$('.projectContent').each(function() {
  			makeHeight($(this));
  	});
});

function openPage(jElm) {
	$(jElm).addClass('selectedRed');
	$(jElm).parent().addClass('openedProject');
	$(jElm).parent().append('<div class="pageContent"><img src="/images/loading.gif" alt="loading" /></div>');
	
	$(jElm).parent().children('.pageContent').show();
	$.get('/ajax/getPage.php?pageId='+$(jElm).attr('href').replace(/#/g, ''), function(data) {
		$(jElm).parent().children('.pageContent').hide();		
  		$(jElm).parent().children('.pageContent').html(data).slideDown(300, function() {
  			makeHeight($(this));
  		});
		$('.pageContent a').click(function () { doClosing = false; });

  		lastHash 		= "#page-"+$(jElm).attr('href').replace(/#/g, '')+"-"+makeLink($(jElm).text());
		location.hash 	= "page-"+$(jElm).attr('href').replace(/#/g, '')+"-"+makeLink($(jElm).text());
	});

	$(jElm).attr('rel', 'opened');
}

function closePage(jElm) {
	$(jElm).removeClass('selectedRed');
	$(jElm).parent().removeClass('openedProject');

	$(jElm).parent().children('.pageContent').slideUp(300, function () {
		$(this).remove();
	});
	$(jElm).attr('rel', 'closed');
	lastHash 		= "n";
	location.hash 	= "n";
}

function openProject(jElm) {
	if ($(jElm).hasClass('notPub')) return;
	$(jElm).addClass('selectedRed');
	$(jElm).parent().addClass('openedProject');
	
	$(jElm).parent().children('.projectThumb').hide();
	$(jElm).parent().append('<div class="projectContent"><img src="/images/loading.gif" alt="loading" /></div>');
	
	$(jElm).parent().children('.projectContent').show();
	$.get('/ajax/getProject.php?projectId='+$(jElm).attr('href').replace(/#/g, ''), function(data) {
		$(jElm).parent().children('.projectContent').hide();		
  		$(jElm).parent().children('.projectContent').html(data).slideDown(300, function() {
  			makeHeight($(this));
  		});
  		
  		lastHash 		= "#project-"+$(jElm).attr('href').replace(/#/g, '')+"-"+makeLink($(jElm).text());
  		location.hash 	= "project-"+$(jElm).attr('href').replace(/#/g, '')+"-"+makeLink($(jElm).text());

	});

	$(jElm).attr('rel', 'opened');
}

function closeProject(jElm) {
	$(jElm).removeClass('selectedRed');
	$(jElm).parent().removeClass('openedProject');
	$(jElm).attr('rel', 'opened');
	$(jElm).parent().children('.projectContent').slideUp(300, function () {
		$(this).remove();
	});
	$(jElm).attr('rel', 'closed');
	
	lastHash 		= "n";
	location.hash 	= "n";
}

function makeLink(text) {
	var link = text.toLowerCase();
	    link = link.replace(/"/g, "");
	    link = link.replace(/'/g, "");
	    link = link.replace(/\?/g, "");
	    link = link.replace(/!/g, "");
	    link = link.replace(/”/g, "");
	    link = link.replace(/“/g, "");
	    link = link.replace(/ /g, "-");
	    link = link.replace(/©/g, "copyright");
	    link = link.replace(/Â/g, "a");
	    link = link.replace(/Ä/g, "a");
	    link = link.replace(/É/g, "e");
	    link = link.replace(/È/g, "e");
	    link = link.replace(/Ë/g, "e");
	    link = link.replace(/Ô/g, "o");
	    link = link.replace(/Ö/g, "o");
	    link = link.replace(/Ü/g, "u");
	    link = link.replace(/ç/g, "c");

	    link = link.replace(/â/g, "a");
	    link = link.replace(/ä/g, "a");
	    link = link.replace(/é/g, "e");
	    link = link.replace(/è/g, "e");
	    link = link.replace(/ë/g, "e");
	    link = link.replace(/ô/g, "o");
	    link = link.replace(/ö/g, "o");
	    link = link.replace(/ü/g, "u");
	    link = link.replace(/ç/g, "c");
	    link = link.replace(/\./g, "-");
	    link = link.replace(/,/g, "-");
	    link = link.replace(/--/g, "-");
	return link;
}

function makeHeight(jElm) {
	jElm.children('.bloc').css('height', '');
	jElm.children('.bloc').each(function() {
		if ($(this).prev().hasClass('bloc')) {
			if ($(this).offset().top == $(this).prev().offset().top) {
				//alert($(this).children('img').get(0));
				
				var height 		= $(this).prev().height();
				var imageHeight = $(this).children('img').height();
	
				var finalHeight = (height < imageHeight) ? imageHeight : height;
				
				if (finalHeight < $(this).height()) finalHeight = $(this).height();
				$(this).css('height', finalHeight);
				
			} 
		}
	});
}

function positionNews() {
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	$('#newsBloc').css('left', windowWidth/2 - 225).css('top', windowHeight/2 - 162);
}

var scrollTopValue = 0;

function showPlan(link) {
	scrollTopValue = $(document).scrollTop();
	$(document).scrollTop(0);

	$('#plan').html("<img src='"+link.replace(/uploads\//g, 'uploads/thumbs/900/')+"' alt='' />").fadeIn();
	$('#content').hide();
	$('#search').hide();
}

function searchPage() {
	var query = $('#searchTextOffice').val();
	
	
	if (query.length < 3) {
		$('.officeTitle').show();
		$('.pageLine').show();
		return;
	}
	
	$.get('/ajax/searchPage.php?query='+query, function(data) {
		$('.pageLine').each(function() {
			if (!isInPageList(data, $(this).attr('id'))) { 
				$(this).hide();
			} else {
				$(this).show();
			}

			closePage($(this).children('a'));
		});
		
		$('.officeTitle').each(function () {
			var numVisible = 0;
			$(this).nextUntil('.officeTitle').each(function() {
				if ($(this).is(':visible')) numVisible++;
			});
			
			if (numVisible == 0) {
				$(this).hide();
			} else {
				$(this).show();			
			}
		});
	});
}

function searchSite(useText) {
	var query  = '';
	var status = '';
	var type   = '';
	var contest= '';
	
	if (useText) {
		query = $('#searchText').val();
		$('.type').removeClass('selectedRed');
		$('.status').removeClass('selectedRed');
		
		if (query.length < 3) {
			$('.projectDate').show();
			$('.projectLine').show();
			return;
		}
	} else {
		status  = $('.status').filter('.selectedRed').attr('href');
		type    = $('.type').filter('.selectedRed').attr('href');
		contest = $('.filter2').filter('.selectedRed').attr('href');
		
		if (status != undefined) status = status.replace(/#status/g, '');
		if (type != undefined) type = type.replace(/#type/g, '');
		if (contest != undefined) contest = contest.replace(/#status/g, '');
	}
	
	$.get('/ajax/searchProject.php?query='+query+'&status='+status+'&type='+type+'&contest='+contest, function(data) {
		$('.projectLine').each(function() {
			if (!isInList(data, $(this).attr('id'))) { 
				$(this).hide();
			} else {
				$(this).show();
			}

			closeProject($(this).children('a'));
		});
		
		
		if (query == '' && status == undefined && type == undefined && contest == undefined) {
			$('.projectDate').show();	
		} else {
			$('.projectDate').hide();
		}
	});
}

function isInPageList(list, id) {
	list = list.split(',');
	id	 = id.replace(/page_/, '');
	
	for (var i = 0; i < list.length; i++) {
		var listElement = list[i];
		if (list[i] == id) return true;
	}
	
	return false;
}

function isInList(list, id) {
	list = list.split(',');
	id	 = id.replace(/project_/, '');
	
	for (var i = 0; i < list.length; i++) {
		var listElement = list[i];
		if (list[i] == id) return true;
	}
	
	return false;
}
