var currentPage = 'home';
var copy;
var addresses = new Array("above", "home", 'ceramics', 'production', 'news', 'about', 'contacts');

function angle(center, p1) {
	var p0 = {x: center.x, y: center.y - Math.sqrt(Math.abs(p1.x - center.x) * Math.abs(p1.x - center.x) + Math.abs(p1.y - center.y) * Math.abs(p1.y - center.y))};
	return (2 * Math.atan2(p1.y - p0.y, p1.x - p0.x)) * 180 / Math.PI;
}

function handleChange(event) {
	
    var sections = event.path.split("/");
	
    //SWFAddress.setTitle(formatTitle(event.path));
	if(sections[1].length>1){openPage(sections[1]);}
	
}

function setAddress () {
	
	SWFAddress.setValue('/'+this.get('address'));
}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
var isiOS;
var activeLink;

window.addEvent('domready', function () {
	
	
	
	activeLink = $('description_link');
	
	$('description_link').addEvent('mousedown', function() {
		
	
		
		new Fx.Tween('production_c', {duration:850, property:'margin-left'}).start(0);
		new Fx.Tween('description_link', {duration:850, property:'opacity'}).start(0).addEvent('complete', function(){activeLink.setStyle('display', 'none');});
		activeLink.setStyle('display', 'block');
		new Fx.Tween(activeLink , {duration:850, property:'opacity'}).start(1);
		activeLink.setStyle('margin-top', this.getStyle('margin-top'));
		activeLink.setStyle('margin-left', this.getStyle('margin-left'));
		activeLink = $('description_link');
		
	});
	$('gallery_link').addEvent('mousedown', function() {
		
		
		new Fx.Tween('production_c', {duration:850, property:'margin-left'}).start(-675);
		new Fx.Tween('gallery_link', {duration:850, property:'opacity'}).start(0).addEvent('complete', function(){activeLink.setStyle('display', 'none');});;
		activeLink.setStyle('display', 'block');
		activeLink.tween('opacity',0,1);
		activeLink.setStyle('margin-top', this.getStyle('margin-top'));
		activeLink.setStyle('margin-left', this.getStyle('margin-left'));
		activeLink = $('gallery_link');
	});
	$('tech_link').addEvent('mousedown', function() {
		
		new Fx.Tween('production_c', {duration:850, property:'margin-left'}).start(-1350);
		new Fx.Tween('tech_link', {duration:850, property:'opacity'}).start(0).addEvent('complete', function(){activeLink.setStyle('display', 'none');});;
		activeLink.setStyle('display', 'block');
		activeLink.tween('opacity',0,1);
		activeLink.setStyle('margin-top', this.getStyle('margin-top'));
		activeLink.setStyle('margin-left', this.getStyle('margin-left'));
		activeLink = $('tech_link');
	});
	
	new Fx.Accordion($('accordion'), '#accordion h2', '#accordion .pcontent');
	
	isiOS = navigator.userAgent.match(/iPad/i) != null;
	if(isiOS==false){
		isiOS = navigator.userAgent.match(/iPod/i) != null;
	}
	if(isiOS==false){
		isiOS = navigator.userAgent.match(/iPhone/i) != null;
	}
	//isiOS = true;
	//alert(isiOS);
								  
	if ($('map')) {initialize();}
	reposition ();
	
	if($('productiontxt')){$('gallery-flexcroll-vscroller').set('opacity', 0);}
	
	if (isiOS==false){
		for (var i = 0; i<$$('#menu a').length; i++) {
	
		
		var link = $$('#menu a')[i];
		link.set('address', addresses[i]);
		if (i==0) {
			link.set('address', 'home');
		}
		if (i==1) {
			link.set('address', 'above');
		}
		
		link.addEvent('mousedown', setAddress);
		}
	} else {
		SqueezeBox.initialize();
	}
	
	if ($$('.close_project')){
	for (var i = 0; i<$$('.close_project').length; i++) {
		
		var link = $$('.close_project')[i];
		link.set('index', i);
		
		link.addEvent('mousedown', closeProject);
	}
	}
	if ($$('.ceramics_link')){
	for (var i = 0; i<$$('.ceramics_link').length; i++) {
		
		var link = $$('.ceramics_link')[i];
		link.set('index', i);
		
		link.addEvent('mousedown', openTile);
	}
	}
	if ($('close_tile')){
	$('close_tile').addEvent('mousedown', closeTile);
	}
	if ($$('.preview')){
	for (var i = 0; i<$$('.preview').length; i++) {
		
		var link = $$('.preview')[i];
		link.set('index', i);
		
		link.addEvent('mousedown', openProject);
	}
	}
	
	
	if ($$('.circle_areas')){
		for (var i = 0; i<$$('.circle_area').length; i++) {
		
			var link = $$('.circle_area')[i];
			link.set('index', i);
		
			link.addEvent('mousedown', openGallery);
		}
	}
	
	
	if ($$('.news_entry')){
		
		for (var i = 0; i<$$('.news_entry').length; i++) {
			$$('.news_entry')[i].set('opacity', 0);
		}
		$$('.news_entry')[0].set('opacity', 1);
		activeEntry = $$('.news_entry')[0];
	}
									  
});


