var n = null;
var bdebugger = null;
var is_scrolling = false;
var eBanner = null;

function URL(text)
{
	return encodeURIComponent(text);
}

function facebookConnectPopUp(app_id, redirect_uri)
{
	final_url = "https://graph.facebook.com/oauth/authorize?client_id=" + URL(app_id)
		+ "\x26redirect_uri=" + URL(redirect_uri) + "\x26type=user_agent\x26display=popup\x26scope=email";

	window.open(final_url, "_new", "width=550,height=350,left=300,top=150");
}

function change_background(site, category,mouse_type)
{
	if ( mouse_type == '1' )
	{
		document.getElementById(category).style.background='#FFFFFF';
	}
	else
	{
		if ( site == '001.0' )
		{
			document.getElementById(category).style.background='url(/area/001.0/images/nav_bg.jpg)';
		}
		else if ( site == '001.1' )
		{
			document.getElementById(category).style.background='url(/area/001.1/images/nav_bg.jpg)';
		}
		else if ( site == '003.0' )
		{
			document.getElementById(category).style.background='url(/area/003.0/images/nav_bg.jpg)';
		}
		else if ( site == '002.0' )
		{
			document.getElementById(category).style.background='url(/area/003.0/images/nav_bg.jpg)';
		}
		else if ( site == '004.0' )
		{
			document.getElementById(category).style.background='url(/area/004.0/images/nav_bg.jpg)';
		}
		else if ( site == '005.0' )
		{
			document.getElementById(category).style.background='url(/area/005.0/images/nav_bg.jpg)';
		}
		else if ( site == '006.0' )
		{
			document.getElementById(category).style.background='url(/area/006.0/images/nav_bg.jpg)';
		}
		else if ( site == '008.0' )
		{
			document.getElementById(category).style.background='url(/area/008.0/images/nav_bg.jpg)';
		}
		else
		{
			document.getElementById(category).style.background='url(/area/000.0/images/nav_bg.jpg)';
		}
	}
}

function get_position(obj)
{
	var xpos = 0;
	var ypos = 0;
	var xsiz = 0;
	var ysiz = 0;

	if (obj.offsetParent)
	{
		xsiz = obj.offsetWidth;
		ysiz = obj.offsetHeight;
		do { xpos += obj.offsetLeft; ypos += obj.offsetTop; } while (obj = obj.offsetParent);
	}

	return [xpos, ypos, xsiz, ysiz];
}

function slide_banner()
{

	var eBannerTop = document.getElementById("banner_top");
	eBanner = document.getElementById("banner_float");
	var eBody = document.getElementById("site_body");

	if (!eBannerTop || !eBanner || !eBody) return;

	bdebugger = document.getElementById("debugger");

	var top_position = get_position(eBannerTop);
	var ban_position = get_position(eBanner);

	var top_limit = top_position[1] - 10;
	var bot_limit = top_limit + top_position[3] - ban_position[3];

	var s = (window.pageYOffset != undefined ? window.pageYOffset : eBody.scrollTop);
	var sh = eBody.scrollHeight;
	var ch = eBody.clientHeight;

	if (s < top_limit)
	{
		n = 0;
	}
	else if (s > bot_limit)
	{
		n = bot_limit - top_limit;
	}
	else
	{
		n = s - top_limit;
	}

	if (bdebugger)
	{
		bdebugger.innerHTML = "BODY.scrollTop: " + s + "<br/>";
		bdebugger.innerHTML += "BODY.scrollHeight: " + sh + "<br/>";
		bdebugger.innerHTML += "BODY.clientHeight: " + ch + "<br/>";
		bdebugger.innerHTML += "Scroll Amt: " + n + "<br/>";
		bdebugger.innerHTML += "top_limit: " + top_limit + "<br/>";
		bdebugger.innerHTML += "bot_limit: " + bot_limit + "<br/>";
		bdebugger.innerHTML += "top_position: " + top_position[0] + ", " + top_position[1] + ", " + top_position[2] + ", " + top_position[3] + "<br/>";
		bdebugger.innerHTML += window.pageYOffset + "<br/>";
		bdebugger.innerHTML += (window.pageYOffset == undefined) + "<br/>";

	}

	if (!is_scrolling) { is_scrolling = true; window.setTimeout("update_scroll()", 20); }
}

