/* -----------------------------------------------------
    HARVEST MODAL LINKS
 -----------------------------------------------------*/
mix_links = new Array();
var modal_to_show = 'showMix';

function init_mixes() {
// MEERDERE 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;
    // ALLE MODAL LINKS ACTIVEREN
    for(i=0;i<as.length;i++) {
        isRef=as[i].rel;
        // SHOW WERK
        if(isRef.indexOf('mix_focus')==0) {
            le_link = as[i];
            le_link.his_href = as[i].href;
            mix_links[u]=le_link;
            mix_links[u].onclick=function() {
                link_insert_loader(this);
                show_modal(this.his_href, 'showMix');
                return false; 
            };
            u++;
        // SHOW KUNST TE KOOP -> rel="show_ktk";
        } else if(isRef.indexOf('edit_agent')==0) {
            le_link = as[i];
            le_link.his_href = as[i].href;
            edit_agent_links[u_2]=le_link;
            edit_agent_links[u_2].onclick=function() {
                link_insert_loader(this);
                show_modal(this.his_href, 'modal_agent_edit');
                return false; 
            };
            u_2++;
        }
    }
	// CHECK OF ER EEN MIX FOCUS HEEFT
	if(document.getElementById('instant_focus')) {
		var show_instant_focus = document.getElementById('instant_focus');
	    launch_modal(modal_to_show);
	}    
}
/* -----------------------------------------------------
    LOAD MODAL CONTENT Functions
 -----------------------------------------------------*/
var modal_pic_height=0;

function show_modal(the_link, modal_soort) {
    var his_relevant_info = return_string_relevant_info(the_link);
	insert_modal_html();
    // LOAD CORRECT FILE
    if(modal_soort=='showMix') {
	    var info_id = his_relevant_info[1];
        var load_file="_inc/load_ajax.inc.php?part=mix_focus&id="+info_id;
    }
    modal_to_show = modal_soort;
    sendRequest(load_file, handleRequest);
}

function insert_modal_html() {
    // INSERT HTML AT TOP OF PAGE
	var body_tag = document.getElementsByTagName("body").item(0);
    // -- MODAL
    var page_modal = document.createElement("div");
	page_modal.setAttribute('id','modal_holder');
	body_tag.insertBefore(page_modal, body_tag.firstChild);
	// -- Overlay BOXES
	var mix_covers_hold_div = document.createElement("div");
	mix_covers_hold_div.setAttribute('id','mix_covers_hold');
	body_tag.insertBefore(mix_covers_hold_div, page_modal.nextSibling);
	var covers_html='';
	var num_covers = mix_links.length;
	for(var i=0; i<num_covers;i++) {
		covers_html = covers_html+'<div class="mixCoverBlok"></div>';
	}
	mix_covers_hold_div.innerHTML=covers_html;
}

function return_string_relevant_info(the_link) {
    var full_href_string = the_link.toString();
    var relevant_info_array = full_href_string.split('--');
    return relevant_info_array;
}

var active_loading_link;
var active_loading_link_html;
var mix_loader_html = ' <img src="gifs/ajax_loader.gif" alt="Loading..." border="0" />';

function link_insert_loader(the_link) {
    active_loading_link = the_link;
    active_loading_link_html = the_link.innerHTML;
    the_link.innerHTML = active_loading_link_html + mix_loader_html;
}
function hide_inserted_loader() {
    if((active_loading_link!='') && (active_loading_link!=null)) {
        active_loading_link.innerHTML = active_loading_link_html;
    } 
}


/* -----------------------------------------------------
    INSTANT EDIT CALLBACK FUNCTIONS
 -----------------------------------------------------*/
function handleRequest(req) {
    var html_to_show = req.responseText;
	var container = document.getElementById('modal_holder');
	container.innerHTML = html_to_show;
	hide_inserted_loader();
    launch_modal(modal_to_show);
}

/* -----------------------------------------------------
    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 mix_covers_hold 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 launch_modal(modal_to_display) {
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    //  QUICK EDIT ZICHTBAAR ZETTEN
    var mix_focus_div = document.getElementById(modal_to_display);
    mix_focus_div.style.display = "block";
    var mix_focus_div_h = mix_focus_div.offsetHeight;
    // center quickedit and make sure that the top and left values are not negative
    var mix_focus_div_top = arrayPageScroll + ((arrayPageSize[3] - mix_focus_div_h) / 2);
    mix_focus_div.style.top = (mix_focus_div_top < 0) ? "0px" : mix_focus_div_top + "px";
    
    //  COVERS OP ZICHTBAAR
    var mix_covers_hold = document.getElementById('mix_covers_hold');
    mix_covers_hold.style.display = "block";
    
    // DELETE SHIZZLE
    var delete_link = document.getElementById('close_modal');
    delete_link.onclick=function() {
        close_cur_modal();
        return false;
    }
}

function close_cur_modal() {
    if (document.getElementById) {
    	// REMOVE THE DYNAMIC DIVS
		if (document.getElementById('mix_covers_hold')) {     
          var mix_covers_hold = document.getElementById('mix_covers_hold');
          document.body.removeChild(mix_covers_hold);
		}
		if (document.getElementById('modal_holder')) {     
          var modal_holder = document.getElementById('modal_holder');
          document.body.removeChild(modal_holder);
		}
		if (document.getElementById(modal_to_show)) {     
          var modal_to_show_to_del = document.getElementById(modal_to_show);
          document.body.removeChild(modal_to_show_to_del);
		}
    }
}