		var $window = $(window),
			$header = $("#header"),
			$wrap = $("#wrap"),
			$contentWrap = $("#contentWrap"),
			$main = $("#main"),
			$links = $("#main-nav a"),
			$linksLi = $("#main-nav li"),
			$songsLinks = $(".songs a");
		if(!($.browser.msie)) { //&& $.browser.version <= 9
			$window.scroll(function() { 
				if($window.scrollTop() > 450) {
					$header.css({ position: 'fixed', top: '-450px' });
					$wrap.css({ marginTop: '450px', position: 'relative', top: '0'});
				} else {
					$wrap.css({ marginTop: '-147px', position: 'relative', top: '0'});
					$header.css({ position: 'relative' ,top:'0' });
				}
			});
		}
		var playingSong;
		var update = function() {
			Cufon.refresh();
			var $relese = $(".releseInfo"),
					$artist = $(".artist"),
					$songsLinks = $(".songs a");
			$artist.find(".artistMore").each(function () {
					var $this = $(this);
					$this.data("realHeight" , $(this).outerHeight(true));
			});
			$relese.parent().find(".releaseMore").hide();
			$relese.parent().removeClass("active");
			$artist.parent().find(".artistMore").hide();
			$artist.parent().removeClass("artistActive");
		
			$artist.find('.artistImgWrap').click(function() {
				var $this =	$(this).parent(),
						$artistMore = $this.find(".artistMore"),
						height = $artistMore.data("realHeight");
				if(!$this.hasClass("artistActive")) {
					$artist.removeClass("artistActive").find(".artistMore").each( function() { 
						$(this).stop().animate({ height: 0 }, 'slow' ,function() { $(this).hide() } );
					});
					$this.addClass("artistActive");
					$artistMore.show().stop().animate({ height: height }, 'slow' );
				} else {
					$artistMore.stop().animate({ height: 0 }, 'slow' , function() { 
						$(this).hide(); 
						$this.removeClass("artistActive");
					});
				}
			});
			$relese.click(function() {
				var $this = $(this).parent(),
						$releaseMore = $this.find(".releaseMore");
				if($this.hasClass("active")) {
					$this.find(".releaseMore").slideUp('slow',function() { $this.removeClass("active"); });
				} else {
					$this.addClass("active");
					$releaseMore.slideDown('slow');
				}
			});
			
			// Contact page 
			$(".errors").hide();
			var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			
				// Booking 
				var $bookingSubmit = $("#bookingSubmit"),
						$bookingForm = $("#bookingForm"),
						$bookingErrors = $("#bookingErrors"),
						booking = [
						$("#bookingArtist"), //0
						$("#bookingDj"), //1
						$("#bookingMonth"), //2
						$("#bookingDay"), //3
						$("#bookingYear"), //4
						$("#bookingContact"), //5
						$("#bookingLocation"), //6
						$("#bookingEmail"), //7
						$("#bookingMsg") //8
						], 
						bError = new Array(),
						bErrors = false;
				// on booking form submit 
				$bookingSubmit.click( function(e) {
					// artist
					if((booking[0].val() === "none" && booking[1].val() === "none") || (booking[0].val() !== "none" && booking[1].val() !== "none")) {
						booking[0].css("outline","1px solid #ff0000");
						booking[1].css("outline","1px solid #ff0000");
						bError[0] = "You need to select one artist.";
					} else {
						booking[0].css("outline","none");
						booking[1].css("outline","none");
						bError[0] = "";
					}
					// date
					cDate = new Date();
					bDate = new Date(booking[4].val(),booking[2].val()-1,booking[3].val());
					if(cDate > bDate) {
						booking[4].css("outline","1px solid #ff0000");
						booking[2].css("outline","1px solid #ff0000");
						booking[3].css("outline","1px solid #ff0000");
						bError[1] = "Date need to be greater than current time";
					}else{
						booking[4].css("outline","none");
						booking[2].css("outline","none");
						booking[3].css("outline","none");
						bError[1] = "";
					}
					// contact 
					if(booking[5].val() === "") {
						booking[5].css("outline","1px solid #ff0000");
						bError[2] = "Contact field should be filled";
					} else {
						booking[5].css("outline","none");
						bError[2] = "";
					}
					// location 
					if(booking[6].val() === "") {
						booking[6].css("outline","1px solid #ff0000");
						bError[3] = "Location field should be filled";
					} else {
						booking[6].css("outline","none");
						bError[3] = "";
					}
					// email 
					if(booking[7].val() === "" || !emailReg.test(booking[7].val())) {
						booking[7].css("outline","1px solid #ff0000");
						bError[4] = "Your email is not valid";
					} else {
						booking[7].css("outline","none");
						bError[4] = "";
					}
					// msg 
					if(booking[8].val() === "") {
						booking[8].css("outline","1px solid #ff0000");
						bError[5] = "Message field should be filled";
					} else {
						booking[8].css("outline","none");
						bError[5] = "";
					}
					// check for errors
					for(var i=0;i<bError.length;i++) {
						if(bError[i] !== "") { bErrors = true; break; }
						bErrors = false;
					}
					
					if(bErrors === true) {
						// display errors
						var bErrorHtml = "<li><h3>Errors</h3></li>" 
						$bookingErrors.show();
						for(var i=0;i<bError.length;i++) {
							if(bError[i] !== "") {
								bErrorHtml += "<li>*" + bError[i] + "</li>";
							}
						}
						$bookingErrors.html(bErrorHtml);
					} else {
						// no errors submit form using ajax
						$bookingErrors.hide();
						var dataString = "";
						for(var i=0;i<booking.length;i++) {
							dataString += booking[i].attr("name")+ "=" + booking[i].val() + "&";
						}
						dataString += "form=booking";
						
						$.ajax({
							type: "POST",
							url: $bookingForm.attr("action"),
							data: dataString,
							success: function(res) {
								res = $.parseJSON(res);
								
								if(res.sucess == 1) {
									$bookingForm.hide();
									$bookingForm.html("<div class='message'></div>").find(".message")
									.html("<h4>Booking Form Submitted!</h4>")
									.append("<p>We will be in touch soon.</p>");
									$bookingForm.fadeIn('slow');
								}
							}
						});
					}
					return false;
				});
				// Question
				var $questionSubmit = $("#questionSubmit"),
					$questionForm = $("#questionForm"),
					$questionErrors = $("#questionErrors"),
					question = [
					$("#questionName"), //0
					$("#questionEmail"), //1
					$("#questionMsg") //2
					], 
					qError = new Array(),
					qErrors = false;
					
				// on question submit 
				$questionSubmit.click(function() {
					
					// name 
					if(question[0].val() === "") {
						question[0].css("outline","1px solid #ff0000");
						qError[0] = "Name field should be filled";
					} else {
						question[0].css("outline","none");
						qError[0] = "";
					}
					// email 
					if(question[1].val() === "" || !emailReg.test(question[1].val())) {
						question[1].css("outline","1px solid #ff0000");
						qError[1] = "Your email is not valid";
					} else {
						question[1].css("outline","none");
						qError[1] = "";
					}
					// msg 
					if(question[2].val() === "") {
						question[2].css("outline","1px solid #ff0000");
						qError[2] = "Message field should be filled";
					} else {
						question[2].css("outline","none");
						qError[2] = "";
					}
					
					// check for errors
					for(var i=0;i<qError.length;i++) {
						if(qError[i] !== "") { qErrors = true; break; }
						qErrors = false;
					}
					
					
					if(qErrors === true) {
						// display errors
						var qErrorHtml = "<li><h3>Errors</h3></li>" 
						$questionErrors.show();
						for(var i=0;i<qError.length;i++) {
							if(qError[i] !== "") {
								qErrorHtml += "<li>*" + qError[i] + "</li>";
							}
						}
						$questionErrors.html(qErrorHtml);
					} else {
						// no errors submit form using ajax
						$questionErrors.hide();
						var dataString = "";
						for(var i=0;i<question.length;i++) {
							dataString += question[i].attr("name")+ "=" + question[i].val() + "&";
						}
						dataString += "form=question";
						
						$.ajax({
							type: "POST",
							url: $questionForm.attr("action"),
							data: dataString,
							success: function(res) {
								res = $.parseJSON(res);
								
								if(res.sucess == 1) {
									$questionForm.hide();
									$questionForm.html("<div class='message'></div>").find(".message")
									.html("<h4>Your message is submitted!</h4>")
									.append("<p>We will be in touch soon.</p>");
									$questionForm.fadeIn('slow');
								}
							}
						});
					}
					return false;
				});

					// News
				var $newsSubmit = $("#newsSubmit"),
					$newsForm = $("#newsForm"),
					$newsErrors = $("#newsErrors"),
					news = [
					$("#newsGreeting"), //0
					$("#newsEmail"), //1
					$("#newsCaptcha") //2
					], 
					nError = new Array(),
					nErrors = false;
					
					
					
					// on news submit 
				$newsSubmit.click(function() {
					
					// email 
					if(news[1].val() === "" || !emailReg.test(news[1].val())) {
						news[1].css("outline","1px solid #ff0000");
						nError[1] = "Your email is not valid";
					} else {
						news[1].css("outline","none");
						nError[1] = "";
					}
					// msg 
					if(news[2].val() === "") {
						news[2].css("outline","1px solid #ff0000");
						nError[2] = "Security code field should be filled";
					} else {
						news[2].css("outline","none");
						nError[2] = "";
					}
					// check for errors
					for(var i=1;i<nError.length;i++) {
						if(nError[i] !== "") { nErrors = true; break; }
						nErrors = false;
					}
					if(nErrors === true) {
						// display errors
						var nErrorHtml = "<li><h3>Errors</h3></li>" 
						for(var i=1;i<nError.length;i++) {
							if(nError[i] !== "") {
								nErrorHtml += "<li>*" + nError[i] + "</li>";
							}
						}
						$newsErrors.html(nErrorHtml);
						$newsErrors.show();
					} else {
						// no errors submit form using ajax
						$newsErrors.hide();
						var dataString = "";
						for(var i=0;i<news.length;i++) {
							dataString += news[i].attr("name")+ "=" + news[i].val() + "&";
						}
						dataString += "form=news";
						$.ajax({
							type: "POST",
							url: $newsForm.attr("action"),
							data: dataString,
							success: function(res) {
								res = $.parseJSON(res);
								if(res.sucess == 1) {
									$newsForm.hide();
									$newsForm.html("<div class='message'></div>").find(".message")
									.html("<h4>You are now subscibed to our mailing list!</h4>");
									$newsForm.fadeIn('slow');
								} else {
									var nErrorHtml = "<li><h3>Errors</h3></li><li>Security code is wrong!</li>" 
										$newsErrors.show();
										$newsErrors.html(nErrorHtml);
										$("#captchaImg img").remove();
										$("#captchaImg").append('<img src="/wp-content/contact/captcha.php" alt="captcha" />');
								}
							}
						});
					}
					
					return false;
				});
					
		};
		var hash = window.location.hash;
		switch(hash) {
			case '#about':
			case '#releases':
			case '#artists':
			case '#media':
			case '#contact':
				$links.removeClass('active');
				$("#main-nav " + hash.replace("#",".")+" a").addClass('active');
				$contentWrap.text("");
				$('<div class="ajaxSpinner"></div>').appendTo("#contentWrap");
				$main.load(hash.replace("#","/") + ' #contentWrap' , update );
			break;
		}
		update();
		
		var $lavalamp = $("#header").append('<div id="lavalamp"> </div>').find("#lavalamp").css({ left: $(".active").position().left + 'px'}).addClass($(".active").parent()[0].className + 'Lava');
			
		
			$links.find(".tesseract").css('opacity',0);
			$("#main-nav .active").find(".tesseract").css('opacity',1);
			$links.hover(function() {
					$lavalamp.stop().animate({ left: $(this).position().left + 'px'},300);
					if(!$(this).hasClass('active')) {
					$(this).find(".tesseract").css('opacity',0).animate({"opacity": 1},'slow');
					}
			},function() {
					$lavalamp.stop().animate({ left: $(".active").position().left + 'px'},300);
					if(!$(this).hasClass('active')) {
					$(this).find(".tesseract").stop(true,true).animate({"opacity": 0},'slow');
					}
			});
		$links.click(function() {
				var $this = $(this);
				hash = $this.attr("href").replace("/", "");
				if(hash != "") { 
					window.location.hash = hash;
				} else {
					window.location.hash = "#news";
				}
				$links.removeClass('active').find(".tesseract").css('opacity',0);
				$this.addClass('active').find(".tesseract").css('opacity',1);
				$lavalamp.stop().animate({ left: $this.position().left + 'px'},300).removeClass().addClass($(this).parent()[0].className + 'Lava');
				$("#contentWrap").text("");
				$('<div class="ajaxSpinner"></div>').appendTo("#contentWrap");
				$main.load($this.attr('href') + ' #contentWrap' , update );
				if(playingSong !== undefined) {
					soundManager.stop('song');
					soundManager.destroySound('song');
				}
				return false;
		});
		
		soundManager.debugMode = false;
		soundManager.url = '/wp-content/themes/tesseract/js/swf/';
		soundManager.onload = function() {
			
			
			$songsLinks.live('click' , function() {
				var $this = $(this),
						songUrl = $this.attr("href");
				
				if(songUrl == playingSong) {
					soundManager.stop('song');
					soundManager.destroySound('song');
					$this.removeClass("play");
					playingSong = undefined;
					$this.attr("title", "Play Song");
				} else {
				$songsLinks = $('.songs a');
				$songsLinks.removeClass('play');
				$songsLinks.attr("title", "Play Song");
				$this.addClass("play");
				$this.attr("title", "Stop Song");
				playingSong = songUrl;
				soundManager.stop('song');
				soundManager.destroySound('song');
				soundManager.createSound('song',songUrl);
				soundManager.play('song');
				}
				return false;
			});
		};
		$("#social li a").css({opacity: 0});
		$("#social li a").hover(function() {
			$(this).stop(true,true).animate({opacity: 1},'slow');
		}, function() {
			$(this).stop(true,true).animate({opacity: 0},'slow');
		});
		

