window.addEvent('domready', function() {
	
	//initialize
	window.collection = '';
	
	setTitle('Blog');
	start_hashlistener();
	
	// logo hover displays ape
	$('logo').addEvents({
		mouseover: function() {
			new Fx.Tween($('ape')).start('opacity', 0, 1);
		},
		mouseout: function() {
			new Fx.Tween($('ape')).start('opacity', 1, 0);
		}
	});
	
	// actions for grouplinks
	$$('.grouplink').each(function(e) {
		$(e).addEvents({
			click: function(event) {
				$(e).toggleClass('white');
				$(e).getParent().getElement('.sub').toggleClass('collapsed');
				event.preventDefault();
			}
		});
	});
});

//DEFINE FUNCTIONS
// set page title
function setTitle(title) {
	document.title = 'Rodriguez Fazanatas\u2009—\u2009'+title;
}

// navigation
function killActive() {
	$$('.active').each(function(e) {
		$(e).toggleClass('active');
	});
}

// swap images (used by BLOG)
function swapImage(img_id) {
	$(img_id).setStyle('opacity', 0.01)
	$(img_id).setProperty('src', image_set[i]);
	new Fx.Tween($(img_id)).start('opacity', 0, 1);
};

// load pages via ajax
function loadHTML(target) {
	var req = new Request.JSON({
		url:target, 
		method: 'get',
		onSuccess: function(responseJSON) {
			$('content').set('html', responseJSON.body);
			if(target.indexOf('pret') != -1) {
				window.image_set =
					JSON.decode(responseJSON.image_set).images;
				initCollections();
			} else if(target.indexOf('blog') != -1) {
				initBlog();
			} else if(target.indexOf('bespoke') != -1) {
				initBespoke();
				window.bespoke_galleries =
					JSON.decode(responseJSON.bespoke_galleries);
			} else if(target.indexOf('freunde') != -1) {
				initFriends();
			};
		},
		onFailure: function(xhr,responseText) {
			alert('hello. failed to load HTML. hanging here …');
		}
	});	
	req.send();
}

// PRÈT-A-PORTÈR
function initCollections() {
	setTitle('Prêt-à-porter');
	i = 0;
	var image_set = window.image_set;
	$('collectioncontent').getElement('img').setProperty('src', image_set[i]);
	
	// slideshow	
	var refreshSlideshow = function() {
		$('image').setStyle('opacity', 0.001);
		$('image').setProperty('src', image_set[i]);
		//fadeImage.start(0.001, 1);
		new Fx.Tween($('image')).start('opacity', 0, 1);
	}
	
	var fadeLeft = new Fx.Tween('leftarrow', {
		property: 'opacity',
		duration: 150
	});
	
	var fadeRight = new Fx.Tween('rightarrow', {
		property: 'opacity',
		duration: 150
	});
	
	$('leftarrow').addEvents({
		mouseover: function(e) {
			if(i>0) {
			this.setStyle('opacity', 1);
				//fadeLeft.start(0, 1);	
			};
		},
		mouseout: function() {
			this.setStyle('opacity', 0.001);
			//fadeLeft.start(0.0001);
		},
		click: function() {
			if(image_set.length>1) {
				if(i>0) {
					i--;
					refreshSlideshow('image');
				};
			};
			if(i==0) {
				$('leftarrow').setStyle('visibility', 'hidden');
			};
			return false;
		}
	});
	
	$('rightarrow').addEvents({
		mouseover: function() {
			if(image_set.length > 1 && i<image_set.length-1) {
				this.setStyle('opacity', 1);
				//fadeRight.start(0, 1);
			};
		},
		mouseout: function() {
			this.setStyle('opacity', 0.001);
			//fadeRight.start(0.0001);
		},
		click: function() {
			if(image_set.length>1) {
				if(i<image_set.length-1) {
					i++;
					refreshSlideshow('image');
				};
			};
			if(i == image_set.length-1) {
				$('rightarrow').setStyle('visibility', 'hidden');
			};
			return false;
		}
	});

	var showInfo = new Fx.Tween($('info'), {
		property: 'height',
		duration: 250
	});

	var hideInfo = new Fx.Tween($('info'), {
		property: 'height',
		duration: 250
	});
	
	$('showinfo').getElement('a').addEvent('click', function() {
		if($('showinfo').getElement('a').get('html') == 'Show info') {
			$('showinfo').getElement('a').set('html', 'Hide info');
			showInfo.start('570px');
		} else {
			$('showinfo').getElement('a').set('html', 'Show info');
			showInfo.start('0px');
		};
		return false;
	});
}

