$(document).ready(function(){
	
	$(function() {
		$("#navigation").tabs({ 
			spinner: "Loading..."
		});
	});
	
	$('a[title]').qtip({ 
		position: {
	      corner: {
	         target: 'topMiddle',
	         tooltip: 'bottomMiddle'
	      },
	      adjust: { x: 0, y: -30 }
	   },	
		style: { 
			name: 'dark',
			tooltip: true,
			tip: 'bottomMiddle'
		} 
	});
	
});

function showShare( type, id, title, headline, description ) {
	$( "#comment"+type+"_"+id ).hide();
	$( "#share"+type+"_"+id ).html( "<center><img src=\"images/loading.gif\" /><br/><br/>Loading share...</center>" );
	$( "#share"+type+"_"+id ).slideDown();
	$.ajax({
		   type: "POST",
		   url: "widgets/showShare.php",
		   data: "type="+type+"&id="+id+"&title="+title+"&headline="+headline+"&description="+description,
		   success: function( data ){
			$( "#share"+type+"_"+id ).html( data );
			$( "#"+type+""+id ).html( "<a href=\"javascript: showViewComments( '"+type+"', '" +id+ "', '"+title+"', '"+headline+"', '"+title+"' );\">Add / View Comments</a> | <a href=\"javascript: closeShare( '"+type+"', '" +id+ "', '"+title+"', '"+headline+"', '"+title+"' );\">Cancel share</a>" );
		   },
		   error: function( data ){
			   $( "#share"+type+"_"+id ).html( "<div class=\"ui-state-error ui-corner-all\">Error loading comments. Please try again.<div>" );
		   }
	});
}

function showViewComments( type, id, title, headline, description ) {
	$( "#share"+type+"_"+id ).hide();
	$( "#comment"+type+"_"+id ).html( "" );
	$( "#comment"+type+"_"+id ).show();
	$.ajax({
		   type: "POST",
		   url: "widgets/showComments.php",
		   data: "type="+type+"&id="+id+"&title="+title,
		   success: function( data ){
			$( "#comment"+type+"_"+id ).html( data );
			$( "#"+type+""+id ).html( "<a href=\"javascript: closeComments( '"+type+"', '" +id+ "', '"+title+"', '"+headline+"', '"+title+"' );\">Close Comments</a> | <a href=\"javascript: showShare( '"+type+"', '" +id+ "', '"+title+"', '"+headline+"', '"+title+"' );\">Share this post</a>" );
		   },
		   error: function( data ){
			   $( "#comment"+type+"_"+id ).html( "<div class=\"ui-state-error ui-corner-all\">Error loading comments. Please try again.<div>" );
		   }
	});
}

function closeComments( type, id, title, headline, description ) {
	$( "#comment"+type+"_"+id ).html( "" );
	$( "#comment"+type+"_"+id ).hide();
	$( "#"+type+""+id ).html( "<a href=\"javascript: showViewComments( '"+type+"', '" +id+ "', '"+title+"', '"+headline+"', '"+title+"' );\">Add / View Comments</a> | <a href=\"javascript: showShare( '"+type+"', '" +id+ "', '"+title+"', '"+headline+"', '"+title+"' );\">Share this post</a>" );
}

function closeShare( type, id, title, headline, description ) {
	$( "#share"+type+"_"+id ).slideUp();
	$( "#share"+type+"_"+id ).html( "" );
	$( "#"+id ).html( "<a href=\"javascript: showViewComments( '"+type+"', '" +id+ "', '"+title+"', '"+headline+"', '"+title+"' );\">Add / View Comments</a> | <a href=\"javascript: showShare( '"+type+"', '" +id+ "', '"+title+"', '"+headline+"', '"+title+"' );\">Share this post</a>" );
}

function showMedia() {
	$( "#showMediaLinks" ).html( "<center><img src=\"images/loading.gif\" /><br /><br />Loading media...</center>" );
	$.ajax({
		   type: "POST",
		   url: "widgets/showMediaLinks.php",
		   data: "id=0&type="+$( "#mediaTypes" ).val(),
		   success: function( data ){
			$( "#showMediaLinks" ).html( data );
		   },
		   error: function( data ){
			   $( "#showMediaLinks" ).html( "<div class=\"ui-state-error ui-corner-all\">Error loading media. Please try again.<div>" );
		   }
		});
}

