// JavaScript Document

$(document).ready(function () {

	///<COOKIES>
	//SET The various cookies throughout the site
	
	//set the name field 
		if ($.cookie('name') != null)
			{
				$("[name='name']").val($.cookie('name'));
			}
		//if the cookie for state is set, make the value
		if ($.cookie('state') != null)
			{	
				$("select[name='state']").attr("value",$.cookie('state'));
			}

		
	//</cookies>	

	//this is located in the navigation
	//when click opens a dialog with the sendnote-form
		$('.open-sendnote-form').click( function(){
			$(".sendnote-form").dialog({modal:true, title:'Send a note!', position: [20, 20]});
		});
	
	//open-tag-search
	//when clicked opens searchform. 
		$(".open-tagsearch-form").click( function(){
			$(".searchtag-form").dialog({modal:true, title:'Search a tag!', position: [20, 20]});
		});


	//homepage vertical ticker
	$('.vticker').show(); 
	$('.vticker').vTicker({
		   speed: 500,
		   pause: 3000,
		   showItems: 3,
		   animation: 'fade',
		   mousePause: true,
		   height: 0,
		   direction: 'down'
     }	);



	//tipsy - tooltips
		$('[title]').tipsy(
			
			{
				delayIn: 1000,      // delay before showing tooltip (ms)
				delayOut: 2000,     // delay before hiding tooltip (ms)
				fade: true,     // fade tooltips in/out?
				fallback: '',    // fallback text to use when no tooltip text
				gravity: 's',    // gravity
				html: false,     // is tooltip content HTML?
				live: false,     // use live event support?
				offset: 0,       // pixel offset of tooltip from element
				opacity: 0.8,    // opacity of tooltip
				title: 'title',  // attribute/callback containing tooltip text
				trigger: 'hover' // how tooltip is triggered - hover | focus | manual
			}
		
		);



	//uses the corner plugin to make rounded corners
		$('.corner').corner( '5px');
	
	
	
	
	
	//cleans text fields from invalid characters and converts to uppercase
		$('.no-ichars').keyup(function() {
			
			var foo = $(this).val(); //gets value from input
			foo = foo.toUpperCase(); //makes value uppercase
			foo = foo.replace(/ /g, ""); //removes spaces
			
			//assigns invalid characters
			var iChars = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
			foo = foo.replace(iChars,""); //remove those invalid chars
			
			$(this).val(foo); //puts cleaned value back in input
			
		});
	
	
	//limit character ammount
		$('.limitchars').keyup(function() {
			var foo = $(this).val();
			
			if (foo.length > 254)
				{
					
					foo = foo.substring(0,253);
					$(this).val(foo);	
					
				}
			//displays the remaining characters
			var bar = 254 - foo.length; 
			
			$('.remaining-chars').text("("+bar+")");
			
		});	

	//comment button - when clicked, goes to the note page
	//this is needed for older browser IE
		$("button.comment").click( function(){
			var noteid = $(this).parents("li").attr('roadzernote');
			window.location = "get.php?n=" + noteid;
		});

	//submits note votes via post
		$(".note *[class^='vote']").click( function(){
			//get the note id
			var noteid = $(this).parents("li").attr('roadzernote');
			
			//sets the correct field to update
			if ($(this).hasClass("voteup"))
				{
					var myfield = "vote_up";
					
				}
			else if ($(this).hasClass("votedown"))
				{
					var myfield = "vote_down";
						
				}
			$(this).html("Vote submited!");
			$.post("_includes/submitvote.php", {noteid: noteid, field: myfield});
			
		});


	//submits comment votes via post
		$(".comment button[class^='vote']").click( function(){
			//get the note id
			var commentid = $(this).parents("li").attr('commentId');
			
			
			//sets the correct field to update
			if ($(this).hasClass("voteup"))
				{
					var myfield = "vote_up";
					
				}
			else if ($(this).hasClass("votedown"))
				{
					var myfield = "vote_down";
						
				}
			$(this).html("Vote submited!");
			$.post("_includes/submitcommentvote.php", {commentid: commentid, field: myfield});
			
		});






	//rollover image code
	//put code like this:
	//<img src="first.gif" hover="second.gif"  class="rollover"/>
			$('.rollover').hover(function() {
				var currentImg = $(this).attr('src');
				$(this).attr('src', $(this).attr('hover'));
				$(this).attr('hover', currentImg);
			}, function() {
				var currentImg = $(this).attr('src');
				$(this).attr('src', $(this).attr('hover'));
				$(this).attr('hover', currentImg);
			});
	



	//for any tag that has license plate language, 
	//it will switch to normal language on mouse hover
		$(".withTagLang").hover(
			function () {
				$(this).find("span.tagLang").hide();
				$(this).find("span.normalLang").show();
			},
			function () {
				$(this).find("span.tagLang").show();
				$(this).find("span.normalLang").hide();
			}
		);
	
	//display instructions for each element with class of .withinstructions
	//display the element with .instructions on hover
		$(".withinstructions").hover(
			function () {
				$(this).find(".instructions").show();
			},
			function () {
				$(this).find(".instructions").hide();
			}
		);
	
	
	
	//send email from contact form
		$("#contact-sendemail").click( function() {
			$(".loading").fadeIn(200);
			var myName = $("input[name='name']").val();
			var myEmail = $("input[name='email']").val(); 
			var myDetails = $("textarea[name='details']").val();	
				
			$.post("_includes/email.php", {name: myName, email: myEmail, details: myDetails}, 
				
					function(){
						$("#contact-email-form").hide();
						$('#feedback').html(
							"<p><b>Your message has been sent. If it requires a response, we will get back to you as soon as possible.</b></p>"
						);						
					});
			
			$(".loading").fadeOut(200);
		});
	
	
	
	
	//sendmsg
	// uploads the message sent to a particular license plate
		$(".sendmsg").click ( function() {
			
			$(this).fadeOut(200);
			$(".loading").fadeIn(200);
			
			var myLicense = $(".sendnote-form input[name='license']").val();
			var myState = $(".sendnote-form select[name='state']").val(); 
			var myMessage = $(".sendnote-form textarea[name='message']").val();
			
			if (myLicense !="" && myState != "" && myMessage !=""){
				
			//post message
				$.post("_includes/sendmsg.php", {license: myLicense, state: myState, message: myMessage} ,
					function(){
						

					
					});	
			
			//set state on cookie
				$.cookie("state",myState);
				
			//notify user
					$('.feedback').html("<p class='success'><a href='get.php?s="
							 + myState
							 + "&l=" 
							 + myLicense 
							 +"'>Message sent! Click here to view the messages sent to this vehicle.</a></p>"
					).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100)
					.fadeOut(100).fadeIn(100);						
						
			}
			else
			{
				$('.feedback').html
				("<p class='error corner'>We need more information. Please verify that you enter all fields.</p>").fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100)
					.fadeOut(100).fadeIn(100).delay(4000).fadeOut(100);
			}
			
			$("textarea[name='message']").val("");
			$(this).fadeIn();
		});
		
			
	//getmsg 
	//redirects to get.php passing the values for the selected plate and state
		$(".btnSearchTag").click ( function() {
			var myLicense = $(".searchtag-form input[name='license']").val();
			var myState = $(".searchtag-form select[name='state']").val(); 
			
			if (myLicense !="" && myState != ""){
				
				window.location="get.php?s="+myState+"&l="+myLicense;
			}
			else
			{
				$('.feedback').html
				("<p class='error corner'>We need more information. Please verify that you enter all fields.</p>")
				.fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100)
				.fadeOut(100).fadeIn(100).delay(2000).fadeOut(200);
			}
		});	
			



	///<comments>
	//
		$("#sendComment").click( function(){
			
			//get the note id
				var strName = $("[name='name']").val();
				var intNote = $(this).attr('noteId'); 	
				var strComment = $("textarea[name='comment']").val();
			
			//Set the name value on the cookie for future use	
				$.cookie("name",strName);
			
			//if theres no name, put this
				if (strName == "") {strName = "Anonymous";}
			
			//submit value
				if (strComment != "")
				{
					//submit comment
						$.post("_includes/submitcomment.php", {comment: strComment, note: intNote, name:strName});
			
			
					//notify user comment was sent
						$('#feedback').html
						("<p class='success corner'>Your comment was posted and will display when the page is refreshed!</p>")
						.fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100)
						.fadeOut(100).fadeIn(100).delay(4000).fadeOut(200);
						
					//empty textarea
						$("textarea[name='comment']").val('');	
				}
				else
				{
					//notify user 
						$('#feedback').html
						("<p class='error corner'>Please enter a comment.</p>")
						.fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100)
						.fadeOut(100).fadeIn(100).delay(4000).fadeOut(200);
				}
				
			
			
				
		});
	
	
	
	///</comments>
	
	
	
	//when the mouse is over the li, change the style for the li
		$("#notes .note").hover(
			function () {
				$(this).addClass("noteHovered");
			}, 
			function () {
				$(this).removeClass("noteHovered");
			}
		);
	
	
	//makes the banner on the homepage rotate
		$('#ic-box2').jshowoff({ speed:5000, links: true, hoverPause:true, controls:false }); 
	

	//displays the facebook page like box using JqueryUI modal dialog
		$(".facebook").click( function() {
			$(".facebookpage").dialog({
				modal:true
			});
		});
		
		
	// place holder - displays a placeholder text on tags that have a placeholder attribute
		$("[placeholder]").ghostText();


	

	
	
});

