jQuery.noConflict();
function nospam(a, b, c, d) {
    var text;  
    var tg='<';
    var at='@';
    var dest=a+at+b+'.'+c;
    if(d=='' || !d) { text = dest; }
    else { text = d; }
    document.write(tg+'a hr'+'ef="mai'+'lto:'+dest+'">'+text+tg+'/a>');
}
function resend_verification_email() {
    jQuery('p#resend_verification_loading').removeClass('hidden');
    jQuery('#verification_email_controls').addClass('hidden');
    jQuery('#resend_verification_loading').load('/my/_ajax.email?method=resend');
}
function cancel_new_email_verification() {
    jQuery('#new_email_unconfirmed').slideUp();
    jQuery.post('/my/_ajax.email?method=cancel');
} 
function nospam_v2(a, b) {  
    var tg='<';
    var at='@';
    var dest=a+at+b;
    document.write(tg+'a hr'+'ef="mai'+'lto:'+dest+'">'+dest+tg+'/a>');
}
function show_more_saved_events() {
    jQuery('#show_more_saved_events_wrap').addClass('hidden');
    jQuery('ul#my_saved_events li[class~=hidden]').removeClass('hidden');
}
function show_more_fave_place_events() {
    jQuery('#show_more_fave_place_events_wrap').addClass('hidden');
    jQuery('ul#my_fave_place_events li[class~=hidden]').removeClass('hidden');
}
function show_more_venue_events() {
    jQuery('#show_more_venue_events_wrap').addClass('hidden');
    jQuery('ul#venue_events li[class~=hidden]').removeClass('hidden');
}
function save_place(id) {
    jQuery.post('/places/_ajax.save?action=save&id='+id);
    jQuery('a#save_place_button').attr('class','hidden');
    jQuery('a#unsave_place_button').attr('class','butt');
}
function unsave_place(id) {
    jQuery.post('/places/_ajax.save?action=unsave&id='+id);
    jQuery('a#save_place_button').attr('class','butt');
    jQuery('a#unsave_place_button').attr('class','hidden');
}
function save_event(id) {
    jQuery.post('/events/_ajax.save?action=save&id='+id);
    jQuery('a#save_event_button').attr('class','hidden');
    jQuery('a#unsave_event_button').attr('class','butt');
}
function unsave_event(id) {
    jQuery.post('/events/_ajax.save?action=unsave&id='+id);
    jQuery('a#save_event_button').attr('class','butt');
    jQuery('a#unsave_event_button').attr('class','hidden');
}
function events_search() {
    jQuery('#events_search').submit();
}
function places_search() {
    jQuery('#places_search').submit();
}
function create_account(url) {
    jQuery('#signup_form').attr('action',url).submit();
}
function login(url) {
    jQuery('#login_form').attr('action',url).submit();
}
function change_my_password() {
    jQuery('.password_fields').removeClass('hidden');
    jQuery('#password_change_text').addClass('hidden');
}
function reset_password_start() {
    var data = jQuery('#forgot_password_form').serializeArray();
    jQuery('#forgot_check p').load('/_forgot.php', data, function(){
        jQuery('#forgot_stuff').addClass('hidden');
        jQuery('#forgot_check').removeClass('hidden');
    });
}
function reset_password_try_again() {
    jQuery('#forgot_check').addClass('hidden');
    jQuery('#forgot_stuff').removeClass('hidden');
}
function send_another_new_acount_letter() {
    jQuery('#verify_alert_inner p').removeClass('hidden');
    jQuery.post('/my/_ajax.verify_v2.php', function(data) {
        jQuery('#verify_alert_inner').addClass('hidden');
        jQuery('#new_verification_inner').removeClass('hidden');
    })
}

function edit_my_settings() {
    jQuery('#my_settings').addClass('hidden');
    jQuery('#my_settings_editor').removeClass('hidden');
}
function cancel_edit_my_settings() {
    jQuery('#my_settings').removeClass('hidden');
    jQuery('#my_settings_editor').addClass('hidden');
}
function save_my_settings(url) {
    jQuery('#my_settings_form').attr('action',url).submit();
}

