$.ajaxSetup({
	'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

$.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

$.fn.uniform = function(settings) {
  settings = $.extend({
    valid_class    : 'valid',
    invalid_class  : 'invalid',
    focused_class  : 'focused',
    holder_class   : 'ctrlHolder',
    field_selector : 'input, select, textarea'
  }, settings);
  
  return this.each(function() {
    var form = $(this);
   
    var focusControlHolder = function(element) {
      var parent = element.parent();
      
      while(typeof(parent) == 'object') {
        if(parent) {
          if(parent[0] && (parent[0].className.indexOf(settings.holder_class) >= 0)) {
            parent.addClass(settings.focused_class);
            return;
          }
        } 
        parent = $(parent.parent());
      } 
    };
   
    form.find(settings.field_selector).focus(function() {
      form.find('.' + settings.focused_class).removeClass(settings.focused_class);
      focusControlHolder($(this));
    }).blur(function() {
      form.find('.' + settings.focused_class).removeClass(settings.focused_class);
    });
  });
};

	
//----------------------------
// metadata
//----------------------------

(function($) {

$.extend({
	metadata : {
		defaults : {
			type: 'class',
			name: 'metadata',
			cre: /({.*})/,
			single: 'metadata'
		},
		setType: function( type, name ){
			this.defaults.type = type;
			this.defaults.name = name;
		},
		get: function( elem, opts ){
			var settings = $.extend({},this.defaults,opts);
			// check for empty string in single property
			if ( !settings.single.length ) settings.single = 'metadata';
			
			var data = $.data(elem, settings.single);
			// returned cached data if it already exists
			if ( data ) return data;
			
			data = "{}";
			
			if ( settings.type == "class" ) {
				var m = settings.cre.exec( elem.className );
				if ( m )
					data = m[1];
			} else if ( settings.type == "elem" ) {
				if( !elem.getElementsByTagName ) return;
				var e = elem.getElementsByTagName(settings.name);
				if ( e.length )
					data = $.trim(e[0].innerHTML);
			} else if ( elem.getAttribute != undefined ) {
				var attr = elem.getAttribute( settings.name );
				if ( attr )
					data = attr;
			}
			
			if ( data.indexOf( '{' ) <0 )
			data = "{" + data + "}";
			
			data = eval("(" + data + ")");
			
			$.data( elem, settings.single, data );
			return data;
		}
	}
});

$.fn.metadata = function( opts ){
	return $.metadata.get( this[0], opts );
};

})(jQuery);	

$(document).ready(function(){

	$("#ui-tabs-panel > ul").tabs({ fx: { opacity: 'toggle' } });
	
	$("#comment_form, #asset_project_id, #category_form, #client_form, #service_form, #announcement_form").submitWithAjax();
	$(".create_asset, a#create_category, a#create_service, a#create_client, a#create_announcement, a#view_services, a#view_categories, a#view_announcements").fancybox({
		'zoomSpeedIn':	0, 
		'zoomSpeedOut':	0,
		'overlayShow': true 
	});
	
	$("a.resetButton").click().clearForm();
	
	$("a.modal_cancel_form").click(parent.$.fn.fancybox.close);
	
	$(".remove_asset").click( function() {
		$(".asset_item").fadeOut(3000);
		$(".images li a.remove").fadeOut(3000);
		
	});
	
	$(".notice").fadeOut(4000);
	$("#notice-contact").fadeOut(8000);
	
	$(".portfolio-assets a").fancybox({
		'overlayShow': true 
	});
	
	$('#hide_announcement').click( function() {	
		
		$.get('/hide_announcements');

	    $("#announcements_box").fadeOut(3000);
	    return false;
	});

  
	//-------------------------- Mark It Up
	
	mySettings = {
		onShiftEnter:	{keepDefault:false, replaceWith:'<br />\n'},
		onCtrlEnter:	{keepDefault:false, openWith:'\n<p>', closeWith:'</p>\n'},
		onTab:			{keepDefault:false, openWith:'	 '},
		markupSet: [
			{name:'Heading 1', key:'1', openWith:'<h1(!( class="[![Class]!]")!)>', closeWith:'</h1>', placeHolder:'Your title here...' },
			{name:'Heading 2', key:'2', openWith:'<h2(!( class="[![Class]!]")!)>', closeWith:'</h2>', placeHolder:'Your title here...' },
			{name:'Heading 3', key:'3', openWith:'<h3(!( class="[![Class]!]")!)>', closeWith:'</h3>', placeHolder:'Your title here...' },
			{name:'Heading 4', key:'4', openWith:'<h4(!( class="[![Class]!]")!)>', closeWith:'</h4>', placeHolder:'Your title here...' },
			{name:'Heading 5', key:'5', openWith:'<h5(!( class="[![Class]!]")!)>', closeWith:'</h5>', placeHolder:'Your title here...' },
			{name:'Heading 6', key:'6', openWith:'<h6(!( class="[![Class]!]")!)>', closeWith:'</h6>', placeHolder:'Your title here...' },
			{name:'Paragraph', openWith:'<p(!( class="[![Class]!]")!)>', closeWith:'</p>' },
			{separator:'---------------' },
			{name:'Bold', key:'B', openWith:'(!(<strong>|!|<b>)!)', closeWith:'(!(</strong>|!|</b>)!)' },
			{name:'Italic', key:'I', openWith:'(!(<em>|!|<i>)!)', closeWith:'(!(</em>|!|</i>)!)' },
			{name:'Stroke through', key:'S', openWith:'<del>', closeWith:'</del>' },
			{separator:'---------------' },
			{name:'Ul', openWith:'<ul>\n', closeWith:'</ul>\n' },
			{name:'Ol', openWith:'<ol>\n', closeWith:'</ol>\n' },
			{name:'Li', openWith:'<li>', closeWith:'</li>' },
			{separator:'---------------' },
			{name:'Blockquote', openWith:'<blockquote>\n', closeWith:'</blockquote>\n' },
			{name:'Cite', openWith:'<cite>', closeWith:'</cite>' },
			{separator:'---------------' },
			{name:'Picture', key:'P', replaceWith:'<img src="[![Source:!:http://]!]" alt="[![Alternative text]!]" />' },
			{name:'Link', key:'L', openWith:'<a href="[![Link:!:http://]!]"(!( title="[![Title]!]")!)>', closeWith:'</a>', placeHolder:'Your text to link...' },
			{separator:'---------------' },
			{name:'Clean', className:'clean', replaceWith:function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") } },
			{name:'Preview', className:'preview', call:'preview' }
		]
	};

 	$(".markItUp").markItUp(mySettings);
	
	//----------------------------------
	
	
    $('.uniForm').uniform();



//	$.initializeValidation();
 
});

