var siteRoot = 'http://www.cheapassgamer.com';
//var siteRoot = 'http://localhost/cag';

function hidePanel()
{
	$('#cag_panel').hide();
}

function showPanel()
{
	$('#cag_panel').show();
	$('#cag_panel').css({"z-index":"8000"});
	
	loadContent('overview');
}

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString() + "; path=/");
}


function toggle_collapse_specific(objid, state)
{
	if (!is_regexp)
	{
		return false;
	}

	obj = fetch_object('collapseobj_' + objid);
	img = fetch_object('collapseimg_' + objid);
	cel = fetch_object('collapsecel_' + objid);

	if (!obj)
	{
		// nothing to collapse!
		if (img)
		{
			// hide the clicky image if there is one
			img.style.display = 'none';
		}
		return false;
	}

	if (obj.style.display == 'none' && state==1)
	{
		toggle_collapse(objid);
	}
	
	if (obj.style.display != 'none' && state==0)
	{
		toggle_collapse(objid);
	}
	
	return false;
}

function loadContent( contentType )
{
	var url = siteRoot + '/index_ajax.php';
	var params = 'do=overview&action=' + contentType;
		
	My_AJAX_Object = new vB_AJAX_Handler(true);
	My_AJAX_Object.onreadystatechange(vB_AJAX_Listener_Panel);
	
	My_AJAX_Object.send(url, params);
	
	setStatus('loading');
}

function vB_AJAX_Listener_Panel()
{
	if (My_AJAX_Object.handler.readyState == 4 && My_AJAX_Object.handler.status == 200 && My_AJAX_Object.handler.responseText)
	{
		var divid = 'live_content';
				
		document.getElementById(divid).innerHTML = My_AJAX_Object.handler.responseText;
	}
}


var vbimgcodeWidthMax = 711;
var vbimgcodeWidthSizeTo = 711;

function fix_images() {
if (document.images.length) {
var docImg = document.images;
   for ( var i = 0 ; i < docImg.length ; i++)
      if (docImg[i].width > vbimgcodeWidthMax && docImg[i].className == 'cag_img') {
         docImg[i].width = vbimgcodeWidthSizeTo;
      }
   }
}


Function.prototype.sleep = function (millisecond_delay) {
	if(window.sleep_delay != undefined) clearTimeout(window.sleep_delay);
	var function_object = this;
	window.sleep_delay = setTimeout(function_object, millisecond_delay);
};

function setStatus( status )
{
	// status groups
	if (status == 'loading')
	{
		$('#live_content').html('<div align="center" style="padding: 100px 0px 100px 0px"><img src="' + imgdir_misc + '/13x13progress.gif" border="0" alt="Loading" /></div>');
		return;
	}
}

function setUserbar(messageid, rawtext)
{
	if (userIsGuest == 1)
	{
		return;
	}

    if (!window.originalStatus)
    {
		window.originalStatus = document.getElementById('cag_userbar').innerHTML;
    }
	
	if (rawtext==1)
	{
		 document.getElementById('cag_userbar_content').innerHTML = messageid;
		 return true;
	}

	switch(messageid)
	{
	default:
	  	if (window.originalStatus)
		{
			document.getElementById('cag_userbar').innerHTML = originalStatus;
		}
	}

	return true;
}


function vote(dealid, score, paneltype)
{
	if (userIsGuest == 1)
	{
		registerAlert("To vote on deals you need to be a registered CAG member.");
		return;
	}
	
	$('#votePanel_' + dealid).html('<img src="'+ imgdir_misc +'/13x13progress.gif" border="0" alt="" />');

	var url = siteRoot + '/forums/deals_ajax.php';
	var div = 'votePanel_' + dealid;
	var pars = 'do=vote&dealid=' + dealid + '&score=' + score + '&paneltype=' + paneltype;
	
	$.ajax({
	   type: "POST",
	   url: url,
	   data: pars,
	   success: function(data, textStatus){
		   $('#' + div).html(data);
	   }
	 })
}

function registerAlert(alertLabel)
{
	if (userIsGuest == 0)
	{
		return;
	}
	
	if (alertLabel)
	{
		alertLabel += '<br /><br />';
	}
	$('#cag_panel').after('<div id="registerDialog" title="Become a CAG Member - It\'s Free!"><div>' + alertLabel + '</div><div>Membership is free, and comes with the following benefits:<br />- game collections and wishlists<br />- price alerts<br />- access to our deals and trading community<br />- participate in community discussions<br />- create your own blog<br />- fewer forum advertisements<br /></div><div class="padded" style="height: 40px; text-align: center"><a href="' + siteRoot + '/forums/register.php"><img src="' + imgdir_misc + '/registertoday.gif" border="0" alt="Register" title="Register" /></a></div></div>');

    $("#registerDialog").dialog({
	bgiframe: true,
	resizable: false,
	modal: true,
	overlay: {
		backgroundColor: '#000',
		opacity: 0.5
	},
	close: function() {
		$(this).dialog('destroy');
		$('#cag_panel').remove();
	}
	});

}
