Cufon.replace('#navMenu a');
	$(function() {
    	//Reset all form input controls on page refresh.											 
			$(':input', 'form').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');											 
			//implement the the flowing highlight for the main menu.											 
        	var $menu = $('#navMenu');
			var $selected = $menu.find('li.current');
			
			var $moving		= $('<li />',{
				className : 'move',
				top	: $selected[0].offsetTop - 6 + 'px',
				width : $selected[0].offsetWidth + 'px'
			});
			
			
			$menu.bind('mouseleave',function(){moveTo($selected,400);})
			 	.append($moving)
			 	.find('li')
			 	.not('.move')
			 	.bind('mouseenter',function(){
					var $this = $(this);
					var offsetLeft = $this.offset().left - 20;
					moveTo($this,400);
			 	}) .bind('mouseleave',function(){
					var $this = $(this);
					var offsetLeft = $this.offset().left - 20;});

			function moveTo($elem,speed){
					$moving.stop(true).animate({
					top		: $elem[0].offsetTop - 6 + 'px',
					width	: $elem[0].offsetWidth + 'px'
				}, speed, 'easeOutExpo');
			}

			$.easing.elasout = function(x, t, b, c, d) {
 				var s=1.70158;var p=0;var a=c;
 				if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
 				if (a < Math.abs(c)) { a=c; var s=p/4; }
 				else var s = p/(2*Math.PI) * Math.asin (c/a);
 				return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
			 }; 
			
			$menu.hover(
				function (e) {
	                	$menu.stop().animate({'opacity':'1'},500);
	        	}, function (e) {
	                	$menu.stop().animate({'opacity':'0.1'},500);
	        	}		
			);
			
			
			$('#navMenu a').bind('click', function(event) {
				var $anchor = $(this);
				$posid = $anchor.attr('href');
				$selected = $anchor.parents('li');
				if (!$(this).hasClass('alt')){
					$.bbq.pushState({ url: $anchor.attr('href') });
					windowSlider($posid);
				}
				event.preventDefault();
			});
			
			$('div.page a.anchor-zero').click(function(){ windowSlider('#home'); });
			
			$('#talks-suggestion a').live('click', function() {
				windowSlider('#contact');
			});
			
			rebindform();
			
			//for the form label toggle
			$('#contact-box').find('.cinp').focus(function () {
				$(this).addClass('contact-input-focus');
				if(this.value == this.defaultValue){
					this.value = '';
				}
				if(this.value != this.defaultValue){
					this.select();
				}
			});
			$('#contact-box').find('.cinp').blur(function () {
				$(this).removeClass('contact-input-focus');	
				if($.trim(this.value) == '') {
					this.value = (this.defaultValue ? this.defaultValue : '');	
				}
			});
			
			
			//bind the ajax links 
			$('a.ajax').live('click', function(event){ 
				if ($(this).hasClass('alt')){
					$href = $(this).attr('rel') + '.php';
				} else {
					$href = $(this).attr('href');
				}
				$.bbq.pushState({ url: $href });
				//getDynamicContent($href);
				event.preventDefault();
			});
			
			
			$(window).bind( "hashchange", function(event) {
				event.preventDefault();
   				$url = event.getState( "url" );
				if($url == '#contact') {
					windowSlider('#contact');
				} else if ($url == '#events') {
					windowSlider('#events');
				} else if ($url == '#about') {
					windowSlider('#about');
				} else if ($url == 'talks.php' || $url == 'media.php' || $url == 'home.php') {
					getDynamicContent($url);
					windowSlider('#home');
					if ($url == 'media.php'){$('#home').removeClass('background');}else{$('#home').addClass('background');}
				//} else if ($url.split("?")[0] == 'video.php') {
				//	$.getScript($url, function() {
  				//		alert('Load was performed.');
				//	});
				} else {
					getDynamicContent($url);
				}
  			});
			
			
		$('#literature-comments-link').live('click', function () {
			$('#article').hide();
			$('#article-comments').show();
			$('#literature .comments-link').hide();
			$('#literature .back-link').show();
		});
		$('#literature-back-link').live('click', function () {	
			$('#article-comments').hide();
			$('#article').show();
			$('#literature .back-link').hide();
			$('#literature .comments-link').show();
		});	
			
			
	    //});
		
		$(window).trigger( "hashchange" );
		
		$('a.comment-reply').live('click', function () {
			$topicid = $('#content').attr('topicid');
			$replyParent = $(this).parents('div.talks-dialog');
			$('#reply-box').remove();
			$usernames = $replyParent.find('h3.usernames').html();
			$postid = $replyParent.attr('id');	
			$("<div id='reply-box' class='respond'><h3>Reply " + $usernames + "</h3><p class='desc'>Make sure you enter the <span class='required'>* required</span> information where indicated. Comments are moderated.</p><form id='replyform' method='post' action='comments-post.php'><div class='user-info-box'><p class='input-half overstyle'><label for='User'>Name <span class='required'>*</span></label><input id='User' class='{required:true,minlength:5,maxlength:50} input' type='text' tabindex='1' name='txtuser' /></p><p class='input-half overstyle'><label for='Email'>Email <span class='required'>*</span></label><input id='Email' class='{required:true,email:true} input' type='text' tabindex='2' name='txtemail' /></p><div class='clear'></div></div><div id='message-box' class='message-box overstyle'><label for='Comment'>Message <span class='required'>*</span></label><textarea class='{required:true,minlength:5,maxlength:1000} input textarea' tabindex='3' rows='10' cols='0' id='Comment' name='txtcomment'></textarea></div><div class='container' ></div><p id='submit-form'><input type='image' value='Reply' tabindex='4' id='btnReply'  name='btnreply' src='css/post-btn.png'><input type='hidden' id='TopicID' name='txttopicid' value='" + $topicid + "'><input type='hidden' id='PostID' name='txtpostid' value='" + $postid  + "'></p></form></div>")			
			.insertAfter($replyParent).fadeIn('fast', function() {
				rebindform();
			});
		});
		
		$('#btnPost').live('click', function () {
			$commentform = $('#commentform');
			$btn = $(this);
			var validator = $commentform.validate({errorLabelContainer: $('#commentform div.container')});
			if (validator.form()) {	
				$btn.hide().after('<div id="sending">Posting...</div>');
				$data = $commentform.serialize() + "&btnpost=post";
				$.ajax({
					type: "POST",
					url: "comments-post.php",
					dataType: "html",
					data: $data,
					success: function(msg){
						$('#respond-box').fadeOut('slow', function () { 
								$("<div class='sent-message' style='display:none;'><p class='comment-recieved'></p></div>").insertAfter($('#respond-box')).fadeIn('slow').delay(8000).fadeOut('slow', function () { 
								$(this).remove();
								$('#respond-box').fadeIn('slow');	
								$commentform.find(':input').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected').trigger('blur');																																				 						});
						});
					},
					complete: function() {
						$('#sending').remove();
						$btn.show();
					}
				});
			}
			return false;
		});
		$('#btnReply').live('click', function () {
			$btn = $(this);
			var validator = $('#replyform').validate({errorLabelContainer: $('#commentform div.container')});
			if (validator.form()) {	
				$btn.hide().after('<div id="sending">Posting...</div>');
				$data = $('#replyform').serialize() + "&btnreply=post";								   
				$.ajax({
					type: "POST",
					url: "comments-post.php",
					dataType: "html",
					data: $data,
					success: function(msg){
						$('#reply-box').fadeOut('slow', function () { 
								$("<div class='sent-message' style='display:none;'><p class='comment-recieved'></p></div>").insertAfter($('#reply-box')).fadeIn('slow').delay(12000).fadeOut('slow', function () { 
								$(this).remove();	
								$('#reply-box').remove();																																				 						});
						});
					},
					complete: function() {
						$('#sending').remove();
						$btn.show();
					}
				});
			}
			return false;
		});
		$('#btnSendMessage').live('click', function () {
			$contactform = $('#contact-form');
			$btn = $(this);
			var validator = $contactform.validate({errorLabelContainer: $('#contact-form div#contacterror')});
			if (validator.form()) {	
					$btn.hide().after('<div id="sending">Sending...</div>');
					$data = $contactform.serialize() +  "&btnsendmessage=post";
					$.ajax({
						type: "POST",
						url: "contactus.php",
						dataType: "html",
						data: $data,
						success: function(msg) {
							if(msg != "error") {
								$('#contact-form').fadeOut('slow', function () { 
										$("<div style='display:none;'><p class='message-sent'></p></div>").insertAfter($('#contact-box')).fadeIn('slow').delay(8000).fadeOut('slow', function () { 
										$(this).remove();
										$contactform.fadeIn('slow').find(':input').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected').trigger('blur');																																					 						});
								});
							} else { alert("Humans only please!!!"); }
						},
						error: function(msg) {},
						complete: function() {
							$('#sending').remove();
							$btn.show();
						}
					});
			}
			return false;
		});
		$('#btnSubmit').live('click', function () {
			$commentform1 = $('#commentform');
			$btn = $(this);
			var validator = $commentform1.validate({errorLabelContainer: $('#commentform div.container')});
			if (validator.form()) {	
				$btn.hide().after('<div id="sending">Posting...</div>');
				$data = $commentform1.serialize() + "&btnsubmit=post";
				$.ajax({
					type: "POST",
					url: "media-comments.php",
					dataType: "html",
					data: $data,
					success: function(msg){
						$('#respond-box').fadeOut('slow', function () { 
							$("<div class='sent-message' style='display:none;'><p class='comment-recieved'></p></div>").insertAfter($('#respond-box')).fadeIn('slow').delay(8000).fadeOut('slow', function () { 
								$(this).remove();
								$('#respond-box').fadeIn('slow');	
								$commentform1.find(':input').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected').trigger('blur');																																		 							});
						});
					},
					complete: function() {
						$('#sending').remove();
						$btn.show();
					}
				});
				return false;
			}
			return false;
		});
		
		
		//$(window).live('ajaxSuccess', function () {
			//
		//});
		
		$('#lyrics-box.collapse .toggle-lyrics').live('click', function () {
				$box = $(this).parents('#lyrics-box');
				$box.removeClass('collapse').addClass('expand');
				$(this).prev('.lyrics').show();
				$(this).find('span').html("hide lyrics");
		} );
		
		$('#lyrics-box.expand .toggle-lyrics').live('click', function () {
				$box = $(this).parents('#lyrics-box');
				$box.removeClass('expand').addClass('collapse');
				$(this).prev('.lyrics').hide();		
				$(this).find('span').html("show lyrics");											
		} );
		
		var tabContents = $('div.tab-container > div');
		$('ul.tabs a').live('click', function () {
        	tabContents.hide().filter(this.hash).show();
        	$('ul.tabs li').removeClass('active');
        	$(this).parents('li').addClass('active');
        	return false;
    	}).filter(':first').click();
		
		//preload images
		preloader();
		
	});
	//jquery onload function ends here!
	
	function windowSlider($posid) {
		$.scrollTo('#main-mask', 200, {
				   queue:true,
				   axis:'xy',
				   easing:'easeInOutQuart',
				   onAfter:function(){
						   $scrollpane = $('#main-mask');
							$scrollpane.stop().scrollTo($posid, 1000, {
									   axis:'x',
									   easing:'easeInOutQuart'
							});
				   }
		});
		//var $scrollTop = $(window).scrollTop();
		//if ($scrollTop != 0) {
			//$('html, body').stop().animate({ scrollTop: $($posid).offset().top }, 1500,'easeInOutExpo')
				//.animate({ scrollLeft: $($posid).offset().left}, 1500, 'easeInOutExpo');
		//} else { 
			//$('html, body').stop().animate({ scrollLeft: $($posid).offset().left}, 1500, 'easeInOutExpo');	
		//}
	}
	
	
	function rebindings() {
			var nivoSlider= $('#gallary-slider').nivoSlider({
				afterChange: function () {
					$num = $('#gallary-slider').find('.nivo-control.active').attr('rel');
					$('#slider-right .slide-info').hide();
					$('#slider-right').find('.slide-info:eq('+ $num + ')').fadeIn('slow');
				}
			});
			$('#slider-right').find('.slide-info:eq(0)').fadeIn('slow');
			
		
	}
	
	function rebindform() {
				
	//form label style			
			var $box = $('form .overstyle').addClass('overlabel');
			var $boxinput = $box.find('input, textarea');
			var $boxlabel = $box.find('label');
			if ($boxinput.val()) {
				$boxlabel.hide();	
			}
			$boxinput.live('focus', function() {
				$(this).prev().hide()						 
			}).live('blur', function () {
				if (this.value == '') {
					$(this).prev().show();	
				}
			});
			$boxlabel.live('click', function () {
				$(this).next().trigger('focus');							  
			});	
			
			
			
			
	}
	
	function getDynamicContent($url) {
		$('#pd_loading').show();
		$('div#dynamic-content').load($url + ' #content', {ajax: true}, function (responseText, status, XMLHttpRequest) {
				if (status != "error") {
					document.title = "TCSquared | " + $('#content').attr('pagetitle');
					$('#pd_loading').hide();
					rebindings();
					photoGallery();
					rebindform();
					if ($url == 'home.php') {
						AC_FL_RunContent(
							'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
							'width', '100%',
							'height', '100%',
							'src', 'piecemaker',
							'quality', 'high',
							'pluginspage', 'http://www.adobe.com/go/getflashplayer_de',
							'align', 'middle',
							'play', 'true',
							'loop', 'true',
							'scale', 'noscale',
							'wmode', 'transparent',
							'devicefont', 'false',
							'id', 'piecemaker',
							'name', 'piecemaker',
							'menu', 'true',
							'allowFullScreen', 'false',
							'allowScriptAccess','sameDomain',
							'movie', 'piecemaker',
							'salign', ''
						); //end AC code
					}
				}
				if (status = "error") {
					$('#pd_loading').hide();
				}
		});
	}
	
	function preloader() {
		 // counter
		 var i = 0;
		 // create object
		 imageObj = new Image();
		 // set image list
		 images = new Array();
		 images[0]="css/message-sent-bg.png"
		 images[1]="css/comment-recieved-bg.png"
		 images[2]="css/paperball.png"
		 images[3]="css/loading.gif"
		 // start preloading
		 for(i=0; i<=3; i++) 
		 {
			  imageObj.src=images[i];
		 }
	} 
