var g_url = null;
var g_divProductAdsId = null;
var g_userId = null;
var g_productCatType = null;
var g_productCatId = null;
var g_cartItemsId = null;
var g_videoDetailsPageFormat = null;
var g_ChannelPageFormat = null;
var PROD_CAT_TYPE_CHANNEL = 'channel';
var PROD_CAT_TYPE_FOLDER = 'folder';
var PROD_CAT_TYPE_VIDEOSET = 'videoset';
var ITEM_TYPE_VIDEOSET = 'videoSet';
var ITEM_TYPE_PRODUCT = 'product';
var CALLER_ID_PRODUCT = 'productAds';
var CALLER_ID_PRODUCT_ = 'Product';
var CALLER_ID_CART = 'cart';
var SERVER_ERRROR_RESPONSE_BEGIN = 'error';
var ASYNC_REQUEST_METHOD = 'post';
var PARAM_SEPARATOR = '|';
function makeRequestString(url, callerId, data)
{
	return url + '?callerId=' + callerId + '&msg=' + Object.toJSON(data); 
}
function MessageData(productCatType, productCatId, gotoPgNum)
{
	this.productCatType = productCatType;
	this.productCatId = productCatId;
	this.gotoPgNum = gotoPgNum;
	this.toJSON = function()
	{
		return Object.toJSON({productCatType: this.productCatType, productCatId: this.productCatId, gotoPgNum: this.gotoPgNum});
	}
}
function CartMessageData(itemType, itemId)
{
	this.itemType = itemType;
	this.itemId = itemId;
	this.toJSON = function()
	{
		return Object.toJSON({itemType: this.itemType, itemId: this.itemId});
	}
}
function isStringBlank(text)
{
	return (!text || !text.length);
}
function isServerError(responseText)
{
	return (responseText.indexOf(SERVER_ERRROR_RESPONSE_BEGIN) != -1);
}
function handleServerError(responseText)
{
	//alert(responseText.split(PARAM_SEPARATOR)[1]);
}
function checkAndHandleServerError(responseText)
{
	if (isServerError(responseText))
	{
		handleServerError(responseText);
		return true;
	}
	return false;
}
function insertHTML(divControlID, html)
{
	if (!isStringBlank(html))
	{
		$(divControlID).innerHTML = html;
	}
}
function onServerError(response)
{
	document.write(response.responseText);
}
function sendRequest(caller, msgData, onSuccessHandler)
{
    var request = new Ajax.Request(
			makeRequestString(g_url, caller, msgData),
      { asynchronous: true, method: ASYNC_REQUEST_METHOD, onSuccess: onSuccessHandler, onFailure: onServerError });
}
function loadAsyncPart(divProductAdsId, url, userId, productCatId, productCatType, cartItemsId, 
	videoDetailsPageFormat, channelPageFormat)
{
	g_divProductAdsId = divProductAdsId;
	g_url = url;
	g_userId = userId;
	g_productCatId = productCatId;
	g_productCatType = productCatType;
	g_cartItemsId = cartItemsId;
	g_videoDetailsPageFormat = videoDetailsPageFormat;
	g_ChannelPageFormat = channelPageFormat;
}
function loadProductAds(productCatType, productCatId, gotoPgNum)
{
	g_productCatId = productCatId;
	g_productCatType = productCatType;
	sendRequest(CALLER_ID_PRODUCT, new MessageData(productCatType, productCatId, gotoPgNum), onServerResponse);
}
function loadFolderProductAds(folderId)
{
	loadProductAds(PROD_CAT_TYPE_FOLDER, folderId, 1);
}
function loadVideoSetProductAds(videoSetId)
{
	loadProductAds(PROD_CAT_TYPE_VIDEOSET, videoSetId, 1);
}
function loadChannelProductAds(channelId)
{
	loadProductAds(PROD_CAT_TYPE_CHANNEL, channelId, 1);
}
function onServerResponse(response)
{
		var responseText = response.responseText;
		if (!checkAndHandleServerError(responseText))
		{
			insertHTML(g_divProductAdsId, response.responseText);
		}
}
function onServerCartResponse(response)
{		
		var responseText = response.responseText;
		if (!checkAndHandleServerError(responseText))
		{
		    if ($(g_cartItemsId).innerHTML != responseText + " item(s)")
		    {
			    $(g_cartItemsId).innerHTML = responseText + " item(s)";				
			}
			else
			{
			    $(g_cartItemsId).innerHTML = 'already in cart';
			    window.setTimeout('$(g_cartItemsId).innerHTML = "' + responseText + ' item(s)";', 1000);
			}
			window.setTimeout('parent.location = updateShoppingCartUrl;', 1000);
			 //'Public/updateShoppingCart.aspx';
		}
}
function omnitureAddTrack(response)
{
    OmnitureAdd2Cart(response.responseText, ($(g_cartItemsId).innerHTML.indexOf("1") == 0));
}

function add2cart(videoSetId) // from flash
{
	add2cartInternal(ITEM_TYPE_VIDEOSET, videoSetId)
}

function add2cartFromControl(productId) // from productAdContainer
{        
	add2cartInternal(ITEM_TYPE_PRODUCT, productId);
}
function add2cartInternal(itemType, itemId)
{   
	sendRequest(CALLER_ID_CART, new CartMessageData(itemType, itemId), onServerCartResponse);
	sendRequest(CALLER_ID_PRODUCT_, new CartMessageData(itemType, itemId), omnitureAddTrack);
}

function gotoPage(gotoPgNum)
{
	loadProductAds(g_productCatType, g_productCatId, gotoPgNum);
}
function gotoVideoDetails(videoSetId)
{
	window.location = g_videoDetailsPageFormat + videoSetId.toString();
}
function redirect2channel(channelId, userId, startPoint)
{
	var location = g_ChannelPageFormat;
	location = location.replace('__channelId__', channelId.toString());
	location = location.replace('__startpoint__', startPoint.toString());
	location = location.replace('__tokenId__', userId.toString());
	window.location = location;
}