function update_scroll()
{
	var s = parseInt(eBanner.style.top);

	var sn = s + (n > s ? Math.ceil((n - s) / 8) : Math.floor((n - s) / 8));

	eBanner.style.top = sn + "px";

	is_scrolling = (sn != n);

	if (is_scrolling) window.setTimeout("update_scroll()", 20);

}

function createCookie(name,value,days) {
        if (days)
	{
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
        }
        else
	{
		var expires = "";
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//  Facebook global components (to be deprecated)
var mv_movie;
var mv_rating;

//  Facebook global components (to be deprecated)
function popUpDiv(divName, val1, val2, val3)
{
	var sb = document.getElementById('site_body');
	var bd = document.getElementById('DIV_ALL');
	var pd = document.getElementById(divName);

	var logged_in = document.getElementById('LOGGED_IN').value;

	if (logged_in == 'N')
	{
		if(val3 == "profile_thumbs")
		{
			pd = document.getElementById('DIV_FB_LOGIN');
		}
		else
		{
			pd = document.getElementById('DIV_USER_LOGIN');		
		}
	}

	bd.style.display = 'block';
	//bd.style.top = sb.scrollTop;
	bd.style.width = sb.clientWidth;
	bd.style.height = document.body.scrollHeight;
	
	pd.style.display = 'block';
	pd.style.top = sb.scrollTop + 50;
	pd.style.zIndex = 30;
	
	if (divName == "DIV_MOVIE_RATING")
	{
		//window.alert(document.getElementById('LOGGED_IN').value);
		//window.alert(val1);
		deselectRating();
		mv_movie = val1;
		mv_rating = "";
		document.getElementById("mv_review").value = "";
		document.getElementById("flex_movies_div").style.visibility = 'hidden';
	}
	else if (divName == "DIV_BIZ_REVIEW")
	{
		var formBiz = document.getElementById('form_biz');
		formBiz.value = val1;
		var lsRegExp = /\+/g;
		document.getElementById('biz_review_company_name').innerHTML = unescape(val2).replace(lsRegExp, " ");
		fixedBiz=unescape(val2).replace(lsRegExp, " ");
		
		document.getElementById('biz_review_type').innerHTML = "You Like ";
		document.getElementById('review').value='';
		document.getElementById('review').focus();
	}
	
	try
	{
		document.getElementById('banner_float').style.display='none';
	}
	catch(err)
	{}

}

function closePopUp(divName)
{
	document.getElementById('DIV_ALL').style.display = 'none';
	document.getElementById(divName).style.display = 'none';
	if (divName == "DIV_MOVIE_RATING")
	{
		document.getElementById('flex_movies_div').style.visibility = 'visible';
	}
	else if(divName == "DIV_BIZ_REVIEW")
	{
		document.getElementById('biz_profile_like').style.display = 'block';
	
	}
}

function doRating(rating)
{

        for (star = 0;star <= 8; star = star + 1)
        {
                if (rating >= star)
                {	
			if (star >= rating && star == 0)
			{
                                document.getElementById('DIV_' + star).style.background = "url(/images/star_on_zero.png)";
				mv_rating = rating;
			}
                        else if (star % 2 == 1 && star != 0)
                        {
                                document.getElementById('DIV_' + star).style.background = "url(/images/star_on_left.png)"; 
                        }
                        else if (star != 0)
                        { 
                                document.getElementById('DIV_' + star).style.background = "url(/images/star_on_right.png)";
                        }
                }
                else
                {
			if (star == 0)
			{
                                document.getElementById('DIV_' + star).style.background = "url(/images/star_on_zero.png)"; 
			}
                        else if (star % 2 == 1)
                        {
                                document.getElementById('DIV_' + star).style.background = "url(/images/star_off_left.png)";
                        }
                        else
                        {
                                document.getElementById('DIV_'+ star).style.background = "url(/images/star_off_right.png)";
                        }
                }
        }
	document.getElementById('DIV_SELECTED_rating').innerHTML = rating/2 + ' Stars (click to choose)';

}

function selectRating(rating)
{
	//window.alert(rating/2);
	mv_rating = rating;
	//document.getElementById('rating_container').removeEventListener('mouseout',deselectRating,false);
	document.getElementById('rating_container').onmouseout=function(){};
	document.getElementById('DIV_SELECTED_rating').innerHTML = 'You chose: ' + rating/2 + ' Stars';
        
	for (star = 0;star <= 8; star = star + 1)
	{
		if (rating >= star)
		{
			if (star >= rating && star == 0)
			{
				document.getElementById('DIV_' + star).style.background = "url(/images/star_on_zero.png)";
				document.getElementById('DIV_' + star).onmouseover=function(){};
			}
			else if (star % 2 == 1 && star != 0)
			{
				document.getElementById('DIV_' + star).style.background = "url(/images/star_on_left.png)";
				document.getElementById('DIV_' + star).onmouseover=function(){};
			}
			else if (star != 0)
			{
				document.getElementById('DIV_' + star).style.background = "url(/images/star_on_right.png)";
				document.getElementById('DIV_' + star).onmouseover=function(){};
			}
		}
		else
		{
			if (star == 0)
			{
				document.getElementById('DIV_' + star).style.background = "url(/images/star_on_zero.png)";
				document.getElementById('DIV_' + star).onmouseover=function(){};
			}
			else if (star % 2 == 1)
			{
				document.getElementById('DIV_' + star).style.background = "url(/images/star_off_left.png)";
				document.getElementById('DIV_' + star).onmouseover=function(){};
			}
			else
			{
				document.getElementById('DIV_' + star).style.background = "url(/images/star_off_right.png)";
				document.getElementById('DIV_' + star).onmouseover=function(){};
			}
		}
	}
}

function deselectRating()
{
	for (star = 0; star <= 8; star = star + 1)
	{
		if (star == 0)
		{ 
			document.getElementById('DIV_' + star).style.background = "url(/images/star_off_zero.png)"; 
		}
		else if (star % 2 == 1)
		{
			document.getElementById('DIV_' + star).style.background = "url(/images/star_off_left.png)";
		}
		else
		{
			document.getElementById('DIV_' + star).style.background = "url(/images/star_off_right.png)";
		}
	}
}

function load_content(element_id, url)
{

	var req;
	var element = document.getElementById(element_id);

	try { req = new XMLHttpRequest(); } catch (e) { try { req = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { req = new ActiveXObject('Microsoft.XMLHTTP'); } }

	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				element.innerHTML = req.responseText;
			}
			else
			{
				element.innerHTML = 'HTTP Error ' + req.status.toString();
			}
			req = null;
		}
	}

	req.open("GET", url, true);
	req.send(null);

}