function setupScrollbars () {
	if ($$('.flexcroll-scrollbar')){
		for (var i = 0; i<$$('.flexcroll-scrollbar').length; i++) {
		
			var flex = $$('.flexcroll-scrollbar')[i];
			flex.getParent().setStyle('opacity', 0);
		
			//alert(flex.getStyle('visibility'));
			if (flex.getStyle('visibility')!='hidden') {
				flex.getParent().set('opacity', 1);
				flex.getParent().setStyle('display', 'block');
				flex.getParent().setStyle('visibility', 'visible');
			}
		}
	}
}

var activeEntry;
var counter =0;

var tilesOpen = false;

function closeTile () {
	
	new Fx.Tween('ceramics', {property:'margin-left', duration:850}).start(0);
	tilesOpen = false;
	$('previews').removeEvent('mousedown', closeTile);
}



function openTile () {
	var index = Number(this.get('index'));
	
	if (isiOS) {
		
		var txt = $$('.tile .tile_description')[index];
		txt.setStyle('margin-top',0);
	//alert(txt);
		SqueezeBox.open(txt, {handler: 'clone',size: {x: 500, y: 250}});
		
	} else {
		
	if (tilesOpen) {
		
		$$('.tile').setStyle('display','none');
		$$('.tile')[index].setStyle('display','block');
		var target = $$('.tile')[index];
		target.tween('opacity',0,1);
		
	} else {
		
		$$('.tile').setStyle('display','none');
		$$('.tile')[index].setStyle('display','block');
		new Fx.Tween('ceramics', {property:'margin-left', duration:850}).start(window.getSize().x/2);
		$('previews').addEvent('mousedown', closeTile);
		
	}
	tilesOpen = true;
	}
}


function openEntry (index) {
	//alert(index);
	new Fx.Tween(activeEntry, {duration:600, property:'opacity'}).addEvent('complete', fadeEntryIn).start(0);
	activeEntry = $$('.news_entry')[index];
	//$$('.flexcroll-scrollbar')[index].setStyle('visibility', 'visible');
}


function fadeEntryIn () {
	//this.set('opacity', 1);
	counter++;
	activeEntry.setStyle('z-index', 1000+counter);
	activeEntry.setStyle('visibility', 'visible');
	activeEntry.set('opacity', 1);
	//new Fx.Tween(activeEntry, {duration:850, property:'opacity'}).start(1);
}

function openProject() {
	
		
		//isiOS = true;
		var index = Number(this.get('index'));
		if (isiOS) {
			
			
			//alert(projectImages[index]);
			Slimbox.open(projectImages[index], 0);
			
		}else{
			
			copy = this.clone();
			$('ceramics').adopt(copy);
		
			var pos = getElementAbsolutePos(this); 
			
			copy.set('index', index);
			copy.setStyle('position', 'absolute');
			copy.setStyle('cursor', 'pointer');
			copy.setStyle('top', pos.y);
			copy.setStyle('left', pos.x);
			copy.getElement('span').setStyle('display','none');
			//
			//
			var xDes = window.getSize().x/2 - copy.getSize().x/2;
			var offset = xDes-pos.x;
			new Fx.Tween(copy, {property:'left', duration:500}).start(xDes);
			new Fx.Tween('previews', {property:'margin-left', duration:500}).start(offset);
		
			var projectSelected = $$('.project_selected')[this.get('index')];
			projectSelected.setStyle('opacity', 0);
			projectSelected.setStyle('display', 'block');
			
			(function(){
				new Fx.Tween(projectSelected, {property:'opacity', duration:500}).start(1);
				new Fx.Tween('opening', {property:'opacity', duration:500}).start(0).addEvent('complete', hideOpening);
				copy.addEvent('mousedown', openGallery);
			}).delay(1000);
		}
	}


function openGallery() {
	
	var index = Number(this.get('index'));
	//alert(projectImages[index]);
	Slimbox.open(projectImages[index], 0);
}


