var myprod = new Object;
var BP_timeout;

if (!perpull)
	var perpull;
perpull = perpull || 5;

$(document).ready(function() {
	$("a#PS_moveleft").children("img").fadeTo("fast", 0.5);
	$("a#PS_moveright").css({cursor:'pointer'});
	$("div#myprod_slider").children("div.item").each(function(){$(this).hoverIntent(BP_showBub,BP_hideBub);});
	$("div#popUpBubble_left,div#popUpBubble_right").hoverIntent({sensitivity:1,interval:10,over:function(){clearTimeout(BP_timeout)},timeout:100,out:BP_hideBub});
});

//Bubble Popup (BP_)
function BP_showBub() {
	clearTimeout(BP_timeout);
	BP_closeBub();

	var offLeft = $("div#myprod_slider").offset().left || 0; //Distance from left for entire product slider
	var offTop  = $("div#myprod_slider").offset().top || 0;  //Distance from top for entire product slider
	var divLeft = parseInt($(this).css("left")) || 0;        //Distance from left for item within product slider
	var divTop  = parseInt($(this).css("top")) || 0;         //Distance from top for item within product slider (usually 0px)
	var side    = divLeft < 175 ? "left" : "right";          //Determine left/right arrow bubble to use
	var indent  = divLeft < 175 ? 28     : 28 - 310;         //Distance to indent the bubble

	var left    = offLeft + divLeft + indent;
	var top     = offTop  + divTop  - 120;
	var item    = $(this).children("a").attr("href").match(/item=([0-9]+)/)[1];

	if (!getDiv("BP_" + item)) {
		BP_dxGetData(side, left, top, item);
	} else {
		BP_openBub(side, left, top, item);
	}
}

function BP_openBub(side, left, top, item) {
	clearTimeout(BP_timeout); //Redundant
	var html = $("#BP_"+item).html();
	if (html)
		$("div#popUpBubble_"+side).css({display:"block",left:left,top:top}).children("div.productPop").html(html).corner("5px");
}

function BP_hideBub() { BP_timeout = setTimeout("BP_closeBub()", 500); }

function BP_closeBub() { $("div#popUpBubble_left,div#popUpBubble_right").css({display:"none"}); }

function BP_dxGetData(side, left, top, item) {
	$.ajax({
		type: "POST",
		url: BP_requrl,
		cache: false,
		data: "item=" + item,
		success: function(msg) { BP_createBox(item, msg); },
		error: function(XMLHttpRequest, textStatus, errorThrown) { BP_loadError(item); },
		complete: function() { BP_openBub(side, left, top, item); },
		timeout: 7000,
		dataType: "text"
	});
}

function BP_loadError(item) { }

function BP_createBox(item, msg) {
	var div = document.createElement("div");
		div.id = "BP_" + item;
		div.style.display = "none";
	document.getElementsByTagName('body')[0].appendChild(div);
	$("#" + div.id).html(msg);
}

//Product Slider (PS_)
function PS_move(dir) {
	//Create the object if the object doesn't exist
	if (!myprod.declared) {
		myprod.declared = 1; //Establish constants as declared
		myprod.width  = 57;  //Style width (px) of each element
		myprod.curpos = 1;   //Current item in the 1st position
		myprod.scroll = 0;   //Used to prevent multi-scroll
		myprod.reqrun = 0;   //AJAX request already in progress when 1
		myprod.speed  = 250; //length of animation (less is faster)
		myprod.idx    = myprodinf[0] || 0; //ID of the current profile/tribe
		myprod.max    = myprodinf[1] || 0; //Max number of products currently available for display
		myprod.total  = myprodinf[2] || 0; //Total products for artist (used to prevent more requests)
		myprod.tribe  = myprodinf[3] || 0; //If the product slider is used on the tribe (group) page
		myprod.high   = myprod.max - 7;    //Stop position for the slider

		myprod.pos = new Array();
		for (var i = 0; i <= 9; i++) {
			myprod.pos[i] = (i - 1) * myprod.width; //Establish 10 positions with 8 center being visible
		}
	}

	//Select 9 nodes for moving
	var node = new Array();
	for (var i = 1; i <= 9; i++) {
		node[i] = "myprod_" + (myprod.curpos + (i - (dir == "right" ? 1 : 2)));
	}

	switch (dir)
	{
		case "right":
			if (myprod.curpos < myprod.high && myprod.scroll == 0) {
				myprod.scroll = 1;
				setTimeout('myprod.scroll = 0;', myprod.speed);
				for (var i = 1; i <= 9; i++) {
					if (getDiv(node[i])) $("#" + node[i]).animate({left: myprod.pos[i - 1]}, myprod.speed);
				}
				myprod.curpos++;
			}

			//Prefetch a few more elements
			if (myprod.curpos >= myprod.high && myprod.max < myprod.total && myprod.idx > 0) {
				if (!myprod.reqrun)
					PS_dxGetItems();
			}

			if (myprod.curpos > 1) PS_btnchange("left", "enable");
			if (myprod.curpos == myprod.total - 7) PS_btnchange("right", "disable");
			break;

		case "left":
			if (myprod.curpos > 1 && myprod.scroll == 0) {
				myprod.scroll = 1;
				setTimeout('myprod.scroll = 0;', myprod.speed);
				for (var i = 1; i <= 9; i++) {
					if (getDiv(node[i])) $("#" + node[i]).animate({left: myprod.pos[i]}, myprod.speed);
				}
				myprod.curpos--;
			}

			if (myprod.curpos < myprod.high) PS_btnchange("right", "enable");
			if (myprod.curpos == 1) PS_btnchange("left", "disable");
			break;
	}
}

function PS_dxGetItems() {
	myprod.reqrun = 1;
	var tmp = myprod.total - myprod.max;
	var limit = (tmp < perpull) ? tmp : perpull
	$.ajax({
		type: "POST",
		url: PS_requrl,
		cache: false,
		data: "idx=" + myprod.idx + "&count=" + myprod.max + "&limit=" + limit + "&tribe=" + myprod.tribe,
		success: function(msg) { PS_loadData(msg); myprod.reqrun = 0; },
		error: function(XMLHttpRequest, textStatus, errorThrown) { PS_loadError(); myprod.reqrun = 0; },
		timeout: 7000,
		dataType: "text"
	});
}

function PS_loadData(msg) {
	var idxs = msg.split(",");
	for (var i = 0; i < idxs.length; i++) {
		if (idxs[i] != "" && !isNaN(idxs[i])) {
			myprod.max++;
			myprod.high++;
			PS_createBox(idxs[i], myprod.max);
		}
	}
}

function PS_loadError() {
	myprod.total = myprod.max;
	PS_btnchange("right", "disable");
}

function PS_createBox(idx, num) {
	var body = "<a href='" + base_url + "/i.x/shop/itemdetails/-/?item=" + idx + "&_m=a'><img src='" + base_url + "/store/item_file/" + idx + "/image_tiny.jpg' alt='' width='50' height='65' border='0' align='left' /></a>";
	var nodename = "myprod_slider";
	var div = document.createElement("div");
		div.id = "myprod_" + num;
		div.className  = "item";
		div.style.left = myprod.pos[9] + "px";
	getDiv(nodename).appendChild(div);
	getDiv(div.id).innerHTML = body;
	$("#" + div.id).hoverIntent(BP_showBub, BP_hideBub);
}

function PS_btnchange(obj, act) {
	var opac = (act == "enable") ? 1.0 : 0.5;
	var curs = (act == "enable") ? "pointer" : "default";
	$("#PS_move" + obj).children("img").fadeTo("fast", opac).css({cursor:curs});
}