/* -----------------------------------------------------
    HARVEST MODAL LINKS
 -----------------------------------------------------*/
werk_modal_links = new Array();ktk_modal_links = new Array();ktk_overview_modal_links = new Array();function check_modal_links() {
    var body_id = document.body.id;
    var as,i,isRef,le_link, add_link_id, le_link_id;    as=document.getElementsByTagName('a');    var u=0;
    var u_2=0;
    var u_3=0;
    // ALLE MODAL LINKS ACTIVEREN    for(i=0;i<as.length;i++) {        isRef=as[i].rel;
        // SHOW WERK        if(isRef.indexOf('show_werk')==0) {
            le_link = as[i];
            le_link.his_href = as[i].href;
            werk_modal_links[u]=le_link;            werk_modal_links[u].onclick=function() {
                show_loader();
                show_modal(this.his_href, 'modal_artist_werk');
                return false;             };            u++;        // SHOW KUNST TE KOOP -> rel="show_ktk";        } else if(isRef.indexOf('show_ktk')==0) {
            le_link = as[i];
            le_link.his_href = as[i].href;
            ktk_modal_links[u_2]=le_link;            ktk_modal_links[u_2].onclick=function() {
                show_loader();
                show_modal(this.his_href, 'modal_artist_ktk');
                return false;             };            u_2++;        } else if(isRef.indexOf('ktk_overview')==0) {
            le_link = as[i];
            le_link.his_href = as[i].href;
            ktk_overview_modal_links[u_3]=le_link;            ktk_overview_modal_links[u_3].onclick=function() {
                toggle_opacity(this);
                show_modal(this.his_href, 'modal_ktk_overview');
                return false;             };            u_3++;        }    }
    }
/* -----------------------------------------------------
    LOAD MODAL CONTENT Functions
 -----------------------------------------------------*/
var modal_to_show = '';
var modal_pic_height=0;
function show_modal(the_link, modal_soort) {
    var his_relevant_info = return_string_relevant_info(the_link);
    var werk_id = his_relevant_info[0];
    var werk_index = his_relevant_info[1];
    var werk_total = his_relevant_info[2];
    if(modal_soort=='modal_artist_werk') {
        var load_file="_inc/load_ajax.php?part=artist_werk&id="+werk_id+"&index="+werk_index+"&total="+werk_total;
    } else if(modal_soort=='modal_artist_ktk') {
        var load_file="_inc/load_ajax.php?part=artist_ktk&id="+werk_id+"&index="+werk_index+"&total="+werk_total;
    } else if(modal_soort=='modal_ktk_overview') {
        var sort_key = his_relevant_info[3];
        var load_file="_inc/load_ajax.php?part=ktk_overview&id="+werk_id+"&index="+werk_index+"&total="+werk_total+"&sort="+sort_key;
    }
    modal_to_show = modal_soort;
    sendRequest(load_file, handleRequest);
}

function return_string_relevant_info(the_link) {
    var full_href_string = the_link;
    var string_length = full_href_string.length;
    var bracket_index = full_href_string.indexOf("#");
    var relevant_info = full_href_string.substring((bracket_index+1), string_length);
    var relevant_info_array = relevant_info.split('-');
    return relevant_info_array;
}
var cur_loader;
function show_loader() {
    if(document.getElementById('loader_in_modal')) {
        cur_loader = document.getElementById('loader_in_modal');
    } else if(document.getElementById('loader_in_page')) {
        cur_loader = document.getElementById('loader_in_page');
    }
    if((cur_loader!='') && (cur_loader!=null)) {
        cur_loader.style.display = "block";
    }
}
function hide_loader() {
    if((cur_loader!='') && (cur_loader!=null)) {
        cur_loader.style.display = "none";
    } 
    if((linked_img!='') && (linked_img!=null)) {
        linked_img.style.opacity = 1;
    }
}
var linked_img;
function toggle_opacity(link) {
    linked_img = link.childNodes[0];
    linked_img.style.opacity = 0.5;
}
/* -----------------------------------------------------
    INSTANT EDIT CALLBACK FUNCTIONS
 -----------------------------------------------------*/
