var Util = function()
{
	return {
	
	    RemoveSpaces: function(jqo)
	    {
	        jqo.each(function () { this.value = $.trim(this.value); });
	    },
	
	    GenerateIDsForLabels: function()
	    {
	       $('label').each(function(){	
	            (function(label, id) {
	                $(label).prev().attr('id', id);
	                $(label).attr('for', id);
	            })(this, Util.GenerateRandomID());
        	    	    
	        });
	    },
	
	    GoHome: function(url)
	    {
	        window.location.href = url;
	    },
	
	   OnEnter: function(arr)
        {
            for ( var i in arr )
            {
                (function(elt, func){ elt.keyup(function(e)	{ if(e.keyCode == 13) func(); }) })(arr[i][0], arr[i][1])
            }
        },
	
        CloseOtherLightboxes: function()
        {
            $('div.b-lightbox').hide();
        },

        GenerateRandomID: function()
        {
            return Math.random()*1000;
        },

        RecalculateSearchResultsNumber: function(elt, count) 
		{  
		    if( ! count ) count = 1;
		 
		    elt.text( elt.text().replace(/(\d+)\sof\s(\d+)/, function(a, b, c)  { return (parseInt(b)-count)+' of '+(parseInt(c)-count) } ) );
			
		    if( elt.text().indexOf('Showing 0 of 0') != -1 )
		    {							
			    elt.parent().parent().html('<h2>No Items found</h2>');
		    }
    	},				

		ReviveExpanders: function(element) 
		{
			// toggle expanders
			
			if( element )
			{
			    element.find('div.i-expander').click( function() {
				    $(this).toggleClass('expanded');
				    $(this).parent().next().toggle();
			    });
			}
			else
			{
			    $('div.i-expander').click( function() {
				    $(this).toggleClass('expanded');
				    $(this).parent().next().toggle();
			    });
			}
		},
		
		ShowOverlay: function(elt) 
		{
			$('div.b-overlay').height( $(document).height() ).css('opacity','0.8').show();
			if ($.browser.msie) { $('select').not('div.b-lightbox select').hide(); }
			
			if( elt ) elt.show();
			
			return false;
		},
		
		HideOverlay: function(elt)
		{
			$('div.b-overlay').height( $(document).height() ).css('opacity','0.8').hide();
			if ($.browser.msie) { $('select').not('div.b-lightbox select').show(); }
			
			if( elt ) elt.hide();
		},
		
		CloseOverlay: function() 
		{
			$('div.b-overlay').height( $(document).height() ).css('opacity','0.8').fadeOut('fast');
		},
		
		IsValidAge: function( age )
		{
			if( age != "" && ! /^[0-9]+$/.test(age) || parseInt(age) < 1 ||  parseInt(age) > 150 )
			{
				return false;
			}
			else
			{
				return true;
			}
		},
		
		IsDateWithingDates: function( needed, from, to )
		{
			var current_date = new Date( needed );		
			var start_date = new Date( from );
			var end_date = new Date( to );
		
			if(  current_date.getTime() >= start_date.getTime() &&  current_date.getTime() <= end_date.getTime() )
			{						
				return true;
			}
			else
			{
				return false;
			}			
		},	
		
		IsStartDateMoreThanEndDate: function( from, to )
		{
			var start_date = new Date( from );
			var end_date = new Date( to );
		
			if( from != "" && to != "" && start_date.getTime() > end_date.getTime() )
			{						
				return true;
			}
			else
			{
				return false;
			}			
		},		
		
		DecreaseCounterInSearchResults: function( elt )
		{
			elt.text( elt.text().replace(/(\d+)\sof\s(\d+)/, function(a, b, c)  { return (parseInt(b)-1)+' of '+(parseInt(c)-1) } ) );
		},
		
		CheckAll: function (a) 
		{
			$(a).parent().parent().parent().find('input:checkbox').attr('checked', 1);
			return false;
		},
		
		UnCheckAll: function (a) 
		{
			$(a).parent().parent().parent().find('input:checkbox').removeAttr('checked');
			return false;
		},
		
		SwitchTab: function (sel) 
		{
			var index = $(sel).parent().children().index( $(sel) );
			$(sel).parent().children().removeClass('active');
			$(sel).removeClass('over');
			$(sel).addClass('active');
			$(sel).parent().next('div').children().hide();
			$(sel).parent().next('div').children().eq(index).show();
		},
		
		RemoveDuplicatesFromArray: function (source_arr) 
		{
            var hash = {};
            
            for( var i = 0; i < source_arr.length; i++ ) 
			{
				hash[source_arr[i]] = 0;
			}
			
			var arr = [];
			
			for( var i in hash ) arr.push(i);
			
			return arr;
		},
		
		SetCalendars: function(calendars, format, elements)
		{			
			for( var i in  calendars )
			{
			    var input = null;
                var img = null;
                
			    if( typeof(calendars[i]) == 'string')
			   {
			        input = elements ? elements.find('#'+ calendars[i]) : $('#'+ calendars[i]);
                    img = input.next();
                }
               else
               {
                    input = elements ? elements.find('#'+ calendars[i][0]) : $('#'+ calendars[i][0]);
                    img = elements ? elements.find('#'+ calendars[i][1]) : $('#'+ calendars[i][1] );
               }
               
	            if( input.size() )
	            {
		            var options = {
                        inputField: input.get(0),
                        ifFormat: format,						
                        button: img.get(0)
                    };
        					
		            if( format.indexOf("%H:%M") != -1 )
		            {
			            options['showsTime'] = true;
			            options['timeFormat'] =  24;					
		            }
        			
		            Calendar.setup(options);
	            }               		
            }
		},

		SetSlider: function( sliders, pct, campaign_id, enable_sliders )
		{
			/*slider*/
			sliders.each(function() {
				
				var amount = $(this).children('input:first');
				
				$(this).children('div:first').slider({
					value: pct,
					min: 0,
					max: 100,
					step: 1,
					slide: function(event, ui) { amount.val(ui.value + '%') },
					stop: function(event, ui) { 
						$.ajax({
							type: "POST",
							url: window.location.pathname + "/change-ads-percentage/"+ ui.value +"/"+ campaign_id,
							success: function (r)
							{
								r = eval(r);
				
								if( r.code == 0 )
								{
									CommunicationPathPage.UpdateCost(r.cp_cost);
								
									alert("Ads percentage changed successfully");
								}
							}
						});
					}
				});
			});
		},
		
		StartWaiting: function()
		{
			$('#loader').show();
	    },
	    
	    StopWaiting: function()
		{
			$('#loader').hide();
		},
		
	    ChangeNumPerPageResults: function(select)
		{
			var a = window.location.href;
			
			a = a.replace(/&page=\d+/, "").replace(/&num_per_page=\d+/, "") + "&num_per_page="+ select.value;
			
			window.location.href = a;
	    },

		ScrollTo: function(a)
		{
			window.scrollTo(0, $(a).offset().top);
		},

		NotImplemented: function()
		{
			alert('Sorry, not implemented yet');
			return false;
		}
	}
}();
