// I handle mouse-over states for images
function switcher(i) {
	var id = document.getElementById(i);
	var s = id.src;
	var type;
	var src;
	var nsrc;
	
	if (s.indexOf(".gif") >= 0) {
		type = ".gif";
	} else if (s.indexOf(".jpg") >= 0) {
		type = ".jpg";
	} else {
		alert("This is not an image.");
	}
	
	src = s.replace(type, "");
	
	if (src.indexOf("-on") == -1) {
		nsrc = src + "-on" + type;
	} else {
		nsrc = src.replace(/-on/, "");
		nsrc += type;
	}
	
	id.src = nsrc;
}

// I select the larger version of a thumbnail
function thumbnail(thumb,destination) {
	var source = thumb.src;
	var alt = thumb.alt;
	var newsource = source.replace("T.gif",".gif");
	var sendto = document.getElementById(destination);
	sendto.src = newsource;
	sendto.alt = alt;
}

// I load Flash movies
var required = DetectFlashVer('8','0','0');
var tr = 0;

function fl(url,w,h,t) {
	var fla;
	if (required) {
		fla = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + w + '" height="' + h + '">';
		fla += '<param name="movie" value="' + url + '" />';
		fla += '<param name="quality" value="high" />';
		if (tr == 0) {
			fla += '<param name="wmode" value="transparent" />';
		} else {
		}
		fla += '<embed src="' + url + '" quality="high" pluginspage="https://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"';
		if (tr == 0) {
			fla += 'wmode="transparent" ';
		} else {
		}
		fla += 'width="' + w + '" height="' + h + '"></embed>';
		fla +='</object>';
	} else {
		fla = '<p>You do not have the required version of the <a href="https://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">Adobe Flash</a> plug-in. Please download and install the plug-in to experience this site in full.</p>';
	}
	
	if (t == null) {
		document.write(fla);
	} else {
		document.getElementById(t).innerHTML = fla;
	}
}

// We show and hide drop-down menus
function show(d) {
	a = document.getElementById(d);
	a.className = "on";
}

function hide(d) {
	a = document.getElementById(d);
	a.className = "off";
}

// We load the Flash player XML and populate the player on the page
var vidreq;
var vidurl;
var vidholder;
var vidfield;
var vidshow = 0;
var playeron = 1;
var vidshowlength;
var displayid = 0;

function getvideos(code) {
	vidreq = null;
	var vidurl = 'Flash/XML.aspx?Code=' + code;
	if (window.XMLHttpRequest) {
		vidreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		vidreq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (vidreq != null) {
		vidreq.onreadystatechange = loadvideos;
		vidreq.open("GET",vidurl,true);
		vidreq.send(null);
	} else {
		alert("Your browser cannot use this feature.");
	}
}

function loadvideos() {
	if (vidreq.readyState == 4) {
		if (vidreq.status == 200) {
			vidholder = vidreq.responseXML;
			vidfield = document.getElementById('BSJFlashXML');
			var allvids = vidholder.getElementsByTagName('Layout');
			var allids = vidholder.getElementsByTagName('VideoID');
			var alltitles = vidholder.getElementsByTagName('Title');
			var alldescriptions = vidholder.getElementsByTagName('ShortTitle');
			var allurls = vidholder.getElementsByTagName('FLVFile');
			var allthumbs = vidholder.getElementsByTagName('Thumbnail');
			if (vidshow == 0) {
				vidshowlength = allvids.length;
			} else {
				vidshowlength = vidshow;
			}
			for (i = 0; i < vidshowlength; i++) {
				var videoid = allids[i].childNodes[0].nodeValue;
				var videotitle = alltitles[i].childNodes[0].nodeValue;
				var videodescription = alldescriptions[i].childNodes[0].nodeValue;
				var videourl = allurls[i].childNodes[0].nodeValue;
				var videothumb = allthumbs[i].childNodes[0].nodeValue;
				var videodivid = 'v' + videoid;
				var videoimageid = 'vi' + videoid;
				var videotitleid = 'vt' + videoid;
				var videolinkid = 'vl' + videoid;
				var videodiv = document.createElement('div');
				videodiv.setAttribute('id',videodivid);
				videodiv.setAttribute('class','BSJVideoDiv');
				var videoimage = document.createElement('img');
				videoimage.setAttribute('id',videoimageid);
				videoimage.setAttribute('src',videothumb);
				videoimage.setAttribute('align','left');
				var videotext = '<a id="' + videotitleid + '">' + videotitle + '</a><br /><br />';
				videotext += videodescription + '<br /><a id="' + videolinkid + '">Click here to play the video</a>';
				var videoparagraph = document.createElement('p');
				videoparagraph.innerHTML = videotext;
				videodiv.appendChild(videoimage);
				videodiv.appendChild(videoparagraph);
				vidfield.appendChild(videodiv);
				videodiv.className = 'BSJVideoDiv';
				
				linkattach(videoimageid,videourl);
				linkattach(videotitleid,videourl);
				linkattach(videolinkid,videourl);
				if (displayid != 0 && displayid == videoid) {
					fl('Flash/MoviePlayer.swf?videoFile='+videourl+'&sts=t','440','358','BSJFlashPlayer');
				} else if (displayid == 0 && i == 0) {
					fl('Flash/MoviePlayer.swf?videoFile='+videourl,'440','358','BSJFlashPlayer');
				}
			}
		} else {
			alert("No videos!");
        }
    }
}

function linkattach(ob,tar) {
	var object = document.getElementById(ob);
	object.onclick = function() {
		fl('Flash/MoviePlayer.swf?videoFile='+tar+'&sts=t','440','358','BSJFlashPlayer');
	}
}

// We change the font size
var fontsizerequest;
var fontsizevariable;

function getfontsize(size) {
	fontsizerequest = null;
	fontsizevariable = size;
	var fonturl = 'FontSize.aspx?FontSize=' + size;
	if (window.XMLHttpRequest) {
		fontsizerequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		fontsizerequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (fontsizerequest != null) {
		fontsizerequest.onreadystatechange = loadfontsize;
		fontsizerequest.open("GET",fonturl,true);
		fontsizerequest.send(null);
	} else {
		alert("Your browser cannot use this feature.");
	}
}

function loadfontsize() {
	if (fontsizerequest.readyState == 4) {
		if (fontsizerequest.status == 200) {
			var smallicon = document.getElementById('FontSmall');
			var largeicon = document.getElementById('FontLarge');
			var panel = document.getElementById('BSJRightPanel');
			if (fontsizevariable == 0) {
				smallicon.src = "Images/FontSmall-on.gif";
				largeicon.src = "Images/FontLarge.gif";
				panel.className = "BSJRightPanel";
			} else if (fontsizevariable == 1) {
				smallicon.src = "Images/FontSmall.gif";
				largeicon.src = "Images/FontLarge-on.gif";
				panel.className = "BSJRightPanel LargeFont";
			}
		} else {
			alert("Unable to change font size!");
        }
    }
}

// I change the News year
function ddrefresh(formid,dropdownid,yearfield) {
	var menu = document.getElementById(dropdownid);
	var menuform = document.getElementById(formid);
	var menuyear = document.getElementById(yearfield);
	menuyear.value = menu.options[menu.selectedIndex].value;
	menuform.submit();
}

// I update the cart actions
function changeAction(ActionNumber) {
	document.forms["CartForm"].Action.value = ActionNumber;
}

//I open a new window for images
function largerImage(itemid){
    window.open('ItemImage.aspx?ItemID=' + itemid, 'newwindow', config='height=500,width=500,toolbar=no,menubar=no,scrollbars=no');
}