﻿

//--------------------------------------------------  Tooltip Functions  -------------//

var ShowTooltip = function()
{
	var tip = "";
	var width = 0;
	if ($(this).attr("status") == "1")
	{
		tip = "Share this pledge";
		width = 85;
	}
	else if ($(this).attr("type") == "product")
	{
		tip = "Take a pledge and consider buying this product";
		width = 240;
	}
	else
	{
		tip = "Take a pledge";
		width = 70;
	}

	document.getElementById("tooltipHover").innerHTML = tip;


	var position = $(this).offset();
	var wpos = $('#ContentContainer').offset();
	var wmax = $('#ContentContainer').width();

	var left = position.left - 20;
	if (left + width >= wmax)
	{
		left = wmax - 5 - width;
	}

	document.getElementById("tooltipHover").style.width = width + 'px';
	$('#tooltipHover').css('top', (position.top - 25) + 'px');
	$('#tooltipHover').css('left', left + 'px');
}


var HideTooltip = function()
{
	$('#tooltipHover').css('top', '-1000px');
	$('#tooltipHover').css('left', '-1000px');
}


$(document).ready( function() {
	$('.pledgeLinks').hover(ShowTooltip, HideTooltip);
});


//--------------------------------------------------  Product Functions  -------------//

function FilterProducts(id)
{
	var input = document.getElementById(id).getElementsByTagName("INPUT");

	var bid = "";
	var rblBrandFilter = document.forms[0]["rblBrandFilter"];
	for (var i = rblBrandFilter.length - 1; i >= 0; i--) 
	{
		if (rblBrandFilter[i].checked)
		{
			bid = rblBrandFilter[i].value;
			break;
		}
	}
	
	var ids = "";
	for (var i = 0; i < input.length;  i++)
	{
		if (input[i].type == "checkbox")
		{
			if (input[i].checked && input[i].getAttribute("brandID") == bid)
			{
				ids += "," + input[i].value;
			}
		}
	}
	ids = ids.substring(1);


	window.location = location.pathname + "?fid=" + ids + "&bid=" + bid;

	return false;
}

function CheckFilter(oSender)
{
	if (oSender.type == "radio")
	{
		var brandID = oSender.value;

		var div = document.getElementById("divProductFilters").getElementsByTagName("DIV");

		
		for (var i = div.length - 1; i >= 0; i--) 
		{
			if (ul = div[i].getElementsByTagName("UL")[0]) 
			{
				var input = ul.getElementsByTagName("INPUT");
				var hidden = 0;
				for (var j = input.length - 1; j >= 0; j--) 
				{
					//toggle the li
					if (input[j].getAttribute("brandID") == brandID)
					{
						input[j].parentNode.style.display = "block";
					}
					else
					{
						input[j].parentNode.style.display = "none";
						hidden++;
					}
				}
				
				//toggle the div
				input[0].parentNode.parentNode.parentNode.style.display = (hidden == input.length) 
					? "none"
					: "block"
				;
			}
		}
	}
}


//-------------------------------------------------- Rebates Functions  -------------//

function GetRebates(oSelect)
{
	try
	{
		window.location = location.pathname + "?state=" + oSelect.value;
	} catch (e) { throwException('SelectAnswer()', e.lineNumber, e, e.message); }
	return false;
}


//-------------------------------------------------- Misc Functions  -------------//


function SetFormTarget(target, loc) 
{
	document.forms[0].target = target;
	document.forms[0].action = loc;
}

function SwapButtonImage(oButton)
{
	if (!oButton.disabled)
	{
		var ext = GetFileExtention(oButton.src);
		var src = FileNameWithoutExtention(oButton.src);

		if (Contains(src, "_over"))
		{
			src = Replace(src, "_over", "");
		}
		else
		{
			src += "_over"
		}

		src = src + ext;
		oButton.src = src;
		oButton.style.cursor = "pointer";
	}
}


function loadCookies()
{
	var cr = [];
	if (document.cookie != '')
	{
		var ck = document.cookie.split('; ');
		for (var i = ck.length - 1; i >= 0; i--)
		{
			var cv = ck[i].split('=');
			cr[cv[0]] = cv[1];
		}
	}
	return cr;
}
var cookies = loadCookies();

function GetCookie(c_name)
{
	var c = cookies[c_name];
	if (typeof (c) != "undefined")
	{
		return c;
	}
	return null;
}

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());
}


//--------------------------------------------------  Profile Functions  -------------//

function show(argElementId) { document.getElementById(argElementId).style.display = 'block'; }
function hide(argElementId) { document.getElementById(argElementId).style.display = 'none'; }
function toggle(argElementId) { obj = document.getElementById(argElementId); if (obj.style.display == 'block') { hide(argElementId); } else { show(argElementId); } }

function hideAllEditButtons()
{
	hide("pLoginEdit");
}

function showAllEditButtons()
{
	show("pLoginEdit");
}

function formElementsToggle(argElementsArray, argType)
{
	// if the argType is Text, then the Text items are hidden and the Input are shown
	// if the the argType is the opposite, then the opposite happens
	if (argType == "Text")
	{
		for (i = 0; i < argElementsArray.length; i++)
		{
			hide(argElementsArray[i] + "Value");
			show(argElementsArray[i] + "Input");
		}
	} else
	{
		for (i = 0; i < argElementsArray.length; i++)
		{
			hide(argElementsArray[i] + "Input");
			show(argElementsArray[i] + "Value");
		}
	}
}

function turnOnForm(argElementId)
{
	hideAllEditButtons();

	switch (argElementId)
	{
		case "pLogin":
			hide("pLoginEdit");
			show("pLoginButtons");

			formElementsToggle(pLoginElements, "Text")
			break;
	}
}

function turnOffForm(argElementId, formElementsArray)
{
	document.getElementById(argElementId).style.backgroundColor = "transparent";

	switch (argElementId)
	{
		case "pLogin":
			hide("pLoginButtons");
			show("pLoginEdit");

			formElementsToggle(pLoginElements, "Input")
			break;
	}

	showAllEditButtons();
}


//--------------------------------------------------  Nav dropdown  -------------//

$(document).ready(function() {
	$('.userarea').hover(
		function() {
			if ($.browser.msie) {
				$('div.sub', this).css('display', 'block');
			}
			else {
				//$('div.sub', this).fadeIn
				$('div.sub', this).css('display', 'block');
				(
					'fast'
				);
			}
		},
		function() {
			$('div.sub', this).css('display', 'none');
		}
	);
	$('.nav li').hover(
		function() {
			if ($.browser.msie) {
				$('div.sub', this).css('display', 'block');
			}
			else {
				//$('div.sub', this).fadeIn
				$('div.sub', this).css('display', 'block');
				(
					'fast'
				);
			}
		},
		function() {
			$('div.sub', this).css('display', 'none');
		}
	);
});