function handleRequest(req) {
    // var response_array = req.responseText.split("####IMG_H:");
    // var html_to_show = response_array[0];
    // modal_pic_height = response_array[1];
    var html_to_show = req.responseText;
	var container = document.getElementById('modal_holder');
	container.innerHTML = html_to_show;
	hide_loader();
    launch_modal(modal_to_show);
    trigger_prev_next_links();
}
function trigger_prev_next_links() {
    // previous
    if(document.getElementById('modal_prev')) {
        var prev_link = document.getElementById('modal_prev');
        prev_link.onclick = function() {
            show_loader();
            show_modal(this.href, modal_to_show);
            return false;         }
    }
    // next
    if(document.getElementById('modal_next')) {
        var next_link = document.getElementById('modal_next');
        next_link.onclick = function() {
            show_loader();
            show_modal(this.href, modal_to_show);
            return false;         }
    }
}
/* -----------------------------------------------------
    XMLHTTP Functions
    As found on Quirksmode. You hero.
 -----------------------------------------------------*/

function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
		//	alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

function XMLHttpFactories() {
	return [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];
}

function createXMLHTTPObject() {
	var xmlhttp = false;
	var factories = XMLHttpFactories();
	for (var i=0;i<factories.length;i++) {
		try {
			xmlhttp = factories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

/* -----------------------------------------------------
    SET OVERLAY STUFF
 -----------------------------------------------------*/

function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	return yScroll;
}

function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function setDivHeight(id) {    var theDiv = document.getElementById(id);    theDiv.style.height = (arrayPageSize[1] + 'px');
    theDiv.style.display = 'block';}
var ktk_sorteer_select;

function launch_modal(modal_to_display) {
    var arrayPageSize=getPageSize();
    var arrayPageScroll= getPageScroll();
    //  QUICK EDIT ZICHTBAAR ZETTEN
    var targetDiv = document.getElementById(modal_to_display);
    targetDiv.style.display = "block";

    var quickEditBoxH = targetDiv.offsetHeight;
    var quickEditBoxW = targetDiv.offsetWidth;
    
    // DELETE SHIZZLE
    var delete_link = document.getElementById('close_modal');
    delete_link.onclick=function() {
        close_cur_modal();
    }
    var delete_overlay_link = document.getElementById('overlay');
    delete_overlay_link.onclick=function() {
        close_cur_modal();
    }
    // center quickedit and make sure that the top and left values are not negative
    var quickEditTop = arrayPageScroll + ((arrayPageSize[3] - quickEditBoxH) / 2);
    var quickEditLeft = ((arrayPageSize[2] - quickEditBoxW) / 2);
		
    targetDiv.style.top = (quickEditTop < 0) ? "0px" : quickEditTop + "px";
    targetDiv.style.left = (quickEditLeft < 0) ? "0px" : quickEditLeft + "px";
    // check of de select van kunstenaars er is
    if(document.getElementById('sort')) {
        ktk_sorteer_select = document.getElementById('sort');
        ktk_sorteer_select.style.visibility = "hidden";
    }
    setDivHeight('overlay');
}

function close_cur_modal() {
    if (document.getElementById) {
        // this is the way the standards work
        var style1 = document.getElementById('overlay').style;
        style1.display = "none";
        var targetDiv = document.getElementById(modal_to_show).style;
        targetDiv.display = "none";
        // EMPTY HTML IN THE MODAL HOLDER
        var cur_container = document.getElementById('modal_holder');
        cur_container.innerHTML = '';
        // SORTEERDER WEER AAN ZETTEN ALS DIE ER IS
        if(document.getElementById('sort')) {
            ktk_sorteer_select.style.visibility = "visible";
        }
    }
}