
/* ON READY
----------------------------------------------------*/
$(document).ready(function() {
	jQuery(".shareholderPercentage").live('keyup', shareholderPercentageChanged);
	
	jQuery(".alert .close").live('click', function(){
		$(this).parent().parent().fadeOut(500, function(){$(this).remove();});
	});
	
	jQuery(".alertSummoner").livequery('click', function(){
		$("#AlertPlaceHolder").fadeOut(0);
		$("#AlertPlaceHolder").css({
			"position":"absolute",
			"top":$(this).offset().top-20,
			"left":$(this).offset().left-350
		});
		$("#AlertPlaceHolder").fadeIn(500);
	});
	
	jQuery(".alertSummonerReverse").livequery('click', function(){
		$("#AlertPlaceHolder").fadeOut(0);
		$("#AlertPlaceHolder").css({
			"position":"absolute",
			"top":$(this).offset().top-20,
			"left":$(this).offset().left+80
		});
		$("#AlertPlaceHolder").fadeIn(500);
	});
	
	jQuery(".status").livequery(function(){
		$(this).InitNotification();
	});
	
	jQuery("#flashMessage").InitNotification();
});

var glbl_visibleNotifications = Array();

(function($) {
		
/* ADD SHAREHOLDER
----------------------------------------------------*/
	$.fn.AddShareholder = function() {
		var row = $('<li><div><input class="fieldSemiLarge" name="data[Tune][Shareholders][name][]"/></div><div><input name="data[Tune][Shareholders][percent][]" class="fieldMedium shareholderPercentage"/> <span class="textSmall">%</span></li></div>');
		var remove = $("<a class='buttonColorGray floatRight removeButton2 pointer'><span>Remove</span></a>");
		remove.bind('click', function(){$(this).parent().remove();shareholderPercentageChanged();});
		row.append(remove);
		$(this).append(row);
	};

/*ADD COMPOSER 
----------------------------------------------------*/

	$.fn.AddComposer = function() {
		var row2 = $('<li><div><input class="fieldLarge" name="data[Tune][Composer][]"/>');
		var remove = $("<a class='buttonColorGray floatRight removeButton pointer'><span>Remove</span></a></div></li>");
		remove.bind('click', function(){$(this).parent().remove();});
		row2.append(remove);
		$(this).append(row2);
	};
/*ADD CONTRIBUTOR 
----------------------------------------------------*/

	$.fn.AddContributor = function() {
		var row3 = $('<li><div><input class="fieldLarge" name="data[Tune][Contributor][]"/>');
		var remove = $("<a class='buttonColorGray floatRight removeButton pointer'><span>Remove</span></a></div></li>");
		remove.bind('click', function(){$(this).parent().remove();});
		row3.append(remove);
		$(this).append(row3);
	};	
/* INIT NOTIFICATION
----------------------------------------------------*/
	$.fn.InitNotification = function() {
		
		// Height of notification boxes (used for stacking)
		var nh = 80;
		
		// Make a clone of the notification element in order to allow for
		// several concurrent notifications
		var elmNotification = $(this).clone(true);
		
		// Remove old element so that the creating event will fire again 
		// for the next notification
		$(this).remove();
		
		// Set it up
		elmNotification.removeClass('status');
		elmNotification.hide();
		elmNotification.addClass('notification')
		elmNotification.appendTo('body');
		elmNotification.fadeIn('fast');
		
		// Position the new notification correctly
		elmNotification.css('bottom', ((glbl_visibleNotifications.length) * nh) + 10);
		
		// Add to array of visible notifications
		glbl_visibleNotifications.push(elmNotification);
		
		// Set a removal timeout and bind a click event
		setTimeout(function(){elmNotification.RemoveNotification();},10000);
		elmNotification.bind('click', elmNotification.RemoveNotification);
	};
	$.fn.RemoveNotification = function () {
		$(this).fadeOut('slow', function() {
			$(this).remove();
			glbl_visibleNotifications.pop($(this));
		});
	}
	
})(jQuery);


/* OTHER FUNCTIONS
----------------------------------------------------*/
function showDialogue() {
	//setTimeout(function() { $('flashMessage').remove() },1000);
}

function menuMouseOver(element,src) {
	 $(element).attr('src',src);
}

function menuMouseOut(element,src) {
	$(element).attr('src',src);
}

function shareholderPercentageChanged() {
	var totalPercent = 0;
	$("#shareholders").find("input.shareholderPercentage").each(function(i, elm){
		totalPercent += (elm.value - 0);
	});
	
	if(totalPercent > 100)
		$("#YourPercentage").val($("#YourPercentage").val() - (totalPercent - 100));
	else if(totalPercent < 100)
		$("#YourPercentage").val(($("#YourPercentage").val() - 0) + (100 - totalPercent));
}


