$(document).ready(function(){
	message();
	hover();
	menuEvents();
	datepickers();
	$("ul.sortable").sortable();
	$("ul.sortable").disableSelection();
	$(document).pngFix();
	setAutocompleteFields();
	setValidationFields();
	//fixIE();
	
	//temp workaround
	if (($('div#content').width()-$('div#main').width()-$('div#left').width()-11)<600)
		$('div#main-right').width($('div#content').width()-$('div#left').width()-11);
	else
		$('div#main-right').width($('div#content').width()-$('div#main').width()-$('div#left').width()-11);
});

$('div#message').change(function() {
	message();
});

function setAutocompleteFields() {
	$('input[id$="auto"]').each(function(index) {
	
		var url = document.location.href;
		var req_data = "useraction=ajax&api=get_values&field=" + $(this).attr('name') + "&val=";
		
		$(this).autocomplete({
			source: function(request, response) {
				$.ajax({
					type: 'POST',
					url: document.location.href,
					data: req_data + request.term,
				  	success: function(data) {
						response(JSON.parse(data));			  	
				  	},
				  	error: function(data) {
				  		response('');
				  	}
				});
			}
		});
	});
}

function setValidationFields() {
	$('form').each(function(index) {
		$(this).validationEngine();
	});
}

function message()
{
	if (jQuery.trim($('div#message').text()) != '')
	{
		$('div#message').show('slow').delay(10000).hide('slow');
	}
}

function hover() 
{
	$('ch').each(function(index) {
    	$(this).mouseenter(function() {
    		$(this).animate({color: "#505050"}, 200);
    	});
    	
    	$(this).mouseleave(function() {
    		$(this).animate({color: "#f24f00"}, 300);
    	});
  	});
}

function datepickers()
{
	$.datepicker.setDefaults({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true, yearRange: '1910:2100'});
	$("input[id$='datepicker']").datepicker();
}

function textCounter(field, cntfield, maxlimit) 
{
	if ($(field).val().length > maxlimit) // if too long...
	{
		$(cntfield).html('<b style="color:red;">'+$(field).val().length+'</b>');		
	}
	else
	{
		$(cntfield).html($(field).val().length);
	}
}

function menuEvents()
{
	$('.menu-mark').each(function(index) {
	
		var id = '#sub-' + $(this).attr('id');

		$(this).mouseenter(function() {
  			
  			var parent = $(id).parent();
  			var offset = parent.position();
  			var ph = parent.height();
  			
  			var t = offset.top + ph;
  			
  			if ($.browser.msie)
  				t = t-7;
  			
  			$(id).css({'top': t+'px', 'left': offset.left+'px'});
  			
  			$(id).show();
		});

		$(this).mouseleave(function() {
  			$(id).hide();
		});
  	});
  	
	$('.menu-entry').each(function(index) {
	
		var id = '#sub-' + $(this).attr('id');

		$(this).mouseenter(function() {
  			
  			var parent = $(id).parent();
  			var offset = parent.position();
  			var ph = parent.height();
  			
  			var t = offset.top + ph;
  			
  			$(id).css({'top': t+'px', 'left': offset.left+'px'});
  			
  			$(id).show();
		});

		$(this).mouseleave(function() {
  			$(id).hide();
		});
  	});
}

function fixIE()
{
	if ($.browser.msie)
	{
		//borders
		
		//shadows
		
		//gradients
	}
}

/*******************************************************************************
 * 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')
    {

	}
}

function videos()
{
	$('.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('flashvars', 'autostart=false&file=../../../cache/upload/' + $(this).attr('id'));
		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');
					}
				}
	});
}

function confirmDelete(content, form)
{
	confirmDialog("Delete content?", "Do you want to delete " + content + "?", form);
}	

function startWait(id)
{
	$('#'+id).append("<div class='wait'></div>");
	
}

function finishWait(id)
{
	$('#'+id+' > .wait').remove();
}

/**
*
*  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;
	}
}

