/*
$(function(){
	$('.model').children('a').click(function(){	
		var show = !$(this).parent().hasClass('roll');
		$('.model').removeClass('roll');
		$('.model').removeClass('current');
		if (show) {
			$(this).parent().addClass('roll');
		}
		$.scrollTo($('#work h1'));
		return false;
	});		
});
*/

var ie = !!window.ActiveXObject;
var ie8 = ie && document.documentMode == 8;

function getOptionSelected(select){
	var _options = select.getElementsByTagName("option");
	for(var i=0; i<_options.length; i++){
		if(_options[i].selected) return _options[i];
	}
	return false;
}

function notebookListView(xml) {
	var notebooks = xml.getElementsByTagName("notebook");
	var notebookNode = document.getElementById("notebooks");
	var oldNotebookOptions = notebookNode.getElementsByTagName("option");
	while(oldNotebookOptions.length) notebookNode.removeChild(oldNotebookOptions[0]);
	for(var i=0; i<notebooks.length; i++){
		var option = document.createElement("option");
		var optionText = document.createTextNode(notebooks[i].firstChild.data);
		option.appendChild(optionText);
		option.setAttribute("value", notebooks[i].getAttribute("url"));
		notebookNode.appendChild(option);
	}
}

function loadXMLDoc(method, url, handler, data) {
	processReqChange.handler = handler;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open(method,url,true);
        if(method == "post"){
        	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length", data.length);
			req.setRequestHeader("Connection", "close"); 
        }
        req.send(data);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open(method,url,true);
            if (method == "post") {
        		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				req.setRequestHeader("Content-length", data.length);
				req.setRequestHeader("Connection", "close"); 
        	}
            req.send(data);
        }
    }
}

function processReqChange() {
    if(req.readyState == 4){
        if(req.status == 200){
			eval(processReqChange.handler);
		}
    }
}		

function selectProducer(select){
	var producer = getOptionSelected(select).value;
	var AJAX_URL = "/ajax/notebooks?producer="+producer;
	loadXMLDoc("get",AJAX_URL,"notebookListView(req.responseXML.documentElement)",null);
	// notebooks.style.visibility = 'visible';
	document.getElementById("notebooks").style.visibility = 'visible';
}		

function selectNotebook(select){
	var url = getOptionSelected(select).value;
	var windowLocation = String(window.location);
	if(url == 0) {
		return false;
	} else {
		window.location = url;
	}
}