function hideOpening () {
	$('opening').setStyle('display','none');
}

function hideSelected () {
	$$('.project_selected').setStyle('display','none');
}

function closeProject () {
	
	$('opening').setStyle('display','block');
	var projectSelected = $$('.project_selected')[this.get('index')];
	new Fx.Tween(projectSelected, {property:'opacity', duration:500}).start(0).addEvent('complete', hideSelected);
	new Fx.Tween(copy, {property:'opacity', duration:500}).start(0).addEvent('complete', removeCopy);
	$('previews').setStyle('margin-left', 0);
	
	(function(){
			new Fx.Tween('opening', {property:'opacity', duration:500}).start(1);
	}).delay(500);
	
}

function removeCopy () {
	copy.setStyle('display', 'none');
}

function openPage(target){
	
	if (target != currentPage) {
	var prevIndex = addresses.indexOf(currentPage);
	if (target!='home') {
		new Fx.Tween('menu', {property:'top', duration:900}).start(0);
		new Fx.Tween('menu', {property:'background-color', duration:900}).start('#f0efee');
	} else {
		new Fx.Tween('menu', {property:'top', duration:900}).start(window.getSize().y/2);
		new Fx.Tween('menu', {property:'background', duration:900}).start('transparent');
	}
	
	
	var currIndex = addresses.indexOf(target);
	
	if (currIndex>prevIndex) {
		new Fx.Tween(currentPage, {property:'top', duration:900}).start(-window.getSize().y);
	}else{
		new Fx.Tween(currentPage, {property:'top', duration:900}).start(window.getSize().y);
	}
	
	currentPage = target;
	var direction = 'up';
	
	
	if (currIndex>prevIndex) {
		$(currentPage).setStyle('top',window.getSize().y);
	}else{
		direction = 'down';
		$(currentPage).setStyle('top',-window.getSize().y);
	}
	new Fx.Tween(currentPage , {property:'top', duration:900}).start(0);
	
	animate(currentPage, direction);
	}
}


function animate(target, direction) {
	
	if (target == 'ceramics' && direction == 'up'){
		
		var org = $('ceramics_links').getStyle('margin-top');
		$('ceramics_links').setStyle('margin-top', 150);
		new Fx.Tween('ceramics_links' , {property:'margin-top', duration:1100}).start(org);
		
		org = $('previews').getStyle('margin-top');
		$('previews').setStyle('margin-top', 200);
		new Fx.Tween('previews' , {property:'margin-top', duration:1100}).start(org);
		
		org = $('previews-flexcroll-hscroller').getStyle('margin-top');
		$('previews-flexcroll-hscroller').setStyle('margin-top', 200);
		new Fx.Tween('previews-flexcroll-hscroller' , {property:'margin-top', duration:1100}).start(org);
		
	}
	
	if (target == 'about' && direction == 'up'){
		
		var org = $('abouttxt').getStyle('margin-top');
		$('abouttxt').setStyle('margin-top', 250);
		new Fx.Tween('abouttxt' , {property:'margin-top', duration:1200}).start(org);
		
		org = $('about_links').getStyle('margin-top');
		$('about_links').setStyle('margin-top', 450);
		new Fx.Tween('about_links' , {property:'margin-top', duration:1300}).start(org);
		
		org = $('abouttxt-flexcroll-vscroller').getStyle('margin-top');
		$('abouttxt-flexcroll-vscroller').setStyle('margin-top', 450);
		new Fx.Tween('abouttxt-flexcroll-vscroller' , {property:'margin-top', duration:1000}).start(org);
		
		var line = $$("#about .content .line")[0];
		org = line.getStyle('margin-top');
		line.setStyle('margin-top', 550);
		new Fx.Tween(line , {property:'margin-top', duration:1400}).start(org);
		
	}
	
	
	if (target == 'contacts' && direction == 'up'){
		
		var org = $('contacts_info').getStyle('margin-top');
		$('contacts_info').setStyle('margin-top', 150);
		new Fx.Tween('contacts_info' , {property:'margin-top', duration:1100}).start(org);
		
	}
	
	if (target == 'news' && direction == 'up'){
		
		
		var org = $('news_entries').getStyle('margin-top');
		$('news_entries').setStyle('margin-top', 250);
		new Fx.Tween('news_entries' , {property:'margin-top', duration:1200}).start(org);
		
		org = $('indexes').getStyle('margin-top');
		$('indexes').setStyle('margin-top', 450);
		new Fx.Tween('indexes' , {property:'margin-top', duration:1300}).start(org);
		
		var line = $$("#news .content .line")[0];
		org = line.getStyle('margin-top');
		line.setStyle('margin-top', 550);
		new Fx.Tween(line , {property:'margin-top', duration:1400}).start(org);
		
	}
	
	
	if (target == 'production' && direction == 'up'){

		org = $('productiontxt').getStyle('margin-top');
		$('productiontxt').setStyle('margin-top', 250);
		new Fx.Tween('productiontxt' , {property:'margin-top', duration:1200}).start(org);
		
		org = $('gallery_link').getStyle('margin-top');
		$('gallery_link').setStyle('margin-top', 450);
		new Fx.Tween('gallery_link' , {property:'margin-top', duration:1300}).start(org);
		
		org = $('tech_link').getStyle('margin-top');
		$('tech_link').setStyle('margin-top', 450);
		new Fx.Tween('tech_link' , {property:'margin-top', duration:1300}).start(org);
		
		org = $('productiontxt-flexcroll-vscroller').getStyle('margin-top');
		$('productiontxt-flexcroll-vscroller').setStyle('margin-top', 450);
		new Fx.Tween('productiontxt-flexcroll-vscroller' , {property:'margin-top', duration:1000}).start(org);
		
		var line = $$("#production .content .line")[0];
		org = line.getStyle('margin-top');
		line.setStyle('margin-top', 550);
		new Fx.Tween(line , {property:'margin-top', duration:1400}).start(org);
		
	}
}