function forgot_password() {
    jQuery('#forgot_wrap').slideDown('hidden');
    jQuery('#forgot_button').addClass('hidden');
    jQuery('input#forgot_email').focus();
}
function cancel_forgot_password() {
    jQuery('#forgot_wrap').slideUp('hidden');
    jQuery('#forgot_button').removeClass('hidden');
    jQuery('input#forgot_email').blur();
}
function prefill_venue_info(data) {
    var obj = jQuery.parseJSON('{'+data+'}');
    jQuery('#street').removeClass('default').val(obj.street);
    jQuery('#city').removeClass('default').val(obj.city);
    jQuery('#state').removeClass('default').val(obj.state);
    jQuery('#zip').removeClass('default').val(obj.zip);
    jQuery('#venueid').removeClass('default').val(obj.venueid);
    if(jQuery('span#venueid_span').length) {
        jQuery('span#venueid_span').removeClass('hidden').html('<strong>Venue ID:</strong> '+obj.venueid);
    }
    //if(jQuery('#contactnumber').val() == '') {
        jQuery('#contactnumber').val(obj.phone);
    //}
}
function flag_event(id) {
    var flag_ok = true;
    if(jQuery("[@name='flag_type']:checked").val()) {
        /*var other_reason = jQuery.trim('p#other_flag_reason textarea').val();
        if((jQuery("input#other_flag").is(":checked")) && (other_reason == '' || other_reason == 'Reason for flag...' || other_reason == 'Additional details...')) {
            flag_ok = false;
        }
        else {
            flag_ok = true;
        }*/
        if(flag_ok) {
            if(confirm('Are you sure you want to flag this event?')) {
                var data = jQuery('#flag_form').serializeArray();
                jQuery('#event_flag_box').html('<p class="intro">Thank you. We will update or remove this listing if necessary.</p>');
                jQuery.post('/events/_ajax.flag?id='+id, data);
            }
        }
        else {
            alert('Please provide a more detailed reason for flagging this event');
        }
    }
    else {
        alert('Please select a reason for flagging this event');
    }
}
function flag_venue(id) {
    var flag_ok = true;
    if(jQuery("[@name='flag_type']:checked").val()) {
        /*var other_reason = jQuery.trim('p#other_flag_reason textarea').val();
        if(jQuery(".flag_box ul li input#other_flag").is(":checked") && (other_reason == '' || other_reason == 'Reason for flag...' || other_reason == 'Additional details...')) {
            flag_ok = false;
        }*/
        if(flag_ok) {
            if(confirm('Are you sure you want to flag this venue?')) {
                var data = jQuery('#flag_form').serializeArray();
                jQuery('#venue_flag_box').html('<p class="intro">Thank you. We will update or remove this listing if necessary.</p>');
                jQuery.post('/places/_ajax.flag?id='+id, data);
            }
        }
        else {
            alert('Please provide a more detailed reason for flagging this venue');
        }
    }
    else {
        alert('Please select a reason for flagging this venue');
    }
}
/***************************************************
STRIP HTML TAGS
****************************************************/
function strip_tags(html){

    //PROCESS STRING
    if(arguments.length < 3) {
        html=html.replace(/<\/?(?!\!)[^>]*>/gi, '');
    } else {
        var allowed = arguments[1];
        var specified = eval("["+arguments[2]+"]");
        if(allowed){
            var regex='</?(?!(' + specified.join('|') + '))\b[^>]*>';
            html=html.replace(new RegExp(regex, 'gi'), '');
        } else{
            var regex='</?(' + specified.join('|') + ')\b[^>]*>';
            html=html.replace(new RegExp(regex, 'gi'), '');
        }
    }

    //CHANGE NAME TO CLEAN JUST BECAUSE 
    var clean_string = html;

    //RETURN THE CLEAN STRING
    return clean_string;
}
function add_new_event() {
    var ok = true;
    if(!jQuery('#eventname').val() || !jQuery('#description').val() || !jQuery('#venuename').val() || !jQuery('#street').val() || !jQuery('#city').val() || !jQuery('#state').val()) {
        ok = false;
        alert("Please complete all required fileds: \n\nEvent Title, Venue Name, Street, City, State, Description");
    }
    var description = strip_tags(jQuery('#description').val());
    var description_uppercase = description.toUpperCase();
    if(ok && description == description_uppercase) {
        ok = false;
        alert("It looks like the description you have entered \nis typed in ALL CAPS...\n\nPlease don't do that.");
    }
    if(ok && jQuery('#profile_image').val()) {
        var ext = jQuery('#profile_image').val().split('.').pop().toLowerCase(); 
        var allow = new Array('gif','png','jpg','jpeg'); 
        if(jQuery.inArray(ext, allow) == -1) {
            ok = false;
            alert("Please upload a supported file type: \n\n(We support the following extensions: jpeg, jpg, gif, png)");
        }        
    }
    if(ok) {
        jQuery('#add_event_form').attr('action','/events/add?action=add_new');
        jQuery('#add_new_event_button').addClass('hidden');
        if(jQuery('#profile_image').val()) {
            jQuery('#add_new_event_loading span').text('Please be patient while your image is being uploaded...');
        }
        jQuery('#add_new_event_loading').removeClass('hidden');
        jQuery('#add_event_form').submit();
    }
}
function add_new_venue() {
    var ok = true;
    if(!jQuery('#venuetitle').val() || !jQuery('#street').val() || !jQuery('#city').val() || !jQuery('#state').val()) {
        ok = false;
        alert("Please complete all required fileds: \n\nVenue Name, Street, City, State");
    }
    if(ok && jQuery('#profile_image').val()) {
        var ext = jQuery('#profile_image').val().split('.').pop().toLowerCase(); 
        var allow = new Array('gif','png','jpg','jpeg'); 
        if(jQuery.inArray(ext, allow) == -1) {
            ok = false;
            alert("Please upload a supported file type: \n\n(We support the following extensions: jpeg, jpg, gif, png)");
        }        
    }
    if(ok) {
        jQuery('#add_venue_form').attr('action','/places/add?action=add_new');
        jQuery('#add_new_venue_button').addClass('hidden');
        if(jQuery('#profile_image').val()) {
            jQuery('#add_new_venue_loading span').text('Please be patient while your image is being uploaded...');
        }
        jQuery('#add_new_venue_loading').removeClass('hidden');
        jQuery('#add_venue_form').submit();
    }
}
function contact_us() {
    var ok = true;
    if(!jQuery('#name').val() || !jQuery('#email').val() || !jQuery('#subject').val() || !jQuery('#message').val()) {
        ok = false;
        alert("Please complete all required fileds: \n\nName, Email, Subject, Message");
    }
    if(ok) {
        jQuery('#contact_us_form').attr('action','/contact?action=submit_form');
        jQuery('#contact_us_form').submit();
    }
}
function advertise_with_us() {
    var ok = true;
    if(!jQuery('#name').val() || !jQuery('#email').val() || !jQuery('#phone').val() || !jQuery('#message').val()) {
        ok = false;
        alert("Please complete all required fileds: \n\nName, Email, Phone, Details");
    }
    if(ok) {
        jQuery('#advertise_form').attr('action','/advertise/?action=submit_form');
        jQuery('#advertise_form').submit();
    }
}
function add_another_event() {
    jQuery('#success_wrap').addClass('hidden');
    jQuery('#form_wrap').removeClass('hidden');
    jQuery('#eventname').focus();
}
function transfer_to_amazon(id) {
    jQuery('#transfer_link').html('Transferring... please be patient.');
    jQuery.post('/events/_ajax.transfer_photo?eventid='+id, function() {
        jQuery('#transfer_link').html('Done!');
    });
}
function transfer_to_amazon_v2(id) {
    jQuery('#transfer_link').html('Transferring... please be patient.');
    jQuery.post('/places/_ajax.transfer_photo?venueid='+id, function() {
        jQuery('#transfer_link').html('Done!');
    });
}
function remove_photo(id) {
    jQuery('#remove_photo_link').html('Removing... please wait.');
    jQuery.post('/events/_ajax.remove_photo?eventid='+id, function() {
        jQuery('#existing_photo').addClass('hidden');
        jQuery('#new_photo').removeClass('hidden');
    });
}

