/*

HERE WILL BE STORED ALL THE JAVASCRIPT RLEATED FUNCTIONS

*/

var ajax = createAjax();
var winW = 630, winH = 460;

/*
AJAX RELATED FUNCTIONS
*/
function createAjax() {
	var ro;
	if(window.XMLHttpRequest){
		ro = new XMLHttpRequest();
	}else if (window.ActiveXObject) {
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return ro;
}

function callInProgress() {
	if (ajax.readyState == 4 || ajax.readyState == 0) {
		return false;
	}
	return true;
}
/*
END OF AJAX RELATED FUNCTIONS
*/

/*
DRAGGABLE CONTENT RELATED FUNCTIONS
*/
function addMouseDrag(d) {
	draggableDiv = document.getElementById(d);
	document.onmousemove = dragDiv;
	var IE = document.all?true:false;
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
		tempX = ev.pageX
		tempY = ev.pageY
	}
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}
	initialPosition.mouseX = tempX;
	initialPosition.mouseY = tempY;
	initialPosition.divX = findPosX(d);
	initialPosition.divY = findPosY(d);
}
function removeMouseDrag(d) {
	draggableDiv = document.getElementById(d);
	draggableDiv.onmousemove = function(){};
}

function dragDiv(ev) {
	//alert(ev.screenX);
	//alert(ev.screenY);

	var IE = document.all?true:false;
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
		tempX = ev.pageX
		tempY = ev.pageY
	}
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}
	window.status=""+tempX+"-"+tempY;
	draggableDiv.style.left = initialPosition.divX + (tempX - initialPosition.mouseX);
	draggableDiv.style.top = initialPosition.divY + (tempY - initialPosition.mouseY);
}

function setupDraggable() {
	document.onmousemove = mouseMove;
	document.onmouseup   = mouseUp;
}


var dragObject  = null;
var mouseOffset = null;

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);

	if(dragObject){
		dragObject.style.position = 'absolute';
		dragObject.style.top      = (mousePos.y - mouseOffset.y) + "px";
		dragObject.style.left     = (mousePos.x - mouseOffset.x) + "px";

		return false;
	}
}
function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}
function mouseUp(){
	dragObject = null;
}

function makeDraggable(item){
	if(!item) return;
	item.onmousedown = function(ev){
		dragObject  = this;
		mouseOffset = getMouseOffset(this, ev);
		return false;
	}
}
function makeCustomDraggable(item1, item2){
	if(!item1) return;
	//alert(item1);
	//alert(item2);
	item1.onmousedown = function(ev){
		dragObject  = item2;
		mouseOffset = getMouseOffset(this, ev);
		return false;
	}
	//alert("OK");
}
/*
END OF DRAGGABLE CONTENT RELATED FUNCTIONS
*/



function getWindowSize() {


	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}

}

function showProfile(id) {
	if (!callInProgress()){
		ajax.open("GET", "ajax_getprofile.php?model="+id);
		ajax.onreadystatechange = getProfileResponse;
		ajax.send(null);
	} else {
		setTimeout("showProfile('"+id+"')", 100);
	}
}

function getProfileResponse() {
	if (ajax.readyState == 4) {
		var x = document.getElementById('_bottomInformation_');
		x.innerHTML = ajax.responseText;
	}
}

function showPictures(id) {
	if (!callInProgress()){
		ajax.open("GET", "ajax_getpictures.php?model="+id);
		ajax.onreadystatechange = getPicturesResponse;
		ajax.send(null);
	} else {
		setTimeout("showPictures('"+id+"')", 100);
	}
}

function getPicturesResponse() {
	//alert('dat');
	if (ajax.readyState == 4) {
		var x = document.getElementById('_bottomInformation_');
		x.innerHTML = ajax.responseText;
		//alert(ajax.responseText);
	}
}

function showBigPicture(name) {
    //alert(name);
	var x = document.getElementById("_bigPicture_");
	x.style.display = "block";
	var z = document.getElementById("_bigPictureContent_");
	z.innerHTML = '<img id="_innerBigPicture_" src="'+name+'.jpg">';
	var y = document.getElementById("_innerBigPicture_");
	//alert(y.width);
	getWindowSize();
	//x.style.width = y.width;
	//x.style.left = ((winW - y.width) / 2) + "px";
	//alert((winW - y.width) / 2);
}

function closeBigPicture() {
	var x = document.getElementById("_bigPicture_");
	x.style.display = "none";
}

function addToFavorites(user, model) {
	if (!callInProgress()) {
		ajax.open("GET", "ajax_setfavorite.php?member="+user+"&model="+model);
		ajax.onreadystatechange = getAddToFavoritesStatus;
		ajax.send(null);
	} else {
		setTimeout("addToFavorites('"+user+"', '"+model+"')", 100);
	}
}

function getAddToFavoritesStatus() {
	if (ajax.readyState == 4){
		if (ajax.responseText == 1) {
			alert("The model was added as a favorite");
		} else {
			alert("The model was already added as a favorite");
			//alert(ajax.responseText);
		}
	}
}

function getModelShows(model) {
	if (!callInProgress()) {
		ajax.open("GET", "ajax_getshows.php?model="+model);
		ajax.onreadystatechange = getModelShowsResponse;
		ajax.send(null);
	} else {
		setTimeout("getModelShows('"+model+"')", 100);
	}
}

function getModelShowsResponse() {
	if (ajax.readyState == 4) {
		var x = document.getElementById('_bottomInformation_');
		x.innerHTML = ajax.responseText;
	}
}

function showMovie(movie, id) {
	if (!callInProgress()) {
		ajax.open("GET", "ajax_getflashmovie.php?movie="+movie+"&id="+id);
		ajax.onreadystatechange = getShowMovieResponse;
		ajax.send(null);
	} else {
		setTimeout("showMovie('"+movie+"', "+id+")", 100);
	}
}

function getShowMovieResponse() {
	if (ajax.readyState == 4) {
		var x = document.getElementById("_showSWF_");
		x.innerHTML = ajax.responseText;
	}
}

function showSchedule(day, modelID) {
	if (!callInProgress()) {
		ajax.open("GET", "ajax_getschedule.php?startDay="+day+"&modelID="+modelID);
		ajax.onreadystatechange = getScheduleResponse;
		ajax.send(null);
	} else {
		setTimeout("showSchedule('"+day+"')", 100);
	}
}

function getScheduleResponse() {
	if (ajax.readyState == 4) {
		var x = document.getElementById('_bottomInformation_');
		x.innerHTML = ajax.responseText;
	}
}

function payMovie(movieID) {
	if (!callInProgress()) {
		ajax.open("GET", "ajax_paymovie.php?movieID="+movieID);
		ajax.onreadystatechange = getPayMovieResponse;
		ajax.send(null);
	} else {
		setTimeout("payMovie("+movieID+")", 100);
	}
}

function getPayMovieResponse() {
	if (ajax.readyState == 4) {
		response = ajax.responseText;
		if (response == 1) {
			// all ok the movie was paid
			alert("The movie has been paid. You can now view it.");
		} else if (response == 2) {
			alert("You do not have enough money in your account.");
		} else if (response == 3){
			alert("There was an error. Please retry later.");
			//alert(response);
		} else {alert ("You have to be logged in to access recorded shows");}
	}
}
