/**	TITLE: Inspirefy Creativity Plugin
	AUTHOR: Marcus Battle / Ideal Visions LLC
	DATE: 11/10/09
	DESCRIPTION: This plugin houses many of the core functions for the design (inspiration) process for the inspirefy(TM) product
	COPYRIGHT: This plugin is not available for public/commercial use but is intended directly for the inspirefy(TM) product and any other 	products created by the parent company Ideal Visions LLC. All Rights Reserved. Copyright 2009.
**/

(function($) {
		  
// This function builds the css for a newly created page in inspirefy
jQuery.fn.buildcss = function() {
	css_text = '';
	$(this).each(function(index){
		if($(this).attr('id') == 'body'){
			css_text += 'body { ';
		} else if($(this).attr('id') == 'h1'){
			css_text += 'h1 { ';
		} else if($(this).attr('id') == 'h2'){
			css_text += 'h2 { ';
		} else if($(this).attr('id') == 'h3'){
			css_text += 'h3 { ';
		} else {
			css_text += '#' + $(this).attr('id') + ' { ';
		}
		
		if($(this).attr('style') == null){
			css_text += ''; 
		} else {
			css_text += $(this).attr('style');
		}
		css_text += ' }\n\n';	
	});
	
		
	
	return css_text;
};

// Checks to see if the value has already been registered (i.e. domain exists, user exists, etc.)
$.fn.exists = function(options){
	
	var settings = jQuery.extend({ url: 'inspirefy/exists', field:'site_short_name', table:'users', ico_valid:'', ico_invalid:'', ico_warning:'', min_length:'6' }, options);
	
	var field = '#' + $(this).attr('id');
	var $this = $(this);
	var $parent = $(this).parent();
	$parent.append('<div class="exists_msg"></div>');

	$this.live('keyup',function(){
		if($this.val().length >= settings.min_length){
			$.post(settings.url,{value:$this.val(), field:settings.field, table:settings.table},function(data){
				if(data.success){
					$parent.find('.exists_msg').html('<img src="'+settings.ico_valid+'"><span id="msg"> '+data.msg+'</span>');
					$(field).removeClass("invalid");
				} else {
					$parent.find('.exists_msg').html('<img src="'+settings.ico_invalid+'"><span id="msg"> '+data.msg+'</span>');
					$(field).addClass("invalid");
				}
			},'json');
		} else {
			$parent.find('.exists_msg').html('<img src="'+settings.ico_warning+'"><span id="msg">Your input is not long enough. It must be at least '+settings.min_length+' characters.</span>');
			$(field).addClass("invalid");
		}							
	});
	
	return this;
};


$.fn.confirmfield = function(options) {
	var settings = jQuery.extend({ field_suffix: '_c',  ico_valid:'', ico_invalid:'', min_length:'6' }, options);
	
	return this.each(function(){
		var $this = $(this);
		var field = '#' + $(this).attr('id');
		var $confirm_field = $('#' + $(this).attr('id') + settings.field_suffix);
		var $parent = $confirm_field.parent();		
		
		$parent.append('<div class="alert"></div>');
		
		$confirm_field.bind('keyup',function(){

			if($confirm_field.val().length >= settings.min_length){
				if($confirm_field.val() == $this.val()){
					$parent.find('.alert').html('<img src="'+settings.ico_valid+'">');
					$(field).removeClass("invalid");
				} else {
					$parent.find('.alert').html('<img src="'+settings.ico_invalid+'">');
					$(field).addClass("invalid");
				}
			} else {
				$parent.find('.alert').html('<img src="'+settings.ico_invalid+'">');
				$(field).addClass("invalid");
			}
		});
	});
};

$.fn.validate = function() {
	var is_valid = true;
	
	$(this).find('.required').each(function(){
		
		if($(this).hasClass('invalid')){
			is_valid = false;				
		}
		
		if($(this).val().length == 0){
			is_valid = false;
		}
	});

	return is_valid;
};

$.fn.sort_pages = function(){
	//$(this).addClass('drop_kids');
	
	$(this).find('li').each(function(){
		$(this).find('a').attr('href','#');
		
		if($(this).children().is('ul')){
			$(this).find('ul').addClass('drop_kids');
		} else {
			$(this).append('<ul class="drop_kids">&nbsp;</ul>');
		}
	});
	return this;	
}
})(jQuery);