//watermark update
var watermarks = new Array();
function watermarkThis(obj)
{
	watermarkThis(obj,'');
}
function watermarkThis(obj, frmID) {
	var index = watermarks.length;
	watermarks.push({id:obj.id, text:obj.value, orgState: obj.className, frmID: frmID});
	if (frmID != '')
		document.getElementById(frmID).onsubmit = function(){return validateWatermark(index);};
	obj.onfocus = function(){ watermarkFocus(obj,index)};
	obj.onblur = function() {watermarkBlur(obj,index)};
}
function watermarkFocus(obj,index) {
	if (obj.value == watermarks[index].text) {
		obj.value = '';
		obj.className = "stateA stateB";
	}
	var frmID = watermarks[index].frmID;
	
}
function watermarkBlur(obj,index) {
	if (obj.value == '') {
		obj.value = watermarks[index].text;
		obj.className = watermarks[index].orgState;
	}
	var frmID = watermarks[index].frmID;
}

function validateWatermark(index)
{
	var watermark = watermarks[index];
	var val = document.getElementById(watermark.id).value;
	return val != '' && val != watermark.text;
}

//create list page sub cat nav
var isChild = false;

function loadCurCat() {
	document.getElementById("cat--999").href = "/?cmd=sb-list&id=-999&srt=3";
	if(catID == 0) //is search
	{
		document.getElementById("parentCat").innerHTML = "Search<div class=\"edge\"></div>";
		document.getElementById("childCat").innerHTML = catDesc;
		return;
	}
	subCatLink = document.getElementById("sCat-" + catID);
	subCatCont = document.getElementById("cat-" + catID + "-subList");		
	if (subCatLink) {
		subCatParent = subCatLink.parentNode;
		var parentCat = 1;
		parentCat = subCatParent.id.substring(4, subCatParent.id.length - 8).replace('-subList','');
		subCatCont = document.getElementById("cat-" + parentCat  + "-subList");	
		isChild = true;
		subCatLink.className = "navSelected";
		parentCatID  = document.getElementById("cat-" + parentCat);
		document.getElementById("parentCat").innerHTML = "<a href=\"?cmd=sb-list&id=" + parentCat + "\">" + parentCatID.innerHTML + "</a><div class=\"edge\"></div>";	
		document.getElementById("childCat").innerHTML = catDesc;
		loadParentSubCats(subCatCont,parentCat);
	} else {
		document.getElementById("parentCat").innerHTML = document.getElementById('cat-' + catID).innerHTML + "<div class=\"edge\"></div>";	
		if (subCatCont) {
			loadSubCats();	
		}	
	} 
}
function loadSubCats() {
	var subCatCont = document.getElementById("cat-" + catID + "-subList");	
	document.getElementById("subCatsNav").style.display = "";
	document.getElementById("subCatsNav").innerHTML = "	<div class=\"title\">IN THIS CATEGORY:</div>" + 
		"<a id=\"featuredCatItems\" href=\"?cmd=sb-list&id=" + catID + "\">All</a>" +
		subCatCont.innerHTML + "<div class=\"navBtm\"></div>";						
	document.getElementById("subCatsNav").style.display = "";
	document.getElementById("featuredCatItems").className = "navSelected";
}
function loadParentSubCats(elm, parentID) {
	
	document.getElementById("subCatsNav").style.display = "";
	document.getElementById("subCatsNav").innerHTML = "	<div class=\"title\">IN THIS CATEGORY:</div>" + 
		"<a id=\"featuredCatItems\" href=\"?cmd=sb-list&id=" + parentID + "\">All</a>" +
		elm.innerHTML + "<div class=\"navBtm\"></div>";
	var leftNav = document.getElementById("subCatsNav");
	
	for (var i = 0; i < leftNav.childNodes.length; i++)
	{
		if (leftNav.childNodes[i].id == "sCat-" + catID)
		{
			leftNav.childNodes[i].className = "highlight1 slct";
			break;
		}
	}
//	document.getElementById("sCat-" + catID).className = "highlight1";
	document.getElementById("subCatsNav").style.display = "";
	document.getElementById("featuredCatItems").className = "navSelected";
}
//select box
//add indexOf to array object
if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
        fromIndex = 0;
    } else if (fromIndex < 0) {
        fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
        if (this[i] === obj)
            return i;
    }
    return -1;
  };
}

var drpRgstr = new Array();
function showDrop(a,e) {
	curSelectBoxID = a.parentNode.id;
	curSelectBox = a;
	options = a.nextSibling;
	while (options.nodeName !="DIV") {
		options = options.nextSibling
	}
	masterWidth = a.clientWidth;
	if (options.style.display == "" || options.style.display == "none") {
		curMode = "block";
	} else {
		curMode = "";
	}
	options.style.display = curMode;
	if (drpRgstr.indexOf(a) == -1) {
		drpRgstr.push(options);
	}
	for (i=0; i<drpRgstr.length; i++) {
		if(drpRgstr[i] != options) {
			drpRgstr[i].style.display="none";
		}
	}
	cancelBubbleEvent(e);
}
function slctOpt(b) {
	alert(b.getAttribute("optionValue"));
	document.getElementById(curSelectBoxID).value = b.innerHTML;
	options.style.display = "";
	curSelectBox.innerHTML = b.innerHTML;
}
function closeDropDown() {
	document.getElementById("contVarDrp").style.display = "none";
}
//popUp system
function launchStPop(i,m,mc,b,cusFn) {	
	var stPop = document.getElementById("stPop");
	var cusFn = cusFn || 0;
	var ico = document.getElementById("stPopIco");
	var title = document.getElementById("stPopTitle");
	title.innerHTML = m;
	title.className = mc;
	msg = document.getElementById("stPopBody");
	msg.innerHTML = b;
	msg.style.maxHeight = (jQuery(window).height() / 2) + "px"; //added if height too tall
	stPop.style.left = "50%";
	ico.style.backgroundImage = "url('http://www.swagbucks.com/content/skin-02/images/st" + i + ".jpg')";
	if(cusFn > 0 && typeof customStPopFunc != 'undefined') {
		customStPopFunc(cusFn)
	}
	var stPopHeight = stPop.offsetHeight;
	stPop.style.marginTop = "-" + (stPopHeight / 2) + "px";
}
function closeMe(ele) {
	document.getElementById(ele).style.left = "-999px";
}
function showMe(ele) {
	document.getElementById(ele).style.left = "-999px";
}
function cancelBubbleEvents(e) {
    var e = e || window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}

