/**
 * @author Nathan Andersen
 */

var leftOffset = 0;
var lastKeyBlack = false;
var currentIndex = 0;


function urlencode(str) {
	str = escape(str);
	str = str.replace('%23', '-sharp');
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	str = str.replace('(', '%28');
	str = str.replace(')', '%29');
	return str;
	}


function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}


$(function(){
	

	
	var idNum = 0;
	// position Keys
	$(".key").each(function(){
		idNum++;
		$(this).attr('id','key'+idNum);
		
		var spacing = 0;
		if (idNum > 1) spacing = 37;
		
		if (lastKeyBlack) {
			spacing = 17;
		}
		
		if($(this).hasClass('blackKey')) {
			 spacing = 17;
			 lastKeyBlack = true;
		}
		else {
			lastKeyBlack = false;
		}

		
		$(this).css('margin-left',leftOffset + spacing);
		
		leftOffset += spacing;
	});
	$("#pianoImage .key").show();
	

	
	
	
$(".whiteKey .keyInner, .blackKey .sharpKey, .blackKey .flatKey").click(function(){
		
		$("#intro").empty().append('<h2>Now click on a chord to see and hear it using an interactive flash diagram.</h2>')
		var currentCat = "#cat-" + urlencode($(this).text());
		
		$(".chordCategoryHolder").hide();
		
		$(currentCat).show();
		
		var trackCode = "/#" + urlencode($(this).text() + "_chords");
		
		
		window.location.href = "http://www.piano-chords.net" + trackCode;
		pageTracker._trackPageview(trackCode);
		
		
		
	});
	
	
	


	
	$(".variation, .basicChord").click(function(){
		var trackCode = "/#" + urlencode($(this).text());
		window.location.href = "http://www.piano-chords.net" + trackCode;
		pageTracker._trackPageview(trackCode);
		$("#intro").hide();
		$("#announcements").show();
		pianoChordGenerator($(this).attr('id'));
		
	});
	
	initURL();
	
});

function initURL() {

	var currentHref = window.location.href;
	var found = false;
	
	$("div.chordCategoryHolder").each(function(){
		if(!found){
			var thisID = $(this).attr('id');
			var thisRoot = thisID.substring(4,thisID.length);
			if ("http://www.piano-chords.net/#" + thisRoot + "_chords" == currentHref) {
				
				found = true;
				$("div.chordCategoryHolder").hide();
				$(this).show();
				
				}
				
				
			
		}
	});
	
	$(".variation, .basicChord").each(function(){
		if (!found){
			if ("http://www.piano-chords.net/#" + urlencode($(this).text()) == currentHref) {
				found = true;
				$(".chordCategoryHolder").hide();
				$(this).parent().parent().parent().parent().parent().show();
				$(this).trigger('click');
				
			}
		}
		
		
	});
	
}

function pianoChordGenerator(myChordCode){
   
	
	$("#flashPianoHolder").show();
	$("#flashPianoHolder").css('height','600px');
    var flashvars = {
        CurrentChordCode: myChordCode,
        piano_chord_type: ""
    
    };
    var params = {};


    var attributes = {};
	swfobject.embedSWF("swfs/piano-chords-net.swf", "flashPianoDiv", "968", "600", "10.0.0", false, flashvars, params, attributes);
 

	

}

