var spinner = "<div class='spinner'><img src='"+assets+"images/spinner.gif'></div>";

$(function() {
	$(".advanced_form").validate();
	//$("select, :radio, :checkbox, :file").uniform();
	$("a.modal-link").click(function()
	{
		loadModal(780, "/layout/set/modal/"+$(this).attr("href"), false);
		return false;
	});
	$("a.json-link").click(function()
	{
		jsonResponse(780, "/layout/set/modal/"+$(this).attr("href"), true);
		return false;
	});

	
	if($.browser.msie && Number($.browser.version.substring(0,1)) < 7 && ($("#homepage").length == 1)){
		$.modal($("#ie_warning"), {"opacity": 90});
	}
	
	$("form.modal-form").submit(function(evt) {
		if (evt) evt.preventDefault();
		modalForm(780, this);
	})
	spinPreload = new Image()
	spinPreload.src = assets+"images/spinner.gif";
	$(".content-view-line .click-view-full").click(function(evt){
		if (evt) evt.preventDefault();
		window.open($(this).attr("rel"))
	});


	$("table.sortable").tablesorter();

})




function print_r(OObj, recurse, prependingSpace) {
	if(typeof OObj == 'object') {
		var treeDisplay = '';
		for(var key in OObj) {
			treeDisplay += prependingSpace+'['+key+'] => \''+OObj[key]+'\' ('+typeof OObj[key]+')\n';
			if(recurse && typeof OObj[key] == 'object') {
				treeDisplay += print_r(OObj[key], recurse, prependingSpace+'\t');
			}
		}
		return treeDisplay;
	} else {
		return 'not an object!';
	}
}

// submits form and returns result in a modal popup.

function jsonResponse(mWidth,mURL)
{
	if ($("#overlay").length == 0) {
		$("#page").after('<div id="overlay">&nbsp;</div><div id="modal"><div id="modal-content"><img src="' + assets + 'images/spinner.gif"></div></div>');
	}
	$("#overlay").css({width:$("body").width(),height:$(document).height()}).show();
	$("#modal").css({"top":($(document).scrollTop()+100),"left":($(window).width()/2)-(mWidth/2)});
	$("#modal").animate({"width":mWidth,"height":"50px"},250,function(){
		$.getJSON(mURL, function(data){
			if (data.status.message != undefined) {
				$("#modal-content").html(data.status.message);
			}else {
				$("#modal-content").html("There was an error processing this transaction.");
			}
			$("#overlay, #modal-content p.close a").click(modalClose);
			if (data.refresh != undefined) {
				if (data.refresh == true) {
					$("#overlay").oneTime("2s", "refreshPage", function() {
						window.location.reload();
					});
				}
			} else {
				$("#overlay").oneTime("2s", "closeModal", modalClose);
			}

		}, "json");
	});
}

function modalForm(mWidth,form)
{
	if ($("#overlay").length == 0) {
		$("#page").after('<div id="overlay">&nbsp;</div><div id="modal" class="ui-widget ui-widget-content ui-corner-all"><div id="modal-content" ><img src="' + assets + 'images/spinner.gif"></div></div>');
	}
	$("#overlay").css({width:$("body").width(),height:$(document).height()}).show();
	$("#modal").css({"top":($(document).scrollTop()+100),"left":($(window).width()/2)-(mWidth/2)});
	$("#modal").animate({"width":mWidth,"height":"50px"},250,function(){
		$("#modal-content").load("/layout/set/modal"+$(form).attr("action"), $(form).serializeArray(), function(){
			$("#modal").animate({"height":$("#modal-content").outerHeight(true)},500,function(){
				$("#overlay").css({height:$(document).height()});
			});

			$("#overlay, #modal-content p.close a").click(modalClose);
		});
	});
}


function loadModal(mWidth,mURL, autoClose)
{
	if (autoClose == undefined) {
		autoClose = true;
	}
	if ($("#overlay").length == 0) {
		$("#page").after('<div id="overlay">&nbsp;</div><div id="modal" class="ui-widget ui-widget-content ui-corner-all"><div id="modal-content"><img src="' + assets + 'images/spinner.gif"></div></div>');
	}
	$("#overlay").css({width:$("body").width(),height:$(document).height()}).show();
	$("#modal").css({"top":($(document).scrollTop()+100),"left":($(window).width()/2)-(mWidth/2)});
	$("#modal").animate({"width":mWidth,"height":"50px"},250,function(){
		$("#modal-content").load(mURL,function(){
			$("#modal").animate({"height":$("#modal-content").outerHeight(true)},500,function(){
				$("#overlay").css({height:$(document).height()});
			});

			$("#overlay, #modal-content p.close a").click(modalClose);
			if (autoClose == true) {
				$("#overlay").oneTime("3s", "closeModal", modalClose);
			}
			
		});
	});
}

function modalClose(evt){
	if (evt && evt.preventDefault != undefined) {
		evt.preventDefault()
	}
	$("#modal").animate({"height":"50px"},500,function(){
		$("#modal-content").html("&nbsp");
		$("#modal").animate({"width":"0px","height":"0px"},500,function(){
			$("#modal").remove();
			$("#overlay").remove();
		});
	});
	return false;
}