function showLoginBox(invalid) {
	if (typeof loginPopCont != 'undefined') {
		if (invalid) {
			loginPopCont.style.height = "auto";		
			var loginPopContHeight = jQuery('#loginPopInner').height();
			loginPopCont.style.top = ((loginPopContHeight + jQuery(window).height()) / 2) + 'px';		
		} else {
			showLoginPop();
		}
	}
}
function showLoginPop(disableAnimation) {
    loginPopCont.style.height = "auto"; 
    var loginPopContHeight = jQuery('#loginPopInner').height();
    var delay = 500;
    if ( disableAnimation )
        delay = 0;
    jQuery(loginPopCont).animate({top: ((loginPopContHeight + jQuery(window).height()) / 2) + 'px'},delay,function(){jQuery("#emailAddress").focus()});
}
function closeLoginPop(disableAnimation) {
    if (!isDiaologLogin) {
        var delay = 500;
        if ( disableAnimation )
            delay = 0;
        jQuery(loginPopCont).animate({top: '0'},delay);
	}
	else if (closeLoginAction == 1) {
		hideDiaologLogin();
	}
	else {
		location.href = closeLoginAction;
	}
}

function scroll2TopNonIe() {
    var msie = ((navigator.appVersion.indexOf("MSIE")!= -1)&&!window.opera)? true : false;
    if (! msie) {
        scroll(0,0);
    }
}
function showDiaologLogin(title, cancelUrl) {
    document.getElementById("fadeCover").style.display = "block";
	boxTitle = document.getElementById("loginTitle");
	boxTitle.innerHTML = title;
    popupObj = loginPopCont;
    popupObj.style.height = "auto";
	var loginPopContHeight = jQuery('#loginPopInner').height();
	popupObj.style.top = ((loginPopContHeight + jQuery(window).height()) / 2) + 'px';
    popupObj.style.display = "block";
	if (cancelUrl == 0) {
		document.getElementById("btnCancelLogin1").style.display = "none";
		document.getElementById("btnCancelLogin2").style.display = "none";
	}
	closeLoginAction = cancelUrl;
	isDiaologLogin = true;
	//initFB();	
}
function hideDiaologLogin() {
    popupLogin = loginPopCont;
    popupLogin.style.display = "none";
    popupObj.style.top = "0";
    document.getElementById("fadeCover").style.display = "none";
}
var isSwagstore30 = true;
var isDiaologLogin = false;

//end login pop
 // reset pop
function reloadPopStyling() {
	if(!$('#stPopIco').hasClass('ico')) {
		$('#stPopIco').attr('class','').addClass('ico');
	}
	$('.msg:first').css('marginLeft', '')
	$('.stPop').css({
		width:'',
		marginLeft: ''
		
	})
}

function shareThisOnFacebook(){
	if(typeof(fbUrlToShare) == 'undefined'){
		if(typeof(twitterUrlToShare) != 'undefined'){
			fbUrlToShare = twitterUrlToShare;
		}else{
			fbUrlToShare = 'http://www.swagbucks.com/?cmp=132';
		}
	}
	var fbShare;
	fbShare = window.open('http://www.facebook.com/sharer.php?s=100&p[title]=' + encodeURIComponent(twitterTextToShare) + '&p[url]=' + encodeURIComponent(twitterUrlToShare) + '&p[images][0]=http://content.swagbucks.com/profiles/profile-lg/default-new.jpg&p[summary]=Earn rewards and free stuff by searching and shopping online, answering surveys, and more at Swagbucks.com, a customer loyalty rewards program. Be rewarded today.','fbShare','status=0,toolbar=0,scrollbars=0,width=620,height=420');
}
function shareThisOnTwitter(){
	if(typeof(twitterUrlToShare) == 'undefined'){
		if(typeof(fbUrlToShare) != 'undefined'){
			twitterUrlToShare = fbUrlToShare;
		}else{
			twitterUrlToShare = 'http://www.swagbucks.com/?cmp=132';
		}
	}
	if(typeof(twitterTextToShare) == 'undefined') twitterTextToShare = '';
	var twitterShare;
	twitterShare = window.open('http://twitter.com/intent/tweet?text=' + encodeURIComponent(twitterTextToShare) + '&url=' + encodeURIComponent(twitterUrlToShare),'twitterShare','status=0,toolbar=0,scrollbars=0,width=550,height=420');
}
function getTwitterUrlShare(winType, memberID, urlHost){
	return 'http://' + (urlHost ? urlHost : 'www.swagbucks.com') + '/p/referral?cmp=132&cxid=2-' + winType + '&fb_ref=' + memberID + '&to=' + encodeURIComponent('/p/register');
}
function getFbUrlToShare(twitterUrlShare, deno, msgID, fbAddition){
	return twitterUrlShare.replace('cxid=2-', 'cxid=1-') + encodeURIComponent('?fb=true|deno~' + deno + '|msgID~' + msgID + (fbAddition ? fbAddition : ''));
}
