// balcony shirts javascript 

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function getElementsByClassName(classname)
{
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = document.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

addLoadEvent(runBookmark);
function runBookmark(){
	if(!document.getElementById("bookmark")){return false};
	var bookmark = document.getElementById("bookmark");
	var title = bookmark.getAttribute("title");
	bookmark.onclick = function(){
		return addBookmark(title, bookmark);
	}
}


addLoadEvent(openPopup);
function openPopup(){
	
	var popupLinks = getElementsByClassName("openPopup");
	
	for ( var i=0; i < popupLinks.length; i++ ){
		var trigger = popupLinks[i].getAttribute("className") || popupLinks[i].getAttribute("class");
		//alert(trigger);
		switch(trigger)
		{
			case "openPopup prodPopup":
			popupLinks[i].onclick = function(){
				return popup( this, "Product", "600", "744", "yes" );
			}
		break;
		case "balconyProd openPopup prodPopup":
			popupLinks[i].onclick = function(){
				return popup( this, "Product", "600", "744", "yes" );
			}
		break;
		case "standardProd openPopup prodPopup":
			popupLinks[i].onclick = function(){
				return popup( this, "Product", "600", "744", "yes" );
			}
		break;
		case "openPopup emailPopup":
			popupLinks[i].onclick = function(){
				return popup( this, "EmailFriend", "440", "450", "" );
			}
		break;
		case "openPopup forgotten":
			popupLinks[i].onclick = function(){
				return popup( this, "Forgotten", "440", "250", "" );
			}
		break;
		case "openPopup security":
			popupLinks[i].onclick = function(){
				return popup( this, "SecurityCode", "440", "400", "" );
			}
		break;
		case "openPopup termsPopup":
			popupLinks[i].onclick = function(){
				return popup( this, "Terms", "601", "450", "yes" );
			}
		break;
		case "openPopup sizeGuideLink":
			popupLinks[i].onclick = function(){
				return popup( this, "sizeGuide", "602", "550", "yes" );
			}
		break;
		}
	}
}

function popup( vURL, vWinName, vWidth, vHeight, vScrollbar) {
//vWinName cannot contain spaces, leave vScrollbar blank for no scrollba
  
	if (! window.focus) {
		return true;
	}
	
	var vhref,
	vSWidth = screen.width,
	vSHeight = screen.height;
	
	if(vScrollbar == "yes") {
		vWidth = new Number(vWidth) + 16
	}
	
	var vleftPos = (vSWidth / 2) - (vWidth / 2)
	var vtopPos = (vSHeight / 2) - (vHeight / 2)
	
	if (typeof(vURL) == 'string') {
		vhref = vURL;
	} else {
		vhref = vURL.href;
	}
	
	window.open(vhref, vWinName, 'scrollbars=' + vScrollbar + ',statusbar=no,menubar=0,width=' + vWidth + ',height=' + vHeight + ',left=' + vleftPos + ',top=' + vtopPos);	
	
	if (window.event) {
	  var e = window.event;
		//var target = e.srcElement;
//		
//		if (target.tagName === "IMG") {
//			target = target.parentNode;
//		}
//		
//		var theHref = target.getAttribute("href");
//		var linkID = target.getAttribute("id");
//				
//		target.removeAttribute("href");
		
		//readdTheHref(linkID, theHref);
		
		e.returnValue = false;
		return false;
	} else {
	  return false;
	}
}

//function readdTheHref(linkID, theHref) {
//		
//	var theLink = document.getElementById(linkID);
//  theLink.setAttribute("href", theHref);
//}

function addBookmark(title,url) {
  if (window.sidebar) {
    window.sidebar.addPanel(title, url,"");
  } else if( document.all ) {
    window.external.AddFavorite( url, title);
  } else if( window.opera && window.print ) {
    return true;
  }
}

addLoadEvent(faqClick);
function faqClick(){
	//loop through all question links in container and attach onclick to execute activateTip
	//pass question's id to function and append _a to get name of answer id
	if (!document.getElementById("questions")){ return false; }
	var QBox = document.getElementById("questions");
	var questionList = QBox.getElementsByTagName("a");
	if (questionList.length < 1) return false;
		for ( var i=0; i < questionList.length; i++){
			var thisq = i+1;
			//alert(which);
			questionList[i].onclick = function(){
				activateTip(this.id);
			return true;
			}
		}
}

//faqs
//declare the last var and set an initial state (to stop errors)
var last = 'q1';
function activateTip(which){
	//remove any existing highlight
	toTheTop();
	//apply the new highlight
	document.getElementById(which+'_a').className='question on';
	//set the last tag so we know which to remove next time
	last = which;
}

function toTheTop(){
//reset the background image
	document.getElementById(last+'_a').className="question";
//reset the text color
}