// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
jQuery.ajaxSetup({
  beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})


$(function() {


	////
	//Mail check
	$('#contact_form').livequery(function(){
		$(this).validate();
	});

	////
	//PLUGIN SETUP - SETUP TO COVER ALL NEW ADDITIONS TO THE DOM
	$.facebox.settings.opacity = 0.3;
	$('a[rel*=facebox]').livequery(function(){
		$(this).facebox();
	});


	////
	//SETUP MOUSEOVER FADE EFFECT
	$('.admin_thumb').livequery(function(){
		$(this).hover(
		function(){
			$(this).animate({
				opacity:0.7
			},250,'easeOutQuint');
		},
		function(){
			$(this).animate({
				opacity:1
			},250,'easeOutQuint');
		});
	});

	$('.thumb img').livequery(function(){
		$(this).hover(
		function(){
			$(this).animate({
				opacity:0.7
			},250,'easeOutQuint');
		},
		function(){
			$(this).animate({
				opacity:1
			},250,'easeOutQuint');
		});
	});


	////
	//FADE IN IMAGES ON LOAD IN PAGINATION PANEL AND MAIN AREA
	$('.client_thumb_image, .full img, #image_meta').livequery(function(){
			$(this).fadeIn("normal");
	});


	////
	//AJAX CALL TO POPULATE ITEM INFORMATION
	$('.client_thumb_link').livequery('click', function(){
		$.get($(this).attr('href'),function(data){
			$('#item_detail').html(data);
		});
		return false;
	});


	////
	//Setup of the ajax thumbnail paginator
	$('a#thumb_next, a#thumb_back').livequery('click',function(){
		$.get($(this).attr('href'),function(data){
			$('#thumbspace_loader').html(data);
		});
		return false;
	});

	////
	//flash message
	$('#flash').livequery(function(){
		setTimeout(function(){
			$('#flash').hide("fast");
		},1500);
	});

	////
	//SETUP ROUNDED CORNER SHIZZLE
	$('.round').livequery(function(){
		$(this).corner();
	});
	$('.round_bottom').livequery(function(){
		$(this).corner({
				  tl: false,
				  tr: false,
				  bl: { radius: 5 },
				  br: { radius: 5 },
				  antiAlias: true,
				  autoPad: true,
				  validTags: ["div"] });
	});

	////
	//ADD NEW ITEM POP BOX
	$('#new_item').click(function() {
		$('#add_item_title').toggle("fast");
		$('#item_title').hint('blur', 'enter item title');
	});

	////
	//ADD NEW CATEGORY POP BOX
	$('#new_category').click(function() {
		$('#add_category_name').toggle("fast");
		$('#category_name').hint('blur', 'enter category name');
	});

	////
	//APPEND ADD NEW CATEGORY SELECT TO CAT SELECT
    // 	$('#item_category_id').livequery(function(){
    // 		$(this).append('<option value="!">+ add category</option>');
    // 	});


	////
	//UPDATE CATEGORY FOR CURRENT ITEM
	//AND ADD ON CLICK ADDITION TO ADD CATEGORY

	$("select#item_category_id").livequery(function(){
		$(this).change(function(){
			if ($(this).val() > 0){
				$.post($("#set_category_item_url").attr("href"), { category_id: $(this).val()},
				  function(data){
					$('#current_cat').html(data).Highlight();
				  });
			}
			if ($(this).val() == '!'){
				var new_cat = prompt("Enter new category");
				$.post($("#categories_url").attr("href"), { "category[name]": new_cat},
				  function(data){
					$('#category_list_a').html(data).Highlight();
				});
			}
		});
	});

	$(".add_to_cart").livequery(function(){
		$(this).click(function(){
			$.post($("#add_to_cart_url").attr("href"), {}, function(data){
				$.facebox(data);
			});
			return false;
		});
	});

	$("#link_tab").livequery(function(){
		$(this).hoverIntent(function(){
			$("#add_to_cart_effect").fadeIn(1);
		}, function(){
				$("#add_to_cart_effect").fadeOut(1);
			});
	});


	$('.cart_item_up, .cart_item_down').livequery(function(){
		$(this).click(function(){
			$.get($(this).attr("href"), {}, function(data){
				$('#ajax_cart').html(data)
			});
			return false;
		});
	});

	////
	//UPDATE IMAGE STATE
	$("select#image_state").livequery(function(){
		$(this).change(function(){
			$('#loading_spinner_item_state').show();
			$.post($("#set_im_state_item_image_url").attr("href"), { state_change: $(this).val()},
			  function(data){
				$('#loading_spinner_item_state').hide();
				$('#thumblist').html(data);
				$('#item_state').selectOptions("draft");//options[i].selected = true;;
			  });
		});
	});

	////
	//AJAX SPINNER FOR IMAGE UPLOAD
	$('#image_uploader').livequery(function(){
		$(this).submit(function(){
			$('#loading_spinner').show();
		});
	});

	$('#image_is_thumb').livequery(function(){
		$(this).change(function(){
			$('#loading_spinner_thumbnail').show();
			$.post($("#set_thumbnail_item_image_url").attr("href"), { thumbnail: $('#image_is_thumb')[0].checked},
			  function(data){
				$('#loading_spinner_thumbnail').hide();
				$('#thumblist').html(data);
			  });
		});
	});

	////
	//IMage preload
	$('#image img, img.client_thumb_image').livequery(function(){
		$.preload($(this), {
			placeholder:'/images/loading.gif',
			notFound:'/images/loading.gif',
			threshold:10
		});
	});
});

$.fn.selectOptions = function(value, clear)
{
	var v = value;
	var vT = typeof(value);
	var c = clear || false;
	// has to be a string or regular expression (object in IE, function in Firefox)
	if(vT != "string" && vT != "function" && vT != "object") return this;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return this;
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			for(var i = 0; i<oL; i++)
			{
				if(v.constructor == RegExp)
				{
					if(o[i].value.match(v))
					{
						o[i].selected = true;
					}
					else if(c)
					{
						o[i].selected = false;
					}
				}
				else
				{
					if(o[i].value == v)
					{
						o[i].selected = true;
					}
					else if(c)
					{
						o[i].selected = false;
					}
				}
			}
		}
	);
	return this;
};


$.fn.hint = function (blurClass, text_in) {
    if (!blurClass) blurClass = 'blur';

    return this.each(function () {
        // get jQuery instance of 'this'
        var $$ = $(this);

        // get it once since it won't change
        var title = text_in;//$$.attr('');

        // only apply logic if the element has the attribute
        if (title) {

            // Note this is a one liner

            // on blur, set value to title attr if text is blank
            $$.blur(function () {
                if ($$.val() == '') {
                    $$.val(title).addClass(blurClass);
                }
            })

            // on focus, set value to blank if current value matches title attr
            .focus(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            })

            // clear the pre-defined text when form is submitted
            .parents('form:first').submit(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            }).end()

            // now change all inputs to title
            .blur();

            // counteracts the effect of Firefox's autocomplete stripping the blur effect
            if ($.browser.mozilla && !$$.attr('autocomplete')) {
                setTimeout(function () {
                    if ($$.val() == title) $$.val('');
                    $$.blur();
                }, 10);
            }
        }
    });
};


