$(document).ready(function(){    
    
    $('input[type=text], input[type=password], select, textarea').formStyle();
    
    $('a[rel*=facebox]').facebox();
    
    $.fn.ddClearAndHide = function() {
        $(this).html('').parent().hide();      
    }

    $("#next_disabled[title]").tooltip();

    $(".fav").click(function(){
        var fav_pic = base_url() + 'img/fav.png';
        var nofav_pic = base_url() + 'img/nofav.png';

        var id = $(this).attr('id');

        var fav_url = base_url() + 'favs/delete/' + id;
        var nofav_url = base_url() + 'favs/add/' + id;
        
        // If current is not a fav, submit fav and change picture
        if ($(this).attr('href') == nofav_url) {

            // Send the get request to add the favorite
            $.get($(this).attr('href')); 
            
            // change the link to go to removing the fav
            $(this).attr('href', fav_url);
            
            // change the image to show already a fav pic
            $(this).children(":first").attr('src', fav_pic);
            
            // change the title and img alt text to say "Remove from favorites"
            $(this).attr('title', 'Remove from Favorites');
            $(this).children(":first").attr('alt', 'Remove from Favorites');
            
            // show message that it's been added to favorites
            $(this).append('<div class="response">Added to favorites</div>');
            $(this).children(".response").fadeIn("slow", function() {
                $(this).fadeOut("slow", function() {
                    $(this).remove();
                });
            });
            
            //show_timed_alert('This upload has been saved to your favorites!');
            
        } else {

            // Send the get request to delete the favorite
            $.get($(this).attr('href'));            
            
            // change the link to go to adding the fav
            $(this).attr('href', nofav_url);
            
            // change the image to show not a fav pic
            $(this).children(":first").attr('src', nofav_pic);

            // change the title and img alt text to say "Add to favorites"
            $(this).attr('title', 'Add to Favorites');
            $(this).children(":first").attr('alt', 'Add to Favorites');

            // show message that it's been added to favorites
            $(this).append('<div class="response">Removed from favorites</div>');
            $(this).children(".response").fadeIn("slow", function() {
                $(this).fadeOut("slow", function() {
                    $(this).remove();
                });
            });
            
            //show_timed_alert('This upload has been removed from your favorites!');
        }
        
        return false;
    });
    
    $(".good").click(function(){                    
        $.get($(this).attr('href'));
        $(".vote").fadeOut("fast", function() {
            $(".votes").fadeIn();
        });
        return false;
    });
    
    $(".bad").click(function(){                     
        $.get($(this).attr('href'));
        $(".vote").fadeOut("fast", function() {
            $(".votes").fadeIn();
        });
        return false;
    });
});

function required(x_this) {
    if($("#" + x_this).val().length < 1) {
        $("#" + x_this + "error").text("Required!");
        return false;
    } else {
        $("#" + x_this + "error").text("");
        return true;
    }
}

function validate_email(x_this) {
    
	var apos = $("#" + x_this).val().indexOf("@");
	var dotpos = $("#" + x_this).val().lastIndexOf(".");
    var len = $("#" + x_this).val().length;
    
    if($("#" + x_this).val().length < 1) {
        $("#" + x_this + "error").text("Required");
        return false;
    } else if (apos < 1 || dotpos-apos < 2 || dotpos == (len - 1) ) {
        $("#" + x_this + "error").text("Enter valid email");
		return false;
	}
	else {
		return true;
	}
}

//
// Find the first input element (input, select, textarea ... visible, not readonly, not hidden) 
// and set the input focus that element

function find_first_focus(x_locator) {
	setTimeout(function() { 
		//
		// This is in a timeout so that the loaded data has some time to become visible
		//
		x_list = jQuery(x_locator).filter(':not([readonly]):visible').add(jQuery(x_locator).find(':not([readonly]):visible'));	
		y_list = x_list.filter(function(idx) { return one_of(this.tagName,'INPUT,SELECT,TEXTAREA'); });

		if (y_list.size() > 0) y_list.eq(0).focus();
	}, 100);
}

function one_of(arg, arg_list) {
	return (',' + arg_list + ',').indexOf(',' + arg + ',') >= 0;
}

function show_timed_alert(msg) {
    $.facebox({html: msg, showClose: false});
    setTimeout( function() { $.facebox.close(); }, 1500);
}

function localize_url(url) {
    return base_url() + url;
}
