$(document).ready(function()
{	
	/**
	 * FORM BEHAVIOURs
	 */
	//** assign default blur class to form elements
	$(":input:not(:submit)[class!=mceEditor]").addClass('formBlur');
	$("select").addClass('formBlur');
	
	//** bind focus event to inputs, add class
	$(":input:not(:submit)[class!=mceEditor][class!=mSelect]").livequery(
		"focus",
		function() {
			$(this).removeClass('formBlur');
			$(this).addClass('formFocus');
			if ($('label[for="'+ $(this).attr('id') + '"]').length > 0) {
				$('label[for='+ $(this).attr('id') + ']').addClass('labelFocus');
			};
			if($(this).next('p.description').length > 0) {
				$(this).next('p.description').show();
			}
		}
	);

	//** bind blur event to inputs, remove class
	$(":input:not(:submit)[class!=mceEditor][class!=mSelect]").livequery(
		"blur",
		function() {
			$(this).removeClass('formFocus');
			$(this).addClass('formBlur');
			if ($("label[for='"+ $(this).attr('id') + "']").length > 0) {
				$("label[for='"+ $(this).attr('id')+ "']").removeClass('labelFocus');
			};
			if($(this).next('p.description').length > 0) {
				$(this).next('p.description').hide();
			}
		}
	);
    
	//** TINYMCE **// 
    $('.tinymce').livequery(function(){
    	$(this).tinymce(
	    {
			// Location of TinyMCE script
			script_url : '/js/tiny_mce/tiny_mce.js',
			// General options
			theme : "advanced",
			plugins : "style,advlink,inlinepopups,preview,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
			//width: "200",
			// Theme options
			theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,sub,sup,|,bullist,numlist,|,link,unlink,|,charmap,fullscreen",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,cleanup,help,code,|,preview,|,template",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			content_css : "/css/layout-mce.css"/*,
	        template_templates : [{
	        	title : "Promo 2011 default",
	        	src : "template?t=promo",
	        	description : "Default promo layout"
	        },
	        {
	        	title : "Newsletter default",
	        	src : "template?t=newsletter",
	        	description : "Default newsletter layout"
	        }]*/
		});
    });
	
    //** masked time inputs
	$(".time").livequery(function(){$(this).mask("99:99");});
	$(".bday").livequery(function(){$(this).mask("9999/99/99");});
	
	//** disabled links
	$("a.disabled").live('click',function(){
		return false;
	});
	
	$("button.href").live('click',function(){
		var url = $(this).val();
		window.location = url;
	});
	
	$("div.clickHref").live('click',function(){
		var url = $(this).attr('ref');
		if(url.length > 0) {
			window.location = url;
		}
	});

	//** disable autocomplete in forms
	$("form").attr('autocomplete', 'off');

	//** append asterix on required form fields
	$("label.required").livequery(function() {
		$(this).append(' *');
	});
    
    
    
    /**
	 * PLUGINS
	 */
	//** JQUERY UI DATEPICKER - set defaults
	if($.isFunction($.fn.datepicker)) {
		$.datepicker.setDefaults({
			firstDay : 1,
			dateFormat : 'yy/mm/dd',
			showOtherMonths : true
		});
	};
	
	// GRITTER - growl like notifications
	$.extend($.gritter.options, { 
        position: 'bottom-left', // defaults to 'top-right' but can be 'bottom-left', 'bottom-right', 'top-left', 'top-right' (added in 1.7.1)
		fade_in_speed: 350, // how fast notifications fade in (string or int)
		fade_out_speed: 280 // how fast the notices fade out
	});
	
    
	//** JQUERY UI BUTTON
	//** set type jqueryui button for input types: button, submit
	$("button, input:submit, input:button").livequery(
		function() 
		{
			// submit button
			if ($(this).hasClass('formSubmit') || $(this).hasClass('ajaxSubmit')) {
				$(this).button({ icons : { primary : "ui-icon-disk" } });
			} 
			// edit button
			else if ($(this).hasClass('edit')) {
				$(this).button({ icons : { primary : "ui-icon-pencil" } });
			} 
			// create button
			else if ($(this).hasClass('create')) {
				$(this).button({ icons : {primary : "ui-icon-plus" } });
			} 
			// apply button
			else if ($(this).hasClass('apply')) {
				$(this).button({ icons : { primary : "ui-icon-check" } });
			} 
			// reset button
			else if ($(this).hasClass('reset')) {
				$(this).button({ icons : { primary : "ui-icon-refresh" } });
			}  
			// cancel button
			else if ($(this).hasClass('cancel')) {
				$(this).button({ icons : { primary : "ui-icon-close" } });
			}  
			// delete button
			else if ($(this).hasClass('delete')) {
				$(this).button({ icons : { primary : "ui-icon-trash" } });
			}    
			// assign button
			else if ($(this).hasClass('assign')) {
				$(this).button({ icons : { primary : "ui-icon-arrowthick-2-e-w" } });
			}     
			// assign button
			else if ($(this).hasClass('download')) {
				$(this).button({ icons : { primary : "ui-icon-folder-collapsed" } });
			}      
			// assign button
			else if ($(this).hasClass('upload')) {
				$(this).button({ icons : { primary : "ui-icon-arrowthickstop-1-n" } });
			} 
			// default button - no icon
			else {
				$(this).button();
			};
		}
	);
    
    /*$("#toggleHead").live('click',function()
    {
    	var offSet = $("#siteHeader").offset();
    	if(offSet.top == 0)
    	{
    		$("#siteHeader").animate({
    			'margin-top': '-70px'
    		}, 150, function(){});
    		$("#siteBody").animate({
    			'padding-top': '30px'
    		}, 150, function(){});
    	}
    	else {
    		$("#siteHeader").animate({
    			'margin-top': '0'
    		}, 150, function(){});
    		$("#siteBody").animate({
    			'padding-top': '112px'
    		}, 150, function(){});
    	}
    });*/
    
    //** MASKED INPUTS **//
    //$("#release_price").livequery(function(){$(this).mask("99.99");});
    //$(".play_time").livequery(function(){$(this).mask("99:99");});
    
    //** mediaplayer **//
    /*$(".mediapl").livequery(function(){
    	//$(this).media();
    });*/
    
    /*$("#contentBody ul.adminNav li").live('click',function(){
    	var url = $("a",this).attr('href');
    	//alert(url);
    	window.location = url;
    });*/
    
    
    //** AJAX FORM **//
    /*$(".ajaxForm").livequery('submit', function() 
    {
    	$(this).ajaxSubmit({
    		beforeSubmit: function(){},
    		success: function(res, statusText)
    		{
    			if(res.valid == false) {
    				$(res.responseContainer+' div p').html($(res.responseContainer+' div p').html()+res.message);
					$(res.responseContainer).slideToggle(200,function() {
						$(this).effect("pulsate", {times: 4}, 650);
					});
    			}
    			else {
    				if(res.uid != '') { $("#news_uid").val(res.uid); }
    				if(res.responseContainer != '' && res.message != '') 
    				{
    					$(res.responseContainer+' div p').html($(res.responseContainer+' div p').html()+res.message);
    					$(res.responseContainer).slideToggle(200,function() {
    						$(this).effect("pulsate", {times: 4}, 650, function() {
    							$(this).slideToggle(200);
    						});
    					});
    				}
    			}
    		},
    		dataType: 'json'
    	});
    	return false;
    });*/
    

    //** TRIGGERS **//
    /*$("#fncTrigger-Upload").live('click',function() {
    	$("#uploadContainer").slideToggle('fast');
    	return false;
    });
    $("#fncTrigger-Search").live('click',function() {
    	$("#searchContainer").slideToggle('fast');
    	return false;
    });
    $("#fncTrigger-Export").live('click',function() {
    	$("#exportContainer").slideToggle('fast');
    	return false;
    });
    $("#fncTrigger-Refresh").live('click',function() {
    	var selectedPath = $(this).attr('href');
    	$.ajax({
			type: 'POST',
			url: '/file/index/list?path='+selectedPath,
			success: function(data){
				$("#dirContents").html(data);
			},
			dataType: 'html'
		});
    	return false;
    });*/
    
});


/**
 * add gritter (growl-like) notifications
 * @param JSON params
 * @param boolean sticky 
 * @param int time
 */
function addNotice(params,sticky,time,cssClass)
{
	sticky 		= typeof(sticky)   != 'undefined'   ? sticky 	 : true;
	time   		= typeof(time) 	   != 'undefined'   ? time   	 : 5000;
	cssClass	= typeof(cssClass) != 'undefined'   ? cssClass   : 'info';
	$.gritter.add({
		title: params.title,			// (string | mandatory) the heading of the notification
		text: params.text,				// (string | mandatory) the text inside the notification
		//image: 'http://s3.amazonaws.com/twitter_production/profile_images/132499022/myface_bigger.jpg',	// (string | optional) the image to display on the left
		sticky: sticky,					// (bool | optional) if you want it to fade out on its own or just sit there
		time: time,						// (int | optional) the time you want it to be alive for before fading out
		class_name: cssClass	// (string | optional) the class name you want to apply to that specific message
	});
}


