/* get mouse position */
var mouseX = 0;
var mouseY = 0;
$(document).mousemove(function(e) { 
	mouseX = e.pageX;
	mouseY = e.pageY;
});
/* END :: get mouse position */

var globalPath = "";

/* document ready function */
$(document).ready(function(){
	var currentLocation = new String(window.location);
	if (currentLocation.indexOf("localhost") == -1) {
		globalPath = "http://www.akordi.eu/";	
	} else {
		globalPath = "http://localhost/Akordi-eu/";	
	}
	
	if (document.location.href  == globalPath || document.location.href == (globalPath + "zadnji-igrani-akordi/")) {
		setInterval("location.reload(true);", 60 * 1000);	
	}
	
	/* fix the flash elements for IE browsers */
	flashObjects = document.getElementsByTagName("object");
	for (var i = 0; i < flashObjects.length; i++) {
		flashObjects[i].outerHTML = flashObjects[i].outerHTML;
	}
	
	/* blink the information DIV if it is present */
	$(".flashInformation").fadeIn().fadeOut().fadeIn();
	
	/* set the autoscroller speed via links in Controls */
	$("a.autoscroller").click(function(event){
		event.preventDefault();
		var clickedLink = $(this).html();
		var scrollSpeed = clickedLink.substr(25,1);
		if (scrollSpeed >= 0 && scrollSpeed <= 5) setScrollSpeed(scrollSpeed);
	});
	
	/* set the functionality on the "add to songbook" link */
	$("a.addToSongbook").click(function(event){
		event.preventDefault();
		var linkID = $(this).attr("id");
		var songID = linkID.substring(3);
		addToSongbook(songID);
	});
	
	/* set the functionality for AJAX rating of chords and performances */
	$("a.chordsPerformanceRating").click(function(event){
		event.preventDefault();
		var linkID = $(this).attr("id");
		var ratingType = linkID.substr(0,1);
		var ratingMark  = linkID.substr(2,1);
		var ratingParentID = linkID.substring(4);
		vote(ratingType, ratingParentID, ratingMark);
	});
	
	/* trigger the display of additonal information about posting a performance */
	$("a.performanceInfoTrigger").click(function(event){
		event.preventDefault();
		var clickedLink = $(this).html();
		if (clickedLink == "Hočem vedeti več ...") {
			$("a.performanceInfoTrigger").html("Skrij informacije ...");	
			$("#performanceInfo").slideDown("slow");
		} else {
			$("a.performanceInfoTrigger").html("Hočem vedeti več ...");	
			$("#performanceInfo").slideUp("slow");	
		}
	});
	
	/* show the appropriate chord pattern when the mouse pointer hovers over a certain chord */
	$("span.showChordSpanLink").hover(function() {
		var name = $(this).html();
		var linkName = $(this).attr("name");
		var pattern = linkName.substring(6);
		var patternBody = "<b>Akord:</b> " + name + "<br />";
		if (pattern.substring(7, 8) != 1) {		// NOTE: substring is less practical than substr, but supported in Netscape 2 and 3, IE 3 and Opera 3.
			patternBody += pattern.substring(7, 8) + ". polje<br />";
		}
		for (i = 0; i < 6; i++) {
			if (i == 0) patternBody += "<b>e</b> ";
				else if (i == 1) patternBody += "<b>B</b> ";
				else if (i == 2) patternBody += "<b>G</b> ";
				else if (i == 3) patternBody += "<b>D</b> ";
				else if (i == 4) patternBody += "<b>A</b> ";
				else if (i == 5) patternBody += "<b>E</b> ";
			for (j = 0; j < 20; j++) {
				if (j % 4 == 3) patternBody += "|";
					else if (j % 4 == 1 && j == (pattern.substring(5 - i, 6 - i) - 1) * 4 + 1) patternBody += "x";
					else if (j % 4 == 1 && pattern.substring(5 - i, 6 - i) == "x") patternBody += "~";
					else patternBody += "-";
			}
			patternBody += "<br />";
			
		}
		$("#chordPatternContent").html("<pre>" + patternBody + "</pre>");
		$("#chordPattern").css("top", (mouseY - 100) + "px");
		$("#chordPattern").css("left", (mouseX + 15) + "px");
		$("#chordPattern").show();
	}, function() {
		$("#chordPattern").hide();	
	});
	
	/* trigger a popup information, telling the visitor that a login is required */
	$("a.requireLogin").click(function(event){
		event.preventDefault();
		$("#requireLoginInfo").css("top", mouseY).css("left", mouseX + 5).slideDown("fast");
	});
	
	/* close the popup window */
	$("a.closeRequireLogin").click(function(event){
		event.preventDefault();
		$("#requireLoginInfo").slideUp("fast");
	});
	
});

