/**
 * @author Vertazzar
 */

$.extend(jQuery, {'app' : {}});

$.app.core = {
		
		current_location : '',
        validate_forms : function ()
        {
        	$(".validate-form").each(function () {
        		
        	if (!$(this).data('form-validate'))  {
        		
        	$(this).validate({
        		
        		highlight: function(element, errorClass, validClass) {
        			if (element.type === 'radio') {
        				this.findByName(element.name).addClass(errorClass).removeClass(validClass);
        			} else {
        				$(element).addClass(errorClass).removeClass(validClass);
        				$(element).parents('.clearfix').addClass(errorClass).removeClass(validClass);
        			}
        		},
        		
        		unhighlight: function(element, errorClass, validClass) {
        			if (element.type === 'radio') {
        				this.findByName(element.name).removeClass(errorClass).addClass('success');
        			} else {
        				$(element).removeClass(errorClass).addClass(validClass);
        				$(element).parents('.clearfix').removeClass(errorClass).addClass('success');
        			}
        		}
        		
        	});
        	
        	$(this).find("[type='reset']").click(function () {
        		
        		 $(this).parents('form').find('.success').each(function () {$(this).removeClass('success');});
        		 $(this).parents('form').find('.error').each(function () {$(this).removeClass('error');});
       
        	});
        	
        	
        	$(this).data('form-validate', true);
        	
        	
        	}
        	
        	
        	});
        },
  
		initilize : function ()
		{
			  this.current_location = document.location.pathname + document.location.search;
			  
			  var self = this;
			
			  $("a").removeClass("active");
	          $("a").each(function() {
	          			
                       if ($(this).attr("href") == self.current_location && $(this).attr("href") != "javascript:;")
                       {
             
                           $(this).addClass("active");
                           if ($(this).parents('li').length)
                           {
                        	   $(this).parents('li').addClass("active");
                           }
                           
                           
                       }
		       });
	          
		}
		
};

$.app.dialog = {
		
		instances : {},
		factory : function (params)
		{
			 if (!params['target'])
			 {
				 var model_id = 'modal_d_' + params['instance'];
				 params['target'] = '#' + model_id;
				 if (!$(div_id).length)
				 {
					 $('body').append('<div id="'+model_id+'" style="display:none;"></div>');
				 }
				 
			 }
			 var div_id = params['target'];
			 $(div_id).modal(params);
			 this.div = $(div_id);
			 this.html = function (str)
			 {
				 this.div.html(str);
				 return this;
			 };
			 this.close = function ()
			 {
				 this.div.modal('hide');
				 return this;
			 };
			 
			 this.open = function ()
			 {
				 this.div.modal('show'); 
				 return this;
			 };
		},
		instance : function (params)
		{
			 if (typeof (params) == 'string')
			 {
				 params = {'instance' : params};
			 }
			 else if (typeof (params) == undefined)
			 {
				 params = {'instance' : 'default'};
			 }
			 
			 if (!this.instances[params['instance']])
			 {
				 this.instances[params['instance']] = new this.factory(params);
			 }
			 
			 return this.instances[params['instance']];
		}
		
};
