var hstnm = location.host

//if (parent.frames.length > 0)
//	top.location.href = location.href;

if (get_cookie('s_height') != screen.height && get_cookie('s_width') != screen.width)
{
	set_cookie('s_height', screen.height, 10, '/', hstnm);
	set_cookie('s_width', screen.width, 10, '/', hstnm);
}

var messages = Array();
var users = Array();
var users_side = Array(); //remove me
var timer;
var flash_timer;
var current_user = null;
var last_message = null;
var priority_action = false;

function toggle_is_online(div)
{
	var area = document.getElementById("opt_" + div);
	if (area.style.display == "block")
	{
		area.style.display = "none";
		document.getElementById("arrow_closed_" + div).style.display = "block";
		document.getElementById("arrow_opened_" + div).style.display = "none";
	}
	else
	{
		area.style.display = "block";
		document.getElementById("arrow_closed_" + div).style.display = "none";
		document.getElementById("arrow_opened_" + div).style.display = "block";
	}
}
function get_cookie(cookieName)
{
	var cookie_array = document.cookie.split("; ");
	var pair;
	var n;
	var val;

	for (var i=0; i < cookie_array.length; i++)
	{
		pair = cookie_array[i].split("=");
		n = pair[0];
		val = pair[1];

		if (n == cookieName)
			return val;
	}
	return "";
}

function set_cookie(name, value, expire, path, domain, secure)
{
	var cookie_string = name + "=" + value;

	if (expire)
	{
		var exp_date = new Date();
		var ms_from_now = expire * 24 * 60 * 60 * 1000;
		exp_date.setTime(exp_date.getTime() + ms_from_now);
		var exp_string = exp_date.toGMTString();
		cookie_string += "; expires=" + exp_string;
	}
	if (path)   cookie_string += "; path=" + path;
	if (domain) cookie_string += "; domain=" + domain;
	if (secure) cookie_string += "; true";

	document.cookie = cookie_string;
}

function message(username, body, posted_on, photo_id)
{
	this.username 		= username;
	this.body			= body;
	this.posted_on		= posted_on;
	this.photo_id		= photo_id;
}

function user(username, new_messages)
{
	this.username		= username;
	this.new_messages	= new_messages;
}

function init_values()
{
	last_message = 0;	
	messages.length = 0;
	refresh_im();
}

function flashing(on)
{
	var obj;
	for (i=0; i < users.length; i++)
	{
		if (users[i].new_messages > 0)
		{
			obj = document.getElementById(users[i].username);
			if (on)
			{
				obj.style.color = "#77a6d4";
				obj.style.backgroundColor = "#fff";
			}	
			else
			{
				obj.style.color = "#fff";
				obj.style.backgroundColor = "#77a6d4";		
			}			
		}
	}
	flash_timer = setTimeout("flashing(" + (on ? false : true) + ")", 500);
}

function im()
{
	var extra = current_user ? "&u=" + current_user + '&m=' + last_message : '';
	send_request("im.php?k=" + Math.floor(Math.random() * 1000000) + extra);
	if (priority_action == false)
	{
		kill_timer();
		timer = setTimeout(im, 5000);	
	}
}

function refresh_im()
{
	var playpen = document.getElementById("main_area_playpen");
	var p = "";

	if (current_user)
		document.getElementById("message_playpen").style.display = "block";	

	//cleanup
	while (messages.length > 10)
	{
		messages.shift();
	}
		 
	for (i=(messages.length-1); i >= 0; i--)
	{
		p += '<span style="font-size: 4px;"><br /></span>';
		p += display_message(messages[i].username, messages[i].photo_id, messages[i].body, messages[i].posted_on, (current_user == messages[i].username ? true : false));
	}
	playpen.innerHTML = p;
}

function refresh_users()
{
	var playpen = document.getElementById("side_area_playpen");
	var p = ''
	for (i=0; i < users.length; i++)
	{
		p += '<span style="font-size: 4px;"><br /></span>';
		p += '<div class="sub_fill" id="' + users[i].username + '" style="font-size: 12px; font-weight: bold; cursor: pointer; padding: 4px 8px 4px 8px;" onClick="current_user=\'' + users[i].username + '\'; init_values(); im();">';
		p += users[i].username;
		p += '</div>';
	}
	playpen.innerHTML = p;
}

function display_message(username, photo_id, body, date, left_side)
{
	var p = '';
	if (left_side)
	{
		p += '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr>';
		p += '<td width="110" valign="top"><a href="?s=u&u=' + username + '"><img src="photo.php?s=a&i=' + photo_id + '" width="100" height="115" alt="member photo" style="border: 1px solid #000;"></a></td>';
		p += '<td valign="top" class="minor_fill" style="font-size: 12px;"><span style="font-weight: bold; font-size: 10px;">'+ date + '</span><span style="font-size: 4px;"><br /></span>' + decodeURI(body) + '</td>';
		p += '</tr></table>';
	}
	else
	{
		p += '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr>';
		p += '<td valign="top" class="minor_fill" style="font-size: 12px;"><span style="font-weight: bold; font-size: 10px;">'+ date + '</span><span style="font-size: 4px;"><br /></span>' + decodeURI(body) + '</td>';
		p += '<td width="110" valign="top" align="right"><a href="?s=u&u=' + username + '"><img src="photo.php?s=a&i=' + photo_id + '" width="100" height="115" alt="member photo" style="border: 1px solid #000;"></a></td>';
		p += '</tr></table>';		
	}
	return p;
}

function send_message(form)
{
	priority_action = true;
	var bod = document.getElementById("chat_body");
	send_request("im.php?u=" + current_user + "&b=" + encodeURI(bod.value));
	bod.value = "";
	bod.select();
}


function kill_timer()
{
	clearTimeout(timer);
  	timer = null;
}


//request functions
function send_request(url)
{
	try
	{
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponse;
		http.send(null);
	}
	catch(e)
	{
		send_request(url);
	}
}

function handleHttpResponse()
{
	if (http.readyState == 4)
	{
		eval(http.responseText);
		refresh_users();
		kill_timer();
		timer = setTimeout(im, 5000);			
	}
}

function getHTTPObject()
{
	var xmlhttp;
  	/*@cc_on
	@if (@_jscript_version >= 5)
	try 
	{
    	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
    	try 
    	{
        	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      	} 
      	catch (E) 
      	{
        	xmlhttp = false;
      	}
    }
  	@else
  	xmlhttp = false;
  	@end @*/
  	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
  	{
    	try 
    	{
      		xmlhttp = new XMLHttpRequest();
      		xmlhttp.overrideMimeType("text/xml");
    	} 
    	catch (e) 
    	{
      		xmlhttp = false;
    	}
  	}
 	return xmlhttp;	
}

function toggle_div(item_name)
{
	var obj = document.getElementById(item_name);
	if (obj.style.display == 'none')
	{
		obj.style.display = 'block';
	}
	else
	{
		obj.style.display = 'none';
	}
}

function open_win(URL)
{
	day = new Date();
	id = day.getTime();
	window.open(URL, id, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600,left=10,top=10');
}

var http = getHTTPObject();

var win;


function checkForMC(/*Object*/ select) {
 if (select.options[select.selectedIndex].value === "MC") {
  document.getElementById('sergehideXS').style.display = 'none';
 } else { // ovo sam dodao ako user izabere drugu karticu da opet prikaze taj div
  document.getElementById('sergehideXS').style.display = 'block';
 }
}