window.addEvent('resize', reposition);
var map;

function reposition () {
	
	var center = new Object();
	center.x = jQuery(window).width()/2;
	center.y = jQuery(window).height()/2;
		
	var topRight = new Object();
	topRight.x = 0;
	topRight.y = 0;
		
	if (jQuery('.line')) {
		jQuery('.line').rotate(90-angle(center,topRight));
	}
	
	if ($('tiles')) {
		
		$('tiles').setStyle('width', window.getSize().x/2);
		$('tiles').setStyle('height', window.getSize().y-(window.getSize().y-700)/2);
		$('tiles').setStyle('margin-left', -window.getSize().x/2);
		$$('.tile').setStyle('width', window.getSize().x/2);
		$$('.tile').setStyle('height', window.getSize().y-(window.getSize().y-700)/2);
		$$('.tile_description').setStyle('margin-top', window.getSize().y-(window.getSize().y-700)/2-250);
		$('close_tile').setStyle('margin-left', window.getSize().x/2-150);
		$('close_tile').setStyle('margin-top', 10);
	
		if(tilesOpen) {
			$('ceramics').setStyle('margin-left', window.getSize().x/2);
		}
	}
	
	$$('.page').setStyle('height', window.getSize().y);
	
	for (var i = 0; i<$$('.content').length; i++) {
		
		var content = $$('.content')[i];
		var page = $$('.page')[i];
		
		if (page != $(currentPage)) {
			page.setStyle('top', window.getSize().y);	
		}
		if (isiOS) {
			page.setStyle('top', 0);
		}
		
		
		
		if (content.getElementById('line')) {
			var line = content.getElementById('line');
			if (Browser.ie){
				line.setStyle('top', content.getSize().y/2);
			}else{
				line.setStyle('margin-top', content.getSize().y/2);
			}
			line.setStyle('margin-left', -(2500-window.getSize().x)/2);
		}
		
		if (content.getSize().y<window.getSize().y) {
			content.setStyle('margin-top', (window.getSize().y-content.getSize().y)/2);
			content.setStyle('margin-left', (window.getSize().x-content.getSize().x)/2);
		}
		
		page.setStyle('display', 'block');
	
	}
	if (currentPage=='home') {
		$('menu').setStyle('top', window.getSize().y/2+15);	
	}
	
	if (isiOS) {
		if (!$('home')) {
			$('menu').setStyle('top', 0);	
			$('menu').setStyle('background-color','#f0efee');
		} else {
			$('menu').setStyle('top', window.getSize().y/2+15);	
		}
	}
	
	$('menu').setStyle('left', (window.getSize().x-$('menu').getSize().x)/2);
	
	if ($('map')) {
	
		$('map').setStyle('margin-top', -($('map').getSize().y-window.getSize().y)/2);
		map.setCenter(new GLatLng(55.824749,37.429721,1), 16);
	
		$('contacts_info').setStyle('margin-top', -750-$('contacts_info').getSize().y);
		$('contacts_info').setStyle('margin-left', window.getSize().x/2-$('contacts_info').getSize().x/2);
		
	}
}
