String.prototype.capitalize = function(){
   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
  };

$(document).ready(function() {
	/*mailinglist form */
	$('input[emptyvalue]').click(function() {
		if ($(this).val() === $(this).attr('emptyvalue')){
			$(this).val('');
		}
	}).focusout(function() {
		if ($(this).val() === ''){
			$(this).val($(this).attr('emptyvalue'));
		}
	});
	
	/* if not use change zip code to postal code */
	$('#MailinglistCountryId').change(function() {
		var mailinglistZip = $('#MailinglistZip');
		var oldemptyvalue = mailinglistZip.attr('emptyvalue');
		var emptyvalue = '';
		if ($(this).val() === 'US'){
			emptyvalue = 'zip code';
		} else {
			emptyvalue = 'postal code';
		}
		mailinglistZip.attr('emptyvalue', emptyvalue);
		if (mailinglistZip.val() === oldemptyvalue){
			mailinglistZip.val(emptyvalue);
		}
	});
	

	$('#mailinglist').submit(function() {
		var url = $(this).attr('action');
		var data = $(this).serialize();
		$.ajax({
			type: 'POST',
			url: url,
			dataType: 'json',
			data: data,
			success: function (data){
	        	$(".message").remove();
	        	$(".error-message").remove();
			
				//alert(data);
				if (!data.success){
					//data['Mailinglist']['email']
					for (key in data['Mailinglist']){
						var val = data['Mailinglist'][key];
						var name = key.capitalize();
			
						//Display errors
					    var _insert = $(document.createElement('div'))/*.css('display', 'none')*/;
					    _insert.attr('id', 'flashMessage').addClass('message').text(val);
					    _insert.insertAfter($("#Mailinglist"+name));
					}
				} else {
					$('#mailinglist').replaceWith('<div>Thank you for signing up.</div>');
				}
				return false;
			},
		});

		return false;
	});
	
	
	/*twitter*/
    $(".tweet").tweet({
        username: "loveinoctober",
        join_text: "auto",
        avatar_size: 32,
        count: 3,
        auto_join_text_default: "we said,",
        auto_join_text_ed: "we",
        auto_join_text_ing: "we were",
        auto_join_text_reply: "we replied to",
        auto_join_text_url: "we were checking out",
        loading_text: "loading tweets..."
    });
	
	/* qtip */
	// Use the each() method to gain access to each elements attributes
	$('#content a[rel]').each(function(){
		$(this).qtip({
			content: {
				// Set the text to an image HTML string with the correct src URL to the loading image you want to use
				text: $(this).attr('rel'),
/*				
				url: $(this).attr('rel'), // Use the rel attribute of each element for the url to load
				title: {
					text: 'Wikipedia - ' + $(this).text(), // Give the tooltip a title using each elements text
					button: 'Close' // Show a close link in the title
				}
*/
			},
			position: {
				corner: {
					target: 'bottomMiddle', // Position the tooltip above the link
					tooltip: 'topRight'
				},
				adjust: {
					screen: true // Keep the tooltip on-screen at all times
				}
			},
/*			show: { 
				when: 'click', 
				solo: true // Only show one tooltip at a time
			},
			hide: 'unfocus',
*/			style: {
				tip: true, // Apply a speech bubble tip to the tooltip at the designated tooltip corner
				border: {
					width: 0,
					radius: 4
				},
				name: 'light', // Use the default light style
				width: 300 // Set the tooltip width
			}
		})
	});
	
	//music player
	/*
		<li>Love in October - Soft Errors</li>
		<li>The Yearbooks - Stranger of the Night</li>
		<li>Love in October - Dance Johannes Kepler</li>
		<li>Love in October - Like Nothing Ever Happened</li>
		<li>Calvin Marty - Without You (unmastered)</li>
		<li>Enlou - Nineteen ninety-five (unmastered)</li>
	*/
	if ($("#jquery_jplayer").length != 0){
		
		var playItem = 0;
		
		var myPlayList = [
		{name:"Love in October - Do You Love Me",mp3:"mp3/01 Do You Love Me.mp3"},
		{name:"Love in October - Paper Heart",mp3:"mp3/02 Paper Heart.mp3"},
		{name:"Love in October - Desperate",mp3:"mp3/03 Desperate.mp3"},
		{name:"Love in October - Rubix Cube.mp3",mp3:"mp3/04 Rubix Cube.mp3"},
		{name:"Love in October - Don_t Forget.mp3",mp3:"mp3/05 Don_t Forget.mp3"},
		//{name:"Enlou - Nineteen ninety-five (unmastered)",mp3:"http://www.loveinoctober.com/mp3/1995 - Mix 2.mp3"},
		];
		
		// Local copy of jQuery selectors, for performance.
		var jpPlayTime = $("#jplayer_play_time");
		var jpTotalTime = $("#jplayer_total_time");
		var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page
		
		
		$("#jquery_jplayer").jPlayer({
			ready: function() {
				displayPlayList();
				playListInit(true); // Parameter is a boolean for autoplay.
			},
			oggSupport: false,
			swfPath: "js"
		})
		.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
			jpPlayTime.text($.jPlayer.convertTime(playedTime));
			jpTotalTime.text($.jPlayer.convertTime(totalTime));
		
		})
		.jPlayer("onSoundComplete", function() {
			playListNext();
		});
		
		$("#jplayer_previous").click( function() {
			playListPrev();
			$(this).blur();
			return false;
		});
		
		$("#jplayer_next").click( function() {
			playListNext();
			$(this).blur();
			return false;
		});
		
		function displayPlayList() {
			$("#jplayer_playlist ul").empty();
			for (i=0; i < myPlayList.length; i++) {
				var listItem = (i == myPlayList.length-1) ? "<li class='jplayer_playlist_item_last'>" : "<li>";
				listItem += "<span href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>";
				listItem += myPlayList[i].name;
				listItem +="</span>";
				listItem +="</li>";
				$("#jplayer_playlist ul").append(listItem);
				/*
				$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
					var index = $(this).data("index");
					if (playItem != index) {
						playListChange( index );
					} else {
						$("#jquery_jplayer").jPlayer("play");
					}
					$(this).blur();
					return false;
				});
				*/
			}
		}
		
		function playListInit(autoplay) {
			if(autoplay) {
				playListChange( playItem );
			} else {
				playListConfig( playItem );
			}
		}
		
		function playListConfig( index ) {
			$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
			$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current");
			playItem = index;
			$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
		}
		
		function playListChange( index ) {
			playListConfig( index );
			$("#jquery_jplayer").jPlayer("play");
		}
		
		function playListNext() {
			var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
			playListChange( index );
		}
		
		function playListPrev() {
			var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
			playListChange( index );
		}
	}
});

