// Add category (news manager)
function promptToRemote(text, param, url) {
	value = prompt(text + ':');
	if (value) {
		$('.addCategory').after(' <img src="/default/images/spinner.gif" class="loading" align="absmiddle" alt="" />');
		$('.showError').remove();
		new $.ajax({
			url      : url + '?' + param + '=' + encodeURIComponent(value),
			dataType : 'json',
			success  : function(results){
				$('img.loading').remove();
				if (results['SUCCESS']) {
					$('#article_form_categories').addOption(results['DATA'], false);
					$('#article_form_categories').sortOptions(true);
					for (i in results['DATA'])
						$('#article_form_categories').selectOptions(i);
				} else {
						$('.addCategory').after('<span class="showError">' + results['ERRORS'][0] + '</span>');
				}
			}
		});
	}
	return false;
}

// Add tag (news manager)
function promptToRemoteTag(text, param, url) {
	value = prompt(text + ':');
	if (value) {
		$('.addTag').after(' <img src="/default/images/spinner.gif" class="loading" align="absmiddle" alt="" />');
		$('.showError').remove();
		new $.ajax({
			url      : url + '?' + param + '=' + encodeURIComponent(value),
			dataType : 'json',
			success  : function(results){
				$('img.loading').remove();
				if (results['SUCCESS']) {
					$('#article_form_tags').addOption(results['DATA'], false);
					$('#article_form_tags').sortOptions(true);
					for (i in results['DATA'])
						$('#article_form_tags').selectOptions(i);
				} else {
						$('.addTag').after('<span class="showError">' + results['ERRORS'][0] + '</span>');
				}
			}
		});
	}
	return false;
}

$(function() {
		 
	// Systems messages and alerts
	$('.ajaxInfoBar').fadeIn('normal');
	$('.ajaxInfoBar').animate({opacity: 1.0}, 4000);
	$('.success').fadeOut('normal', function() {
		$(this).remove();
	});
	
	// Open in new window
	$("a[rel='external']").attr("target","_blank");

	$("table.index thead tr th:has(a)").mouseover(function(){
		$(this).addClass('cursorOn');
		$(this).children('a').css('text-decoration','none');
	}).mouseout(function(){
		$(this).children('a').css('text-decoration','underline');
		});
		
	$("table.index thead tr th:has(a)").click(function(){
		window.location = $(this).children('a').attr('href');	
	});
	
	// Table row hover highlight
	$("table.index tbody tr").mouseover(function(){
		$(this).addClass("highlight");
	}).mouseout(function(){
		$(this).removeClass("highlight");
	});
	
	$('#searchForm').submit(function(){
		$('.orderSearch').addClass("searchActive");
	});
	
	// Image Button Hovers
	$(".rollover").hover(
		function(){
			if($(this).attr("src").indexOf("_on") == -1) {
				var newSrc = $(this).attr("src").replace(".png","_on.png#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_on.png#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_on.png#hover",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);

	$('.addCategory').click(function(){
		promptToRemote('New category', 'title', '/app/auth/articles/categories/update_ajax/new');
		return false;
	});
		
	$('.addTag').click(function(){
		promptToRemoteTag('New tag', 'name', '/app/auth/articles/tags/update_ajax/new');
		return false;
	});
		
	// Setup any datepicker fields
	if (typeof jQuery.prototype.datepicker != 'undefined'){
		$(".txtFieldDate").datepicker({ 
				dateFormat: "yy-mm-dd",
				showOn: "both", 
				buttonImage: "/default/assets/jquery.ui.datepicker/images/calendar.png", 
				buttonImageOnly: true 
		});
		$(".txtFieldDateOfBirth").datepicker({ 
				dateFormat: "yy-mm-dd",
				showOn: "both", 
				buttonImage: "/default/assets/jquery.ui.datepicker/images/calendar.png", 
				buttonImageOnly: true,
                yearRange: '-150:+0'
		});
	}
			
  var tog = false;
	$('.selectAll').click(function(){
		tog = !tog;
		$('.selectAllEntry').each(function(){
			$(this).attr('checked',tog);
		});
		if (tog == true)
			$(this).html('UnSelect All');
		else
			$(this).html('Select All');
		return false;
	});
	$('.selectAllForm').submit(function(){
		ids = new Array();
		$('.selectAllEntry').each(function(){
			if ($(this).attr('checked')) {
				ids.push($(this).attr('value'));
			}
		});
		$('#selectAllIDs').attr('value', ids.join('|'));
		return true;
	});


	// Power Search - Select Group Dropdown
	
	$('a#toggleDropdown').click(function(){
		var e = $(this).next('div');
		if(e.is(':hidden')){
			$('.powerSearchOptions').fadeIn('fast');
		}else{
			$('.powerSearchOptions').fadeOut('fast');
		}
		return false;
	});
	
	$(".powerSearchOptions input").click(function(){
		var text = $(this).attr('title');
		$("a#toggleDropdown span").html(text);
		$(".powerSearchOptions").fadeOut('fast');
	});
	
	$(document).bind('click',function(e){
		var clicked = $(e.target);
		if (! clicked.parents().hasClass("powerSearchOptions"))
			$(".powerSearchOptions").fadeOut('fast');
	});


});

// Plugin - Text Field Hint
// $("#s").search();

(function($){
	if (typeof jQuery.prototype.search != 'undefined'){
		$(".orderSearch").search();
	}
})(jQuery);

$.fn.search = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			$(this).css({color:"#444"});
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			$(this).css({color:"#999"});
			this.value = this.defaultValue;
		}
	});
};



