$(document).ready(function(){
	fixIE();
	message();
	videos(autoplay);
	hover();
	menuEvents();
	$(document).pngFix();
});

$('div#message').change(function() {
	message();
});

function message()
{
	if (jQuery.trim($('div#message').text()) != '')
	{
		$('div#message').show('slow').delay(4000).hide('slow');
	}
}

function hover() 
{
	$('ch').each(function(index) {
    	$(this).mouseenter(function() {
    		$(this).animate({color: "#03cbd7"}, 200);
    	});
    	
    	$(this).mouseleave(function() {
    		$(this).animate({color: "#ffffff"}, 300);
    	});
  	});
}

function menuEvents()
{
	$('.menu-mark').each(function(index) {
	
		var id = '#sub-' + $(this).attr('id');

		$(this).mouseenter(function() {
  			//$(id).slideToggle();
  			
  			var parent = $(id).parent();
  			var offset = parent.position();
  			var ph = parent.height();
  			if ($.browser.webkit) {var t = offset.top + 25;}
            else if ($.browser.opera){var t = offset.top + 25;}
            
            else {var t = offset.top + 25;}
  			
  			if ($.browser.msie && parseInt($.browser.version) <= 7) t = t+20;
            if ($.browser.msie && parseInt($.browser.version) == 7) t = t-10;
  			
  			$(id).css({'top': t+'px', 'left': offset.left+'px'});

  			$(id).show();
		});

		$(this).mouseleave(function() {
  			//$(id).slideToggle();
  			$(id).hide();
		});
  	});
}

function fixIE()
{
	if ($.browser.msie && parseInt($.browser.version) <= 6)
	{
		//gradients
		//$('div.white-box').gradient({ topcolor: '#ffffff', bottomcolor: '#d1f0fc', horizontal: false, opacity: 20 });

		//shadows
		//$('div.white-box').shadow({ color: "#08c1fb", offset: 5, opacity: 0.2 });

		//borders
		$("div.white-box").corner("top cc:#6ac6eb round 15px").corner("bottom cc:#6ac6eb round 15px");
		$("div.gray-box").corner("top cc:#6ac6eb round 15px").corner("bottom cc:#6ac6eb round 15px");
		$("div#footer-line").corner("cc:#6ac6eb round 10px");
		$("div.lesson-skill-button").corner("cc:#fff round 10px");
		$("div.lesson-header").corner("bottom cc:#fff round 10px");
		$("div.lesson-box-small").corner("cc:#fff round 10px");
		$(".level-header").corner("left cc:#6ac6eb round 10px");
		$(".level-button").corner("right cc:#6ac6eb round 10px");
		$(".level-desc").corner("cc:#6ac6eb round 1px");
		
		$("a.menu-button").corner("cc:#6ac6eb round 10px");
	}
}

/*******************************************************************************
 * Embed flash player where needed
 ******************************************************************************/
function playerReady(thePlayer) 
{
	var player = document.getElementById(thePlayer.id);
	player.addModelListener("STATE", "stateListener");
}

function stateListener(obj) //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED 
{
	if(obj.newstate == 'COMPLETED')
    {
    	var alias = document.URL.replace(/^(.*)\//i,'');
    	
		$.ajax({
			url: 'api/setlesson/'+alias,
		  	success: function(data) {
		    	//reload the next page
		    	if (data.indexOf('link:')>=0)
		    	{
		    		var link = data.replace(/^(.*)link:/i,'');
		    		if (jQuery.trim(link) != '')
		    		{
		    			$('#lesson-menu-wrap').load(link + ' #lesson-menu');
		    		}
		    	}
		  	}
		});
	}
}

function videos(autostart)
{
	$('.video').each(function(index) {
		var player = new SWFObject('include/jscripts/mediaplayer/player.swf','player_'+index,'468','300','9');
		player.addParam('allowfullscreen','true');
		player.addParam('allowscriptaccess','always');
		player.addParam('wmode','transparent');
		//player.addParam('scale', 'noorder');
	    player.addParam('flashvars', 'screencolor=ffffff&autostart=' + autostart + '&bufferlength=10&file=../../../' + $(this).attr('id') + '?' + Math.round(Math.random() * 1000) );
		player.write($(this).attr('id')); //this will invoke playerReady internally
	});
}
/******************************************************************************/

function toggleValue(element, value)
{
	if ($(element).val() == value)
	{
		$(element).val('');
	}
	else if (jQuery.trim($(element).val()) == '')
	{
		$(element).val(value);
	}
}

function confirmDialog(title, message, form)
{
	$("#dialog-confirm").html('<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + message);
	$("#dialog-confirm").dialog({
				height:140,
				modal: true,
				title: title,
				buttons: {
					'OK': function() {
						$(form).submit();
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
	});
}

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) 
	{
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) 
		{
			var c = string.charCodeAt(n);
 
			if (c < 128) 
			{
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) 
			{
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else 
			{
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) 
		{
			c = utftext.charCodeAt(i);
 
			if (c < 128) 
			{
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) 
			{
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else 
			{
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}

