$(document).ready(function(){
	$('.hide-me').animate({opacity: 1.0}, 3000).slideUp('slow');

	//example for the login form
	$('form.login input').each(function(){
		label = $('label[for="'+$(this).attr('id')+'"]', $('form.login')).hide().text();
		$(this).defaultValue(label);
	});

	//A.DEFINITION LIKE JQUERY UI HELP
	$('a.definition').each(function(){
		$(this).addClass('ui-icon ui-icon-help ui-state-hover ui-priority-secondary ui-corner-all');
	});

	//QTIP
	//acronyms and links that just have images (user menus)
	$('a[href][title]:has(img:only-child), acronym').each(function(){
		$(this).data("popup_title", $(this).attr('title'));
		$(this).qtip({
			content: {
				text: false // Use each elements title attribute
			},
			position: {
				adjust: {
					screen: true,
					x:2
				},
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomMiddle'
				}
			},
			style: 'reentry' // Style it according to the defined 'reentry' style at the bottom
		});
	});
	//target external links to _blank and do a preview
	$('a[href^="http"], a.thumb').attr('target','_blank').not('.map').each(function(){
		/* websnapr
		var content = '<img src="http://images.websnapr.com/?url=';
		content += $(this).attr('href');
		content += '" alt="Loading thumbnail..." height="152" width="202" />';
		*/
		//ArtViper
		var content = '<img src="http://www.artviper.net/screenshots/screener.php?url=';
		content += $(this).attr('href');
		content += '&w=202&q=90&sdx=1024&userID=77448486f18c1219&email=aron.duby@gmail.com" width="202" alt="Loading Preview... click to visit the site" />';
		$(this).qtip({
			content: content,
			position: {
				adjust: {
					screen: true,
					x:2
				}
			},
			style: 'reentry'
		});
	});
	
	//MAKE DIALOG LIKE A LIGHT BOX
	$('a.ajax_load').live("click", function(e){
		$('#ajax_loader_master').clone(true).attr({'id':'ajax_loader'}).dialog({
			title: 'loading',
			bgiframe: true,
			modal: true,
			stack:true,
			autoOpen:true,
			resizable:false,
			close:function(event, ui){
				$(this).remove();
			}
		});
		
		url = $(this).attr('href');
		title = $(this).data("popup_title") ? $(this).data("popup_title") : $(this).attr('title');

		$.ajax({
			url: url,
			success: function(data, status){
				$ajax_loader = $('#ajax_loader');
				//add the content, set the title, add cancel button
				$ajax_loader.html(data).dialog('option', 'title', title);
				//ui-ify any submit buttons
				$('input[type="submit"]', $ajax_loader).addClass('ui-state-default ui-corner-all').hover(
					function(){
						$(this).addClass('ui-state-hover');
					},
					function(){
						$(this).removeClass('ui-state-hover');
					}
				).css({'text-transform':'capitalize', 'padding':'4px 6px', 'cursor':'pointer' });
				//resize to the content holder and then reposition
				$ajax_loader.dialog('option', 'width', $('.holder', $ajax_loader).width()+30).dialog('option', 'position', 'center');

			},
			error: function(obj, status, error){
				$('#ajax_loader').html('<p>Page could not be loaded.</p>').dialog('option', 'title', 'Error').dialog('option', 'buttons', {"Close": function() { $(this).dialog("close"); }});
			}
		});
	
		return false;
	});

	
	//TRACK SEARCH CATEGORIES AS SEPERATE GOOGLE ANALYTIC EVENTS
	//event category will be Search and event will the the category
	//get the selected categories from the search form
	$('form#search_form_left input[name="categories[]"]:checked').each(function(){
		var id = $(this).attr('id');
		var tracked = pageTracker._trackEvent("Search", id);
	});

});//end document onload


function makeDialog(text, dialog_title, buttons){
	if(!buttons){
		buttons = {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	}

	$('<div id="made_dialog">'+text+'</div>').dialog({
		title: dialog_title,
		dialogClass: 'alert',
		bgiframe: true,
		modal: true,
		stack:true,
		autoOpen:true,
		width:460,
		minHeight:220,
		draggable: true,
		resizable: true,
		buttons: buttons
	});
}

//QTIP STYLES
$.fn.qtip.styles.reentry = { // Last part is the name of the style
	tip: true, // Give it a speech bubble tip with automatic corner detection
	textAlign: 'center',
	background: '#fefefe',
	color: '#ae632b',
	border: {
		width: 2,
		radius: 5,
		color: '#ae632b'
	},
	name: 'cream' // Inherit the rest of the attributes from the preset dark style
};
$.fn.qtip.styles.delete_feed_item = {
	//padding: '5px',
	color: 'white',
	tip: false, //get rid of the tip
	border: {
		width: 2,
		radius: 5,
		color: '#fff'
	},
	name: 'light'
};
$.fn.qtip.styles.confirm_delete = {
	border: {
		width: 2,
		radius: 5,
		color: '#CE6F6F'
	},
	name: 'red'
};

//php number_format to js
//http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/#comment_2946
function number_format(number, decimals, dec_point, thousands_sep) {
    var n = number, prec = decimals;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
    var dec = (typeof dec_point == "undefined") ? '.' : dec_point;
 
    var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = Math.abs(n).toFixed(prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
 
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
 
    return s;
}

//CATCH CONSOLE.LOG FOR NO-FIREBUG
if (typeof console == 'undefined' || typeof console.log == 'undefined') { console = { log : function (text) { return false; } } }

window.alert = function(text){
	makeDialog(text, 'The Page Says:');
}