// BESPOKE
function initBespoke() {
	setTitle('Bespoke');
	
	//functions
	var toggleOrderInfo = new Fx.Tween($('orderinfo'), {
		property: 'opacity',
		duration: 250
	});
	// SHOW ORDERINFO
	$$('.price').each(function(e) {
		e.addEvents({
			mouseover: function() {
				toggleOrderInfo.start('0', '1');
			},
			mouseleave: function() {
				toggleOrderInfo.start('0');
			},
			mousemove: function(event) {
				$('orderinfo').setStyles({
					'top': event.page.y-160,
					'left': event.page.x-285
				});
			},
			click: function() {
				return false;
			}
		});
	});
	// ZOOM IMAGES
	$$('.image').each(function(e) {
		e.addEvents({
			click: function() {
				image_set = bespoke_galleries[this.get('id')].thumbs;
				if(image_set.length>1) {
					i = bespoke_galleries[this.get('id')].index;
					len = image_set.length;
					if(i < len - 1) {
						i++;
					} else if(len > 1 && i >= len - 1) {
						i = 0;
					};
					bespoke_galleries[this.get('id')].index = i;
					swapImage(this.getChildren()[0].get('id'));
				};
				return false;
			}
		});
	});
}

// FRIENDS
function initFriends() {
	setTitle('Freunde');
	$$('.name').getElement('a').each(function(e) {
		$(e).addEvents({
			mouseover: function() {
				new Fx.Tween($(e).getParent().getParent().getElement('.details')).start('height', '119px');
			},
			click: function() {
				return false;
			}
		});
	});
}

// BLOG
function initBlog() {
	setTitle('Blog');
	//create the Accordion instance
	var myAccordion = new Accordion($('accordion'), 'h1.toggler', 'div.txt_and_img', {
		opacity: false,
		display: -1,
		alwaysHide: true,
		duration: 250
	});
	// set actions for togglers (headline .h1)
	$$('.toggler').each(function(e) {
		e.addEvents({
			mouseover: function() {
				this.addClass('black');
				$(e).getElement('.preview').setStyle('visibility', 'visible');
			},
			mouseout: function() {
				this.removeClass('black');
				$(e).getElement('.preview').setStyle('visibility', 'hidden');
			},
			mousemove: function(event) {
				$(e).getElement('.preview').setStyles({
					'top': event.page.y-85,
					'left': event.page.x-175
				});
			},
			click: function() {
				i = 0;
				var req = new Request.JSON({
					url:'/getpostimages/'+$(e).get('name'), 
					method: 'get',
					onSuccess: function(responseJSON) {
						image_set = responseJSON.images;
						$(e).getParent().getParent().getElement('.blogimage').setProperty('src', image_set[i]);
						$(e).getParent().getParent().getElement('.img_no').set('html', (i+1)+'/'+image_set.length);
					},
					onFailure: function(xhr,responseText) {
						alert('hello. failed to load images. hanging here …');
					}
				});
				req.get();
				return false;
			}
		});
	});
	// image switch
	$$('.blogimage').each(function(e) {
		e.addEvents({
			click: function() {
				if(image_set.length>1) {
					if(i<image_set.length-1) {
						i++;
					} else if(image_set.length>1 && i>=image_set.length-1) {
						i=0;
					};
					$(e).getParent().getParent().getElement('.img_no').set('html', (i+1)+'/'+image_set.length);
					swapImage($(e).get('id'));
				};
				return false;
			}
		});
	});
}

function start_hashlistener() {
    
    hl = new HashListener(blank_page='/media/interface/blank.html');
    
    hl.addEvent('hash-changed', function(new_hash){
        
        if(Boolean(new_hash)) {
		
		killActive();
		
		$$('.directlink').each(function(link){
			if(link.get('href') == '/#' + new_hash){
				link.toggleClass('active');
			}
		});
		
		$$('.collection').each(function(link){
			if(link.get('href') == '/#' + new_hash){
				link.toggleClass('active');
				collection = link.get('id');
				link.getParents('.sub').each(function(sub){
					sub.removeClass('collapsed');
					sub.getAllPrevious('.grouplink').each(function(grouplink){
						grouplink.addClass('white');
					})
				});
			}
		});
		
		loadHTML(new_hash);
        }
	else {
		loadHTML('/blog/');
	}

        
    });
    
    hl.start();
}

