window.addEvent('domready', function() {
	//VARIABLES
	var i = 1;
	var p = 0;
	var tooltips = new Tips($$('.tooltip'), {
		offsets: {
			'x': -5,       //default is 16
			'y': -50        //default is 16
		},
	});
	
	//SHOW BACK OF CARDS
	var showBack = function(a) {
		$(a).getElement('.back').set('opacity', 1);
	};
	
	var hideBack = function(a) {
		$(a).getElement('.back').set('opacity', 0);
	};
	
	$$('.sample').each(function(e) {
		$(e).getElement('.mousearea').addEvents({
			mouseover: function() {
				showBack(e.getProperty('id'));
			},
			mouseout: function() {
				hideBack(e.getProperty('id'));
			},
			click: function() {
				var link = e.getProperty('href');
				window.location = link;
			}
		});
	});
	
	// DO SHIT DEPENDING ON THE LOCATION
	var dd = window.location.href;
	
	// IF ITS THE SAMPLE DETAILS VIEW
	if(dd.indexOf('samples/') != -1) {
		$('e3').setStyle('visibility', 'hidden');
		$$('.sample_image').each(function(e) {
			e.addEvents({
				mouseover: function() {
					$('sample_details').setStyle('visibility', 'visible');
					$('e3').setStyle('visibility', 'visible');
				},
				mouseout: function() {
					$('sample_details').setStyle('visibility', 'hidden');
					$('e3').setStyle('visibility', 'hidden');
				},
				mousemove: function(event) {
					$('sample_details').setStyles({
						'top': event.page.y,
						'left': event.page.x
					});
				}
			});
		});
	
		$$('#hiddenbackground').addEvent('click', function (){
			window.location = '/';
		});
	}
	
	// IF ITS THE DOODLES PAGE
	if(dd.indexOf('doodles') != -1) {
		$$('#hiddenbackground').addEvent('click', function (){
			window.location = '/';
		});
	}
	
	// IF ITS THE HOME PAGE
	if(dd.indexOf('samples') == -1 && dd.indexOf('doodles') == -1) {
		$$('.doodle').each(function(e) {
			$(e).getElement('.mousearea').addEvents({
				mouseover: function() {
					showBack(e.getProperty('id'));
				},
				mouseout: function() {
					hideBack(e.getProperty('id'));
				},
				click: function() {
					var link = e.getProperty('href');
					window.location = link;
				}
			});
		});

		//SHOW/HIDE CONTACT FORM
		$('businesscard').getElement('.back').setStyle('opacity', 0);
		$('businesscard').getElement('.mousearea').addEvents({
			click: function(){
				showBack('businesscard');
				tooltips.hide();
			},
		});

		$('businesscard').getElement('#contactform').addEvent('mouseover', function() {
			tooltips.hide();
		});

		// CHANGE PHRASES ON PHRASECARDS
		var reqphrase = new Request.JSON({
			url: '/get_phrases',
			method: 'get',
			onSuccess: function(responseJSON) {
				phraseset = responseJSON;
				//alert(phraseset.length);
				$('phrasecard').addEvent('click', function() {
					p++;
					if(p >= phraseset.length) {
						p=0;
					}
					$('phrase').set('text', phraseset[p]);
				});
			},
			onFailure: function(xhr, responseText) {
				alert('this is not happening');
			}
		});
		reqphrase.get();

		//GET CONTACT FORM
		var reqform = new Request.JSON({
			url: 'contact', 
			method: 'get',
			onSuccess: function(json) {
				$('contactform').set('html', json.body);
				$$('form').each(function(form) {
					form.addEvent('submit', function() {
						sendForm(form);
						return false;
					});
				});
				$$('form input[type=text]').each(function(e) {
					var oldValue = e.value;
					e.addEvents({
						click: function(){
							if(this.value==oldValue) this.value='';
						},
						blur: function() {
							if(this.value=='') this.value=oldValue;
						}
					});
				});
				$('id_message').addEvents({
					click: function(){
						if(this.getProperty('text')=='Type your message here …') this.value='';
					},
				});
			
				$('back').addEvent('click', function() {
					hideBack('businesscard');
				});
			},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function(xhr,responseText) {
				alert('Systemfehler -2918: '+responseText);
			}
		});
		reqform.get();

		// SEND FORM
		function sendForm(form) {
			var req = new Request.JSON({
				url:form.get('action'),
				onSuccess: function(json, text) {
					$('contactform').set('html', json.body);
					$$('form').each(function(form) {
						form.addEvent('submit', function() {
							sendForm(form);
							return false;
						});
					});
					if($('thanks')) {
						(function(){ 
							hideBack('businesscard'); 
							reqform.get();
						}).delay(2000);
					} else {
						$('back').addEvent('click', function() {
							hideBack('businesscard');
						});
					}
				},
				data:form.toQueryString()
			});	
			req.send();
		}
	}
	
	
	//REQUEST SOUNDS FROM DJANGO AND SAVE THEM IN AN ARRAY (SOUNDSET)
	/*
	var req = new Request.JSON({
		url: '/get_soundset',
		method: 'get',
		onSuccess: function(responseJSON) {
			soundset = responseJSON;
			//ADD RANDOM SOUND TO EVERY ELEMENT WITH CLASS "FX"
			$$('.fx').each(function(e) {
				var sound_id = 'sound'+i;
				embedSound = new Element ('embed', {
					src: soundset[$random(0, soundset.length-1)],
					hidden: true,
					autostart: false,
					id: sound_id,
				});
				e.addEvent('mouseover', function(){
					playSound(sound_id);
				});
				embedSound.inject(e);
				i++;
			});
		},
		onFailure: function(xhr, responseText) {
			alert('this is not happening');
		}
	});
	req.get();
	*/
	
	//SOUND FUNCTION
	playSound = function (sound_id) {
		$(sound_id).Play();
	}
	
});

window.addEvent('load', function() {
	$$('.hidewhileloading').tween('opacity', 0, 1);
});