function loadGallery( gallery, title, pic, height, width ) {
	$( "#dialog" ).html( "<center><img src=\"images/loading.gif\" /><br/><br/>Loading gallery...</center>" );
	$( "#dialog" ).dialog({
		bgiframe: true,
		title: title,
		width: width,
		height: height,
		buttons: {
			"Ok": function() {
				$( this ).dialog( "destroy" );
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "destroy" );
			$( this ).dialog( "close" );
		}
	});
	$.ajax({
	   type: "POST",
	   url: "widgets/loadGallery.php",
	   data: "gallery="+gallery+"&picture="+pic,
	   success: function( data ){
		$( "#dialog" ).html( data );
	   },
	   error: function( data ){
		   $( "#dialog" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function comment( id, page ) {
	$( "#dialog" ).html( "<img src=\"images/loading.gif\" />" );
	$( "#dialog" ).dialog({
		bgiframe: true,
		title: 'Add Comment',
		width: 600,
		height: 350,
		buttons: {
			"Add Comment": function() {
				$( "#errorMsg" ).html( "<center><img src=\"images/loading.gif\" /></center>" );
				$.ajax({
					   type: "POST",
					   url: "widgets/doAddComment.php",
					   data: "module="+page+"&id="+id+"&comment="+$( "#comment" ).val(),
					   success: function( data ){
							$( "#errorMsg" ).html( data );
					   },
					   error: function( data ){
						   $( "#errorMsg" ).html( "Error saving comment." );
					   }
					});
			}
		},
		close: function() {
			$( this ).dialog( "destroy" );
			$( this ).dialog( "close" );
		}
	});
	$.ajax({
	   type: "POST",
	   url: "widgets/showAddComment.php",
	   success: function( data ){
		$( "#comment" ).focus();
		$( "#dialog" ).html( data );
	   },
	   error: function( data ){
		   $( "#dialog" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function postNewTopic() {
	$( "#dialog" ).html( "<img src=\"images/loading.gif\" />" );
	$( "#dialog" ).dialog({
		bgiframe: true,
		title: 'Post New Topic',
		width: 600,
		height: 400,
		buttons: {
			"Post Topic": function() {
				$( "#errorMsg" ).html( "<center><img src=\"images/loading.gif\" /></center>" );
				$.ajax({
					   type: "POST",
					   url: "widgets/doAddTopic.php",
					   data: "module=new&id=0&comment="+$( "#POST" ).val()+"&SUBJECT="+$( "#SUBJECT" ).val(),
					   success: function( data ){
							$( "#errorMsg" ).html( data );
							$.ajax({
								   type: "POST",
								   url: "widgets/getTopics.php",
								   data: "id=0",
								   success: function( data ){
										$( "#topics" ).html( data );
								   },
								   error: function( data ){
									   $( "#topics" ).html( "Unable to reload topics." );
								   }
								});
					   },
					   error: function( data ){
						   $( "#errorMsg" ).html( "Error saving comment." );
					   }
					});
			}
		},
		close: function() {
			$( this ).dialog( "destroy" );
			$( this ).dialog( "close" );
		}
	});
	$.ajax({
	   type: "POST",
	   url: "widgets/showAddTopic.php",
	   success: function( data ){
		$( "#SUBJECT" ).focus();
		$( "#dialog" ).html( data );
	   },
	   error: function( data ){
		   $( "#dialog" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function postReply( id ) {
	$( "#errorMsg" ).html( "<img src=\"images/loading.gif\" />" );
	$.ajax({
	   type: "POST",
	   url: "widgets/doReply.php",
	   data: "id="+id+"&REPLY=" + $( "#REPLY" ).val(),
	   success: function( data ){
		$( "#errorMsg" ).html( data );
		
	   },
	   error: function( data ){
		   $( "#errorMsg" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function showThread( id, topic ) {
	$( "#dialog" ).html( "<img src=\"images/loading.gif\" />" );
	$( "#dialog" ).dialog({
		bgiframe: true,
		title: 'View Topic: ' + topic,
		width: 640,
		height: 600,
		buttons: {
			"Ok": function() {
				$( "#errorMsg" ).html( "<center><img src=\"images/loading.gif\" /></center>" );
				$( this ).dialog( "destroy" );
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "destroy" );
			$( this ).dialog( "close" );
		}
	});
	$.ajax({
	   type: "POST",
	   url: "widgets/showTopic.php",
	   data: "id="+id,
	   success: function( data ){
		$( "#dialog" ).html( data );
	   },
	   error: function( data ){
		   $( "#dialog" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function showJournalEntry( id ) {
	$( "#mainContent" ).html( "<center><img src=\"images/loading.gif\" /></center>" );
	$.ajax({
	   type: "GET",
	   url: "widgets/showJournal.php",
	   data: "id="+id,
	   success: function( data ){
		$( "#mainContent" ).html( data );
	   },
	   error: function( data ){
		   $( "#mainContent" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function login() {
	$( "#LoginMsg" ).html( "<center><img src=\"images/loading.gif\" /></center>" );
	$.ajax({
	   type: "POST",
	   url: "widgets/doLogin.php",
	   data: "action=login&username=" + $( "#USERNAME" ).val() + "&password=" + $( "#PASSWORD" ).val(),
	   success: function( data ){
			$( "#LoginMsg" ).html( data );
	   },
	   error: function( data ){
		   $( "#LoginMsg" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function register() {
	$( "#validateUsername" ).html( "" );
    $( "#validatePasswordLength" ).html( "" );
    $( "#validatePassword" ).html( "" );
    $( "#validateEmail" ).html( "" );
    $( "#validateFirstName" ).html( "" );
    $( "#validateSurname" ).html( "" );
    $( "#validateHumanCheck" ).html( "" );
	$( "#RegisterMsg" ).html( "<center><img src=\"images/loading.gif\" /></center>" );
	$.post( "widgets/doRegister.php", 
    		{ 	action: "register", 
				username: $( "#REGUSERNAME" )[0].value,
				email: $( "#EMAIL" )[0].value,
				cemail: $( "#CONFIRM_EMAIL" )[0].value,
				password: $( "#REGPASSWORD" )[0].value,
				cpassword: $( "#CONFIRM_PASSWORD" )[0].value,
				fname: $( "#FIRST_NAME" )[0].value,
				sname: $( "#SURNAME" )[0].value,
				rand1: $( "#RANDOM1" )[0].value,
				rand2: $( "#RANDOM2" )[0].value,
				userText: $( "#HUMANCHECK" )[0].value,
				newsletter: $( "#NEWSLETTER:checked" ).val()
        	},
        	function( response ) {
	            $.each( response, function( id, html )
	                {
	            		if( id  == 'ok' ) {
	            			$( "#RegisterMsg" ).html( "<div id=\"response\" class=\"ui-state-highlight ui-corner-all\">You have successfully registered. Thank you!<\/div>" );
	            		} else {
	            			$('#' + id ).html( html );
	            		}
	                });
        	}, 
        "json" );
}

function showShowInfo( id ) {
	$( "#dialog" ).html( "<center><img src=\"images/loading.gif\" /><br /><br />Loading show info...<br /></center>" );
	$( "#dialog" ).dialog({
		bgiframe: true,
		title: 'Show Details',
		width: 800,
		height: 500,
		buttons: {
			"Ok": function() {
				$( this ).dialog( "destroy" );
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "destroy" );
			$( this ).dialog( "close" );
		}
	});
	$.ajax({
	   type: "POST",
	   url: "widgets/showShowInfo.php",
	   data: "id="+id+"&content=false",
	   success: function( data ){
		$( "#dialog" ).html( data );
	   },
	   error: function( data ){
		   $( "#dialog" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function showShowComments( id ) {
	$( "#dialog" ).html( "<center><img src=\"images/loading.gif\" /><br /><br />Loading show info...<br /></center>" );
	$( "#dialog" ).dialog({
		bgiframe: true,
		title: 'Show Comments',
		width: 800,
		height: 500,
		buttons: {
			"Ok": function() {
				$( this ).dialog( "destroy" );
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "destroy" );
			$( this ).dialog( "close" );
		}
	});
	$.ajax({
	   type: "POST",
	   url: "widgets/showShowInfo.php",
	   data: "id="+id+"&content=false&comments=true",
	   success: function( data ){
		$( "#dialog" ).html( data );
	   },
	   error: function( data ){
		   $( "#dialog" ).html( "Error loading page. Please refresh." );
	   }
	});
}


function launchStreetTeam( ) {
        $( "#dialog" ).html( "<center><img src=\"images/loading.gif\" /><br /><br />Loading street team...<br /></center>" );
	$( "#dialog" ).dialog({
		bgiframe: true,
		title: 'Keith Mullins Music | Street Team',
		width: 880,
		height: 600,
		buttons: {
			"Ok": function() {
				$( this ).dialog( "destroy" );
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "destroy" );
			$( this ).dialog( "close" );
		}
	});
	$.ajax({
	   type: "POST",
	   url: "streetteam/",
	   success: function( data ){
		$( "#dialog" ).html( data );
	   },
	   error: function( data ){
		   $( "#dialog" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function mailList() {
	$( "#dialog" ).html( "<center><img src=\"images/loading.gif\" /><br /><br />Loading mailing list sign-up...</center>" );
	$( "#dialog" ).dialog({
		bgiframe: true,
		title: 'Subscribe to the Mailing List',
		width: 300,
		height: 200,
		buttons: {
			"Subscribe": function() {
				$( "#subscribeMsg" ).html( "<center><img src=\"images/loading.gif\" /></center>" );
				$.ajax({
				   type: "POST",
				   url: "widgets/subscribeToMailingList.php",
				   data: "action=validateEmail&name=" + $( "#NAME" ).val() + "&email=" + $( "#EMAIL" ).val() + "",
				   success: function( data ){
						$( "#subscribeMsg" ).html( "<center>" + data + "</center>" );
				   },
				   error: function( data ){
					   $( "#subscribeMsg" ).html( "Error loading page. Please refresh." );
				   }
				});
			}
		},
		close: function() {
			$( this ).dialog( "destroy" );
			$( this ).dialog( "close" );
		}
	});
	$.ajax({
	   type: "POST",
	   url: "widgets/showMailingList.php",
	   success: function( data ){
		$( "#dialog" ).html( data );
	   },
	   error: function( data ){
		   $( "#dialog" ).html( "Error loading page. Please refresh." );
	   }
	});
}

function sendToFriend() {
	$( "#dialog" ).html( "<center><img src=\"images/loading.gif\" /><br /><br />Loading friend mailer...</center>" );
	$( "#dialog" ).dialog({
		bgiframe: true,
		title: 'Send a message to a friend about KM',
		width: 500,
		height: 400,
		buttons: {
			"Send to Friend": function() {
				$( "#sendToFriendMsg" ).html( "<center><img src=\"images/loading.gif\" /></center>" );
				$.ajax({
				   type: "POST",
				   url: "widgets/doSendToFriend.php",
				   data: "action=sendToFriend&YOURNAME=" + $( "#YOURNAME" ).val() + "&YOUREMAIL=" + $( "#YOUREMAIL" ).val() + "&FRIENDSEMAIL=" + $( "#FRIENDSEMAIL" ).val() + "&FRIENDSNAME=" + $( "#FRIENDSNAME" ).val() + "&MESSAGE=" + $( "#MESSAGE" ).val(),
				   success: function( data ){
						$( "#sendToFriendMsg" ).html( "<center>" + data + "</center>" );
				   },
				   error: function( data ){
					   $( "#sendToFriendMsg" ).html( "Error loading page. Please refresh." );
				   }
				});
			}
		},
		close: function() {
			$( this ).dialog( "destroy" );
			$( this ).dialog( "close" );
		}
	});
	$.ajax({
	   type: "POST",
	   url: "widgets/showSendToFriend.php",
	   success: function( data ){
		$( "#dialog" ).html( data );
	   },
	   error: function( data ){
		   $( "#dialog" ).html( "Error loading page. Please refresh." );
	   }
	});
}