function remove_photo_v2(id) {
    jQuery('#remove_photo_link').html('Removing... please wait.');
    jQuery.post('/places/_ajax.remove_photo?venueid='+id, function() {
        jQuery('#existing_photo').addClass('hidden');
        jQuery('#new_photo').removeClass('hidden');
    });
}
function ucwords(str) {
    // Uppercase the first character of every word in a string  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/ucwords
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Waldo Malqui Silva
    // +   bugfixed by: Onno Marsman
    // +   improved by: Robin
    str = str.toLowerCase();
    return (str + '').replace(/^(.)|\s(.)/g, function ($1) {
        return $1.toUpperCase();
    });
}
jQuery(function(){
    jQuery(".social .facebook a").click(function() {
        window.open(jQuery(this).attr("href"),"sharer","toolbar=0,status=0,resizable=1,width=626,height=436");
        return false;
    });
    if(jQuery('input#eventname').length) {
        jQuery("input#eventname").live("blur", function () {
            var eventname = jQuery('input#eventname').val();
            var eventnameUpperCase = eventname.toUpperCase();
            if(eventname == eventnameUpperCase) {
                var new_eventname = ucwords(eventname);
                jQuery('input#eventname').val(new_eventname);
            }
        });
    }
    if(jQuery('p.multi_categories').length) {
        jQuery('p.multi_categories').duplicate_remove({
            'limit' : 3
        });
    }
    if(jQuery('p.gallery_images').length) {
        jQuery('p.gallery_images').duplicate_remove({
            'limit' : 5
        });
    }
    if(jQuery('p.media_inputs').length) {
        jQuery('p.media_inputs').duplicate_remove({
            'limit' : 3
        });
    }
    jQuery(".flag_box ul li input, .flag_box ul li label").click(function () {
        if(jQuery("[@name='flag_type']:checked").is(":checked")) {
            if(jQuery(".flag_box ul li input#other_flag").is(":checked")) {
                jQuery(".flag_box p#other_flag_reason textarea").val("Reason for flag...");            
            }
            else {
                jQuery(".flag_box p#other_flag_reason textarea").val("Additional details...");            
            }
            jQuery(".flag_box p#other_flag_reason").removeClass("hidden");
        }
        else {
            jQuery(".flag_box p#other_flag_reason").addClass("hidden");
            jQuery(".flag_box p#other_flag_reason input").val("Additional details...");
        }
    });
    jQuery("label#show_ongoing_label").click(function () {
        if(jQuery("input#show_ongoing").is(":checked")) {
            jQuery("input#show_ongoing").attr('checked','').val('');
            jQuery("#show_ongoing_form").submit();
        }
        else {
            jQuery("input#show_ongoing").attr('checked','checked').val('yes');
            jQuery("#show_ongoing_form").submit();
        }
    });
    jQuery("input#show_ongoing").click(function () {
        if(jQuery("input#show_ongoing").is(":checked")) {
            jQuery("input#show_ongoing").attr('checked','checked').val('yes');
            jQuery("#show_ongoing_form").submit();
        }
        else {
            jQuery("input#show_ongoing").attr('checked','').val('');
            jQuery("#show_ongoing_form").submit();
        }
    });
    jQuery(".datepicker").live("click", function() {
        var dp_id = jQuery(this).attr("id");
        var min_date = (jQuery(this).hasClass("upcoming")) ? "+0" : null;
        var max_date = (jQuery(this).hasClass("past")) ? "+0" : null;
        if(dp_id == "start_date") {
            jQuery(this).datepicker({ showOn: 'focus', duration: 'fast', altField: '#end_date', dateFormat: 'mm/dd/yy', minDate: min_date, maxDate: max_date }).focus();
        }
        else {
            jQuery(this).datepicker({ showOn: 'focus', duration: 'fast', dateFormat: 'mm/dd/yy', minDate: min_date, maxDate: max_date }).focus(); 
        }
    });
    if(jQuery("#start_time,#end_time").length) {
        jQuery("#start_time,#end_time").timePicker({ show24Hours: false});
    }
    if(jQuery('.default').length) {
        jQuery("input.search('.default')").live("focus", function () {
            if(jQuery(this).val() == 'Search') { jQuery(this).val(''); jQuery(this).removeClass('default'); }
        });
        jQuery("input.search('.default')").live("blur", function () {
            if(jQuery.trim(jQuery(this).val()) == '') { jQuery(this).val('Search'); jQuery(this).addClass('default'); }
        });
        jQuery("textarea#street('.default')").live("focus", function () {
            if(jQuery(this).val() == 'Street') { jQuery(this).val(''); jQuery(this).removeClass('default'); }
        });
        jQuery("textarea#street('.default')").live("blur", function () {
            if(jQuery.trim(jQuery(this).val()) == '') { jQuery(this).val('Street'); jQuery(this).addClass('default'); }
        });
        jQuery("input#city('.default')").live("focus", function () {
            if(this.value == 'City') { this.value = ''; jQuery(this).removeClass('default'); }
        });
        jQuery("input#city('.default')").live("blur", function () {
            if(jQuery.trim(this.value) == '') { this.value = 'City'; jQuery(this).addClass('default'); }
        });
        jQuery("input#state('.default')").live("focus", function () {
            if(this.value == 'State') { this.value = ''; jQuery(this).removeClass('default'); }
        });
        jQuery("input#state('.default')").live("blur", function () {
            if(jQuery.trim(this.value) == '') { this.value = 'State'; jQuery(this).addClass('default'); }
        });
        jQuery("input#zip('.default')").live("focus", function () {
            if(this.value == 'Zip') { this.value = ''; jQuery(this).removeClass('default'); }
        });
        jQuery("input#zip('.default')").live("blur", function () {
            if(jQuery.trim(this.value) == '') { this.value = 'Zip'; jQuery(this).addClass('default'); }
        });
        jQuery("input#website_official('.default'),input#website_tickets('.default'),input#website('.default')").live("focus", function () {
            if(this.value == 'http://') { jQuery(this).removeClass('default'); }
        });
        jQuery("input#website_official('.default'),input#website_tickets('.default'),input#website('.default')").live("blur", function () {
            if(jQuery.trim(this.value) == '' || jQuery.trim(this.value) == 'http://') { this.value = 'http://'; jQuery(this).addClass('default'); }
        });
        jQuery("textarea#other_reason('.default')").live("focus", function () {
            if(this.value == 'Additional details...' || this.value == 'Reason for flag...') { this.value = ''; jQuery(this).removeClass('default'); }
        });
        jQuery("textarea#other_reason('.default')").live("blur", function () {
            if(jQuery.trim(this.value) == '') { this.value = 'Additional details...'; jQuery(this).addClass('default'); }
        });
    }
    if(jQuery('#venuename').length) {
        jQuery('#venuename').autocomplete({
            url: '/places/search?action=ajax_search',
            onSelectItem: function(value, data) {            
                //jQuery('#venuename').val('').blur();
                prefill_venue_info(data);
            } 
        });
    }
});