$(document).ready(function () {
	if ($('input[type="text"]').length > 0) initInputs();
	if ($('input[type="password"]').length > 0) initPasswordInputs();

	if ($("div.gallery").length > 0) {
		$("div.gallery img").first().addClass("start");
		$("div.gallery img").slidingGallery({
			container: $("div.gallery"),
			Lheight: 192,
			Lwidth: 256,
			Lshrink: function (dimension) { return dimension * 0.5 },
			gutterWidth: 10
		});
	}

	$(".category-selector").click(function () {
		$(this).nextAll("input[type='checkbox']").click();
	});

	$("img[data-src]").each(function () {
		$(this).attr("src", $(this).attr("data-src"));
	});
});

function initializeFileUpload(options) {
	var showUploadButton = function() {
		$("#upload-button").show();
	};
	if (swfobject.getFlashPlayerVersion().major >= 9) {
		$('#grid-bundle-file').uploadify({
			'uploader': '/assets/uploadify/uploadify.swf',
			'script': options.script,
			'cancelImg': '/assets/uploadify/cancel.png',
			'buttonText': options.buttonText,
			'scriptData': options.scriptData,
			'auto': true,
			'fileExt': '*.bdl',
			sizeLimit: 100000000,
			hideButton: true,
			wmode: 'transparent',
			'fileDesc': 'Grid Bundle Files',
			'onComplete': function (event, id, fileObj, response, data) {
				$("#upload-form").html(options.processingMessage);
				window.location.href = response;
			},
			onCancel: showUploadButton,
			onError: function () {
				alert("There was an error uploading the file. Please confirm that it is a valid grid bundle file, and try again.");
				showUploadButton();
			},
			onOpen: function () { $("#upload-button").hide(); }
		});
		$("#upload").hide();
	}
}

function initPasswordInputs() {
	
	$('input[type="password"][title!=""]').each(function() {
	    
	    var cInput = $(this).hide().blur(inputBlur);
	    
	    var cDummy = $('<input type="text" />').attr({ 
	        'title': cInput.attr('title'), 
	        'class': cInput.attr('class') + ' default', 
	        'value': cInput.attr('title') }).focus(dummyFocus).insertAfter(cInput);
    	
    	function dummyFocus() {
    	    $(this).hide();
    		cInput.show().focus();
    	}
    	
    	function inputBlur() {
    	    if ($(this).val() == '') {
    	        $(this).hide();
	            cDummy.show();
	        }
    	}
	    
    });
	
}

function initInputs() {
	
	$('input[type="text"]').each(function() { checkInput($(this)); });
	
	$('input[type="text"]').focus(function() {
		if ($(this).val()==$(this).attr('title')) {
			$(this).val('');
		}
		$(this).removeClass('default');
	});
	
	$('input[type="text"]').blur(function() { checkInput($(this)); });
	
	function checkInput(input) {
		if (input.val()==''||input.val()==input.attr('title')) {
			input.addClass('default');
			input.val(input.attr('title'));
		}
	}
	
}