/* get a pressed key */
$(document).keypress(function(e) {
	switch(e.which) {
		// numeric keys above the letter keys
		case 48: setScrollSpeed(0); break;
		case 49: setScrollSpeed(1); break;
		case 50: setScrollSpeed(2); break;
		case 51: setScrollSpeed(3); break;
		case 52: setScrollSpeed(4); break;
		case 53: setScrollSpeed(5); break;
		
		/*
		// numpad on the right side of the keyboard
		case 96: setScrollSpeed(0); break;
		case 97: setScrollSpeed(1); break;
		case 98: setScrollSpeed(2); break;
		case 99: setScrollSpeed(3); break;
		case 100: setScrollSpeed(4); break;
		case 101: setScrollSpeed(5); break;
		*/
	}					 
});
/* END :: get a pressed key */

/* implementation of auto-scrolling */
scrollPageRunning = 0;
scrollPosition = 0;
scrollSpeed = 0;
scrollTimeoutId = 0;

function scrollPage() {	
	var chordsEnd = getY($("#subChordsContent")[0]);
	var checkValue = chordsEnd - 590;
	scrollPosition = window.pageYOffset || document.documentElement.scrollTop || 0;
	scrollPosition++;
	if (scrollPosition < checkValue) {
		$("#rightFloated")[0].style.margin = scrollPosition + "px 0px 0px 0px";
		window.scroll(0, scrollPosition);
	}
	scrollTimeoutId = setTimeout("scrollPage()", scrollSpeed);
}

function setScrollSpeed(speed) {
	if (speed == 1) scrollSpeed = 800;
		else if (speed == 2) scrollSpeed = 500;
		else if (speed == 3) scrollSpeed = 200;
		else if (speed == 4) scrollSpeed = 70;
		else if (speed == 5) scrollSpeed = 30;	
	if (scrollPageRunning == 0) {
		scrollPage();
		scrollPageRunning = 1;
	}
	if (speed == 0) {
		scrollSpeed = 1000000;
		clearTimeout(scrollTimeoutId);
		scrollPageRunning = 0;
	}
}

function getY(oElement) {
	var iReturnValue = 0;
	while (oElement != null) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}
/* END :: implementation of auto-scrolling */

/* transpose the song on change of the dropdown menu */
function transpose() {
	transposeIndex = $("#transposeList")[0].selectedIndex;
	transposeValue = $("#transposeList")[0].options[transposeIndex].value;
	if (transposeValue != 100) {
		var currentLocation = new String(window.location);
		currentLocation = currentLocation.split('transpose')[0];
		if (transposeValue != 0) newLocation = currentLocation + "transpose" + transposeValue + "/";
			else newLocation = currentLocation;
		window.location = newLocation;
	}
}
/* END :: transpose the song on change of the dropdown menu */

/* AJAX voting system */
function vote(type, parent, rating) {
	/* type: 1 comment (rating 1 = up, rating 0 = down), 2 chords, 3 performance */	
	if (type == 1) {
		fieldName = "commentVote" + parent;
	} else {
		fieldName = "rateChords";	
	}
	
	$("#" + fieldName).html("Prosimo, počakajte ...");
	
	var timeQuery = new Date();
	timeGET = timeQuery.getTime();
	var queryString = "?type=" + type + "&parent=" + parent + "&rating= " + rating + "&time=" + timeGET;
	
	$.get(globalPath + "action-ajax-vote.php" + queryString, function(data){
		$("#" + fieldName).html(data);
	});
}
/* END :: AJAX voting system */

/* AJAX system for adding songs to user's songbook */
function addToSongbook(songID) {
	
	$("#addToSongbook").html("Prosimo, počakajte ...");
	
	var timeQuery = new Date();
	timeGET = timeQuery.getTime();
	var queryString = "?song=" + songID + "&time=" + timeGET;
	
	$.get(globalPath + "action-ajax-add-to-songbook.php" + queryString, function(data){
		if (data == "CODE: Redirect user to songbook editing page") {
			window.location = globalPath + "index.php?page=songbooks-edit&song=" + songID;	
		} else {
			$("#addToSongbook").html(data);
		}
	});
}
/* END :: AJAX system for adding songs to user's songbook */

/* set the cookies */
function setCookie(cookieName, cookieValue, nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays == null || nDays == 0) nDays = 1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/";
}
/* END :: set the cookies */