// JavaScript Document


/*
var modalWindow = {
	parent:"body",
	windowId:null,
	content:null,
	width:null,
	height:null,
	close:function()
	{
		$(".modal-window").remove();
		$(".modal-overlay").remove();
	},
	open:function()
	{
		var modal = "";
		modal += "<div class=\"modal-overlay\"></div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
		modal += this.content;
		modal += "</div>";	

		$(this.parent).append(modal);

		$(".modal-window").append("<a class=\"close-window\"></a>");
		$(".close-window").click(function(){modalWindow.close();});
		$(".modal-overlay").click(function(){modalWindow.close();});
	}
};

var openMyModal = function(source)  
{  
    modalWindow.windowId = "myModal";  
    modalWindow.width = 480;  
    modalWindow.height = 405;  
    modalWindow.content = "<iframe width='480' height='405' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'></iframe>";  
    modalWindow.open();  
};  */

function remove_query_arg(query,arg_to_remove) {
		stripped_query = query;
		arg_start = query.indexOf(arg_to_remove+"=");	// the char position of the 'order'
		if (arg_start>=0) {
			arg_len = query.substr(arg_start).indexOf("&");	// how many chars after 'order' do we get the first &, if any
			if (arg_len >= 0) {
				stripped_query = (query.substr(0,arg_start))
					+ query.substr(arg_start+arg_len);
			} else { // no further arguments, so just curtail the query string to be up to the 'order' argument
				stripped_query = query.substr(0,arg_start);
			}
		}
		return stripped_query;
}

function cat_show_all() {
	this_page = document.location + "";
	this_query = "";
	if (this_page.indexOf("?")>0) {
		this_query = this_page.substr(this_page.indexOf("?")+1);
		this_query = remove_query_arg(remove_query_arg(this_query,"w2"),"w");
		
		this_page = this_page.substr(0,this_page.indexOf("?")); // split it off before the ?
	}
	document.location = this_page + "?"+this_query;
}

function sortby(fld) {
	this_page = document.location + "";
	this_query = "";
	if (this_page.indexOf("?")>0) {
		this_query = this_page.substr(this_page.indexOf("?")+1);
		
		// now curtail the page only
		this_page = this_page.substr(0,this_page.indexOf("?"));
		this_query_o = this_query.indexOf("order=");	// the char position of the 'order'
		if (this_query_o>=0) {
			this_query_oend = this_query.substr(this_query_o).indexOf("&");	// how many chars after 'order' do we get the first &, if any
			if (this_query_oend >= 0) {
				this_query = this_query.substr(0,this_query_o)
					+ this_query.substr(this_query_o+this_query_oend);
			} else { // no further arguments, so just curtail the query string to be up to the 'order' argument
				this_query = this_query.substr(0,this_query_o);
			}
		}
	}
	next_url = this_page + "?"+this_query+"&order="+fld;
	next_url = next_url.replace("&&","&");
	document.location = next_url;
}

function cat_open(mediacode) {
	if (Ext.get("loading")!=null) {
			Ext.get("loading").setVisible(true);
	}
	Ext.Ajax.request({
		url: "catalogue_one.php?plain&mediacode="+mediacode,
		success: function(response, opts) {
			htmlToAdd = '<div id="cat_display">'+response.responseText+'</div>';
			Ext.get("cat_display_box").insertHtml("afterBegin",htmlToAdd);
			Ext.get("cat_display_box").setVisible(true,true);
			//alert(response.responseText.substr(0,100));
			if (Ext.get("loading")!=null) {
					Ext.get("loading").setVisible(false);
			}
			},
		failure: function(response, opts) {
			alert("Sorry, we couldn't load that set to display.\nPlease contact the webmaster if this persists.");
			if (Ext.get("loading")!=null) {
					Ext.get("loading").setVisible(false);
			}
			}
	});
	
}

function hide_display() {
	Ext.get("cat_display_box").setVisible(false);
	if (Ext.get("cat_display")!=null) {
		Ext.get("cat_display").setVisible(false,true);
		//setTimeout("remove_display_content()",3000);
	}
}

function remove_display_content() {
	if (!Ext.get("cat_display_box").isVisible()) {
	Ext.get("cat_display").remove(); //insertHtml("afterBegin","");
		//alert("hidden again");
	}
}

function cat_more(cat_section) {
	document.location = "catalogue_list.php?from=0&w=cat_section&w2="+cat_section;
}