$(document).ready(function(){

	$('a.pop-opener').click(function(){
		
		$('#' + $(this).attr('rel')).toggle();
		return false;
	});
	
	$('a.tell-a-friend').click(function() { 

        $.blockUI({ 
					message: $('#tell-a-friend-wrap'), 

					css: { 
								top: '0px', 
								left: '50px', 
								width: '700px', 
								border: '10px solid #8CAA20',
								borderTop: 'none',
								backgroundColor: '#EAE8E2' 
						} 
		});
		
		$('#tell-a-friend-wrap').load('/notifications/tell_a_friend/', { title: document.title, url: window.location }, function(){
			
			$('#tell-a-friend-cancel').click(function(){
				
				$.unblockUI();
				return false;
			});

			$('#taf-name').focus();

			$('#tell-a-friend-form').submit(function(){
				
				var vals = $(this).serializeArray();
				$.post('/notifications/tell_a_friend/', vals, function(data){
					
					if (data.status < 1){
						
						alert(data.errors);
					
					} else {
						
						window.location = window.location;
					}

				}, 'json');

				return false;
			});
		});
    });

	// search
	search_origs = { 1 : 'search by city or keyword', 2 : 'or by zip code' };
	$('#search-1').val(search_origs[1]);
	$('#search-2').val(search_origs[2]);
	$('input.search').focus(function(){
		
		val = $(this).val();
		ind = $(this).attr('id').substr(7, 1);

		if (val == search_origs[ind]){
			
			$(this).val('');
		}
	});
	$('input.search').blur(function(){
		
		val = $(this).val();

		if (val == ''){
			
			ind = $(this).attr('id').substr(7, 1);
			$(this).val(search_origs[ind]);
		}
	});

	$('#advanced-search-opener').click(function(){
		
		$('#header-ad').hide();
		$('#header-pitch').hide();
		$('#search-mini').hide();
		$('#search-advanced').show();
		return false;
	});

	$('#advanced-search-closer').click(function(){
		
		$('#search-advanced').hide();
		$('#header-ad').show();
		$('#header-pitch').show();
		$('#search-mini').show();
		return false;
	});

	$('#login-opener').click(function(){
		
		$('#login-info').hide();
		$('#login-quick-form').show();
		$('#username-quick').focus();
		return false;
	});

	$('#login-closer').click(function(){
		
		$('#login-quick-form').hide();
		$('#login-info').show();
		return false;
	});

	$('#new-acct-button').click(function(){
		
		go(this.src);
		return false;
	});
	
	$('#floating-menu-close').click(function(){
		
		$('#floating-menu').hide();											 
	 });
	
	$('select.mover').change(function(){
		
		go($(this).val());
	});

	if ($("table.sortable").length > 0){

		$("table.sortable").tablesorter({ widgets: ['zebra'] });
	}

	$('#bsubmit_home, #bsubmit').click(function(){
		
		window.location = '/submit/an_event/';
	});

	$('a.replacer').click(function(){
		
		$('#' + $(this).attr('rel')).load($(this).attr('href'));
		return false;
	});
	
	$('#images-upload').click(function(){
						 
		$('img.loader').show();
		$(this).hide();
	});
	
});

function go(target){

	window.location = target;
}

function count_chars(field, maximum) {
	// JABO
	// Checks & indicates if input over max chars allowed
	// Field = id of text field; 
	// Outputs char count to element on page with id = field + "-char-count"
	
	field = "#" + field;
	var status = field + "-char-count";
	var value = $(field).val();
	
	if(value.length > maximum) {
		
		$(field).val(value.substring(0, maximum));
		alert("The text you just entered is over " + maximum + " characters. Please limit it or it will be truncated...");
	
	} else { 

		if(maximum - value.length < 1) {
			$(status).html("0");
			
		} else { 
			$(status).html(maximum - value.length); 
		}
		
	}
	 
}

function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)    
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
 
    var __regexp_escape = function(text) {
        if (!arguments.callee.sRE) {
            var specials = [
                '/', '.', '*', '+', '?', '|',
                '(', ')', '[', ']', '{', '}', '\\'
            ];
            arguments.callee.sRE = new RegExp(
                '(\\' + specials.join('|\\') + ')', 'g'
            );
        }
      return text.replace(arguments.callee.sRE, '\\$1');
    };
 
    var numreplx, numon, fincods, k;
   
    if(!(replace instanceof Array)){
        replace = new Array(replace);
        if(search instanceof Array){
            // If search is an array and replace is a string, 
            // then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array)){
        // put search string in an array anyway
        search = new Array( search );
    }
    while( search.length > replace.length ){ 
        // If replace has fewer values than search, 
        // then an empty string is used for the rest of replacement values
        replace[replace.length] = '';
    }
 
    if(subject instanceof Array){
        // If subject is an array, then the search and replace is performed 
        // with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k] = str_replace(search, replace, subject[k]);
        }
        return subject;
    }
      
    // Each entry is replaced all at once, rather than one after another. 
    // This creates a problem: str_replace(["{name}","l"], ["hello","m"], "{name}, lars")
    // Theoretically, the code should return "hello, mars", but instead it returns "hemmo, mars"
    // as pointed out and fixed by Philip Peterson
    numreplx = search.length;
    numon = 0;
    fincods = new Array();
    while( fincods.length < numreplx ){
        nsub = subject;
        for( x = 0; x < fincods.length; x++ ){
            nsub = nsub.replace(new RegExp(__regexp_escape(search[x]), "g"), "[cod"+fincods[x]+"]");
        }
        for( x = 0; x < fincods.length; x++ ){
            nsub = nsub.replace(new RegExp(__regexp_escape("[cod"+fincods[x]+"]"), "g"), replace[x]);
        }
        if( nsub.indexOf("[cod"+numon+"]") == -1 ){
            fincods[fincods.length]=numon;
        }
        numon++;
    }
    for( x = 0; x < fincods.length; x++ ){
        subject=subject.replace(new RegExp(__regexp_escape(search[x]), "g"), "[cod"+fincods[x]+"]");
    }
    for( x = 0; x < fincods.length; x++ ){
        subject=subject.replace(new RegExp(__regexp_escape("[cod"+fincods[x]+"]"), "g"), replace[x]);
    }
    return subject;
}

function isset(  ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // *     example 1: isset( undefined, true);
    // *     returns 1: false
    // *     example 2: isset( 'Kevin van Zonneveld' );
    // *     returns 2: true
 
    var a=arguments; var l=a.length; var i=0;
    
    while ( i!=l ) {
        if (typeof(a[i])=='undefined') { 
            return false; 
        } else { 
            i++; 
        }
    }
    
    return true;
}