function user_bubble(action, item)
{
    var http_obj = bubbleXmlHttpObject();

    if (http_obj == null) { alert ("Browser does not support HTTP Request"); return; }

    var bubble_url = "/user/user_bubble_ajax.amp";
    bubble_url += "?action=" + URL(action);
    bubble_url += "&item=" + URL(item);
    bubble_url += "&sid=" + Math.random();

    http_obj.onreadystatechange = function() { bubbleStateChanged(http_obj, action, item); };
    http_obj.open("GET", bubble_url, true);
    http_obj.send(null);
}

function bubbleStateChanged(http_obj, action, item)
{
    if (http_obj.readyState == 4 || http_obj.readyState == "complete")
    {
        var counts = http_obj.responseXML.getElementsByTagName("data");

        for(var i = 0; i < counts.length; i++)
        {
            var user = counts[i].getElementsByTagName('user')[0].firstChild.data;
            var actionResult = counts[i].getElementsByTagName('actionResult')[0].firstChild.data;
        }

		// window.alert(user + ' ' + actionResult + ' ' + action + item);

    }

}

function bubbleXmlHttpObject()
{
    var xmlHttp = null;
    try
    {
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function user_points_update(action, item)
{
    var http_obj = bubbleXmlHttpObject();

    if (http_obj == null) { alert ("Browser does not support HTTP Request"); return; }

    var user_points_url = "/user/user_points_ajax.amp";
    user_points_url += "?action=" + URL(action);
    user_points_url += "&item=" + URL(item);
    user_points_url += "&sid=" + Math.random();

    http_obj.onreadystatechange = function() { user_points_updateStateChanged(http_obj, action, item); };
    http_obj.open("GET", user_points_url, true);
    http_obj.send(null);
}

function user_points_updateStateChanged(http_obj, action, item)
{
    if (http_obj.readyState == 4 || http_obj.readyState == "complete")
    {
        var counts = http_obj.responseXML.getElementsByTagName("data");

        for(var i = 0; i < counts.length; i++)
        {
            var action = counts[i].getElementsByTagName('action')[0].firstChild.data;
            var item = counts[i].getElementsByTagName('item')[0].firstChild.data;
        }

		document.getElementById('DIV_CURRENT_POINTS').innerHTML = item;

		// window.alert(user + ' ' + actionResult + ' ' + action + item);

    }

}