/*ADD LINK TUNE STEP 2 
----------------------------------------------------*/
$.fn.AddTuneLink = function() {

	var headline = $("#addLinkHeadline").val();
	var url = $("#addLinkUrl").val();
	var description = $("#addLinkDescription").val();
	var row4 = $('<li class="linkBox marginTopBig marginRight" name="data[Tune][Link][]"><a href="'+url+'" title="'+description+'">'+headline+'</a></li>');
	var remove = $("<li><a class='pointer textSmall'><span class='closeButton displayBlock' title='Delete'></span></a></div></li>");
	remove.bind('click', function(){$(this).parent().remove();});
	row4.append(remove);
	$(this).append(row4);
};



/* Add tune help popups */
 
$(document).ready(function(){
	$("#a0").tipsy({gravity: 'w', fade: true})
	$("#a1").tipsy({gravity: 'w', fade: true})
	$("#a2").tipsy({gravity: 'w', fade: true})
	$("#a3").tipsy({gravity: 'w', fade: true})
	$("#a4").tipsy({gravity: 'w', fade: true})
	$("#a5").tipsy({gravity: 'w', fade: true})
	$("#a6").tipsy({gravity: 'w', fade: true})
	$("#a7").tipsy({gravity: 'w', fade: true})
	$("#a8").tipsy({gravity: 'w', fade: true})
	$("#a9").tipsy({gravity: 'w', fade: true})
	$("#a10").tipsy({gravity: 'w', fade: true})
	$("#a11").tipsy({gravity: 'w', fade: true})
	$("#a12").tipsy({gravity: 'w', fade: true})
	$("#a13").tipsy({gravity: 'w', fade: true})
	$("#a14").tipsy({gravity: 'w', fade: true})
	$("#a15").tipsy({gravity: 'w', fade: true})
	$("#a16").tipsy({gravity: 'w', fade: true})
	$("#a17").tipsy({gravity: 'w', fade: true})
	$("#a18").tipsy({gravity: 'w', fade: true})
	$("#a19").tipsy({gravity: 'w', fade: true})
	$("#a20").tipsy({gravity: 'w', fade: true})
	$("#a21").tipsy({gravity: 'w', fade: true})
	$("#a22").tipsy({gravity: 'w', fade: true})
	$("#a23").tipsy({gravity: 'w', fade: true})
	$("#a24").tipsy({gravity: 'w', fade: true})
	// JS for add tune step 3

	// JS to check if radio buttons are checked when the page loads
	if($("#TuneRevenueStreamN").is(":checked")) {
		$("#testing2k").show();
		
	};
	
	if($("#TuneRevenueStreamE").is(":checked")) {
		$("#testing3k").show();
		
	};
	
	if($("#TuneTradingY").is(":checked")) {
		$("#testing4k").show();
		
	};
		// If user selects "new" then show div
	$("#TuneRevenueStreamN").click(function() {
	    if(this.checked){
		 $("#testing2k").slideDown();
		 $("#testing3k").slideUp();
	    }

	});
	
	// If user selects "existing" then show div
	$("#TuneRevenueStreamE").click(function() {
	    if(this.checked){
		 $("#testing2k").slideUp();
		 $("#testing3k").slideDown();
	    }

	});
	
	
	$("#TuneTradingY").click(function() {
	    if(this.checked){
		 $("#testing4k").slideDown();
	    }

	});
	$("#TuneTradingN").click(function() {
	    if(this.checked){
		 $("#testing4k").slideUp();
	    }

	});
	
	$("#readMoreA").click(function() {
		 $("#readMore").slideToggle();


	});
	



	// JS for login box.

	// Hover for login button	
	$("#menuLogin").hover(function() {
		// on mouse in
		$("#menuLogin").attr('src','/img/design/menuLogInAc.png');	
		},
		// on mouse out
		function() {
		$("#menuLogin").attr('src','/img/design/menuLogInIn.png');	
		});
	

	// Toggle for login box
	$("#menuLogin").toggle(function() {

			// Unbind hover on login button because it messes with the image src
			$("#menuLogin").unbind('mouseenter mouseleave');
			$("#loginBox").slideDown();
			$("#menuLogin").attr('src','/img/design/menuLogInAc.png');
		}, 
		function() {
			
			$("#loginBox").slideUp();
			$("#menuLogin").attr('src','/img/design/menuLogInIn.png');				
			
			// rebind hover event, so hover works if box gets toggled again. NEEDED!

			// on mouse in
			$("#menuLogin").hover(function() {
				$("#menuLogin").attr('src','/img/design/menuLogInAc.png');	
			},
			// on mouse out
			function() {
				$("#menuLogin").attr('src','/img/design/menuLogInIn.png');	
			});
		});


	$("#offers").click(function(){
		 $("#SearchGenre").attr('disabled', true);
	});
	$("#songs").click(function(){
		 $("#SearchGenre").attr('disabled', false);
	});
});

