//
// VSM project
//
// intranet scripts
//

// is this a mobile?
function isMobileDevice() {
	if ( isAndroid || isiPhone() ) {
		return true;
	}
	return false;
}

// is this an android?
function isAndroid() {
	if((navigator.userAgent.match(/Android/i))) {
		return true;
	}
	return false;
}

// is this an iphone?
function isiPhone() {
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		return true;
	}
	return false;
}

// open a new window
function open_win( url ) {
	window.open( url );
}

// open a new window
function openWindow( url ) {
	window.open( url );
}


// open a window
function openMobileSimulator( url, width, height ) {
	width  = width  + 17;
	height = height + 17;
	window.open( url, '_blank', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=no, scrollbars=yes, width=' + width + ', height=' + height );
}


// download a file
function downloadFile( url ) {
	window.open( url, '_blank', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=no, width=300, height=100' );
	window.status = 'loading...';
}


// open a fixed size window
function openBareWindow( url, width, height ) {
	window.open( url, '_blank', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=no, width=' + width + ', height=' + height );
	window.status = 'loading...';
}


// swap to the highlighted version of the label
function highlight(e) {
	if ( e.tagName == "IMG" ) {
		var url, name, regexp;
		//window.event.srcElement.hspace = 2;
		//window.event.srcElement.vspace = 2;
		url = e.src;
		regexp = /.gif$/i;
		name = url.replace(regexp,"_h.gif");
		e.src = name;
	}
}


// swap to the normal version of the label
function normal(e) {
	if ( e.tagName == "IMG" ) {
		var url, name, regexp;
		//window.event.srcElement.hspace = 0;
		//window.event.srcElement.vspace = 0;
		url = e.src;
		regexp = /_h.gif/i;
		name = url.replace(regexp,".gif");
		e.src = name;
	}
}


// reload content via AJAX
function reloadContent(element,url)
{
	var xmlhttp = initAjax();
	if (xmlhttp != null) {
	    
        // init
    	sendAjax(xmlhttp,url,element);
        
	} else {
	
	    // failed
		alert('failed');
	}
}


// async AJAX callback
function ajaxCallback(xmlhttp,parent) {
    	
	if (xmlhttp.readyState==4) {
	
	    // is the response ok?
        if (xmlhttp.status==200) {
        
            // update the element
            var element = document.getElementById(parent);
            if (element != null) {
                element.innerHTML = xmlhttp.responseText;
            } else {
                alert('cannot find ' + parent );
            }
            
        } else {
        
            // oops
            alert('request status = ' + xmlhttp.status);
        }
	}
}

var requestn = 0;
function sendAjax(xmlhttp,url,parent)
{
    if (xmlhttp != null) {
        
        // init
		requestn = requestn + 1;
        url = url + '?requestnum=' + requestn;

        // init
        xmlhttp.onreadystatechange= function() { ajaxCallback(xmlhttp,parent); };
		xmlhttp.open("GET",url);
        
        // send the request
        xmlhttp.send(null);
    }
}


function initAjax()
{
    // create the object
    var xmlhttp = null;

    if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
	    try {
		    xmlhttp=new XMLHttpRequest();
       }
       catch (e) {
		    // failed
		    return null;
       }
    } else if (window.ActiveXObject) { //IE
	    try { 
		    xmlhttp=new ActiveXObject("MSXML2.XMLHTTP");
	    }
	    catch (e) {
		    try {
			    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
		    }
		    catch (e) {
			    // failed
			    return null;
		    }
	    }
	}
	
	return xmlhttp;
}

// save mouse coords
var mouse_x = 0;
var mouse_y = 0;
function save_coords(event)
{
	mouse_x = event.clientX;
	mouse_y = event.clientY;
	// alert("X coords: " + mouse_x + ", Y coords: " + mouse_y + ' target: ' + event.target );
}

// clear_element
function clear_element(name)
{
	var element = document.getElementById(name);
	if (element != null) {
		element.innerHTML = '';
	}
}

// close menu
function closepopups()
{
	closemenu();
	closepic();
}

// close menu
function closemenu()
{
	clear_element('menu');
}

// open menu in 'menu' SPAN
function showmenu(name,ypos)
{
	var element = document.getElementById('menu');
	if (element != null) {
		element.style.left = mouse_x;
		element.style.top  = ypos;
	}
	var url = 'html/menu_' + name + '.htm';
	reloadContent('menu',url);
}

// show assets for our story builder
function showassets(name)
{
	var url = 'html/story_' + name + '.htm';
	reloadContent('assets',url);
}

// show page in 'main' SPAN
function showpage(name)
{
	closemenu();
	var url = 'html/page_' + name + '.htm';
	reloadContent('main',url);
}

// close pic
function closepic()
{
	clear_element('showpic');
}

// show pic
function showpic(event,url)
{
	mouse_x = event.clientX;
	mouse_y = event.clientY;
	var element = document.getElementById('showpic');
	if (element != null) {
		element.style.left = mouse_x;
		element.style.top  = mouse_y + window.pageYOffset;
		element.innerHTML = '<TABLE bgcolor=#E67437><TR><TD><IMG border=1 style="border-color:#E67437;" src="' + url + '"></TD></TR>' +
		'<TR><TD align=center><P><FONT color=white>' + url + '</FONT></P></TD></TR></TABLE>';
	}
}

// start dragging
function dragStart(event)
{
	// event.dataTransfer.effectAllowed='move';
	event.dataTransfer.setData('Text', event.target.getAttribute('id'));
	// should set X and Y as offset in source object
	// alert( "event.clientX = " + event.clientX + " event.target.width = " + event.target.width );
	var drag_x = event.target.width  / 2;
	var drag_y = event.target.height / 2;
	event.dataTransfer.setDragImage(event.target,drag_x,drag_y);
	return true;
}

// drag over a new target
function dragOver(event) 
{
	// false means it is OK to drop here
	return false;
}

// drag completed
function dragEnd(event)
{
	// shall we drop the source inner HTML here?
	return true;
}

// drop on target
function dragDrop(event) 
{
	var source = event.dataTransfer.getData('Text');
	var target = event.target.getAttribute('id');
	// alert( "Object " + source + " was dropped on " + target );
	
	var source_element   = document.getElementById(source);
	var source_container = document.getElementById(source + '_container');
	var target_element   = document.getElementById(target + '_container');
	// alert( "Object " + source_element.src + " was dropped on " + target_element.src );
	if ((source_container != null) && (source.substr(0,5) == "frame")) {
		source_container.innerHTML = '<IMG border=0 id="' + source + '" height=138 src="images/film_frame128.png">';
	}
	if ((source_element != null) && (target_element != null)) {
		target_element.innerHTML = '<IMG border=0 id="' + target + '" height=138 draggable=true ondragstart="return dragStart(event)" ondragend="return dragEnd(event)" class=vsm_thumbnail src="' + source_element.src + '">';
	}
	// event.stopPropagation();
	return false; // return false so the event will not be propagated to the browser
}

// drop on target in the bin
function dragDropInBin(event)
{
	var source = event.dataTransfer.getData('Text');
	var target = event.target.getAttribute('id');
	// alert( "Object " + source + " was dropped in BIN (" + target + ")" );
	
	var source_container = document.getElementById(source + '_container');
	if ((source_container != null) && (source.substr(0,5) == "frame")) {
		source_container.innerHTML = '<IMG border=0 id="' + source + '" height=138 src="images/film_frame128.png">';
	}
	// event.stopPropagation();
	return false; // return false so the event will not be propagated to the browser
}


