var obj_PageGreyOut = null;

function BookmarkPage() {
	if (isIE()) {
		window.external.AddFavorite(location.href, document.title);
	} else {
		alert('Your browser does not support automatically adding this page to your favourites. \n\n Please use the Favourites/Bookmark menu.');
	}
 }
 
function isIE() {
	var agt = navigator.userAgent.toLowerCase();
	if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
		return true;
	} else {
		return false;
	}
}

function getRelativePos(obj) {
	var pos = {offsetLeft:0,offsetTop:0};
	while(obj!=null) {
		pos.offsetLeft += obj.offsetLeft;
		pos.offsetTop += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return pos;
}

function getDocHeight() {
	var int_DocHeight;
	if (window.innerHeight && window.scrollMaxY) {	
		int_DocHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		int_DocHeight = document.body.scrollHeight;
	} else {
		int_DocHeight = document.body.offsetHeight;
	}
	return int_DocHeight;
}

function getWindowHeight() {
	var int_WinHeight;
	if (window.innerHeight) {
		int_WinHeight = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		int_WinHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		int_WinHeight = document.body.clientHeight;
	}
	return int_WinHeight;
}

function getPageHeight() {
	var int_WinHeight = getWindowHeight();
	var int_DocHeight = getDocHeight();
	if (int_WinHeight > int_DocHeight) {
		return int_WinHeight;
	} else {
		return int_DocHeight;
	}
}

function openCentralDiv(int_Width, str_InnerHTML, bol_FadeIn, str_DivID) {
	if (bol_FadeIn == null) {
		bol_FadeIn = true;
	}
	var div_CentralDiv = document.createElement('div');
	if (str_DivID && str_DivID != null && str_DivID != '') {
		div_CentralDiv.id = str_DivID;
	}
	div_CentralDiv.style.position = 'absolute';
	var winW = document.body.offsetWidth;
	//var winH = document.body.offsetHeight;
	var winH = getWindowHeight();
	
	var int_ScrollOffset = 0;
	if (window.pageYOffset) {
		int_ScrollOffset = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		int_ScrollOffset = document.documentElement.scrollTop;
	} else if (document.body && document.body.scrollTop) {
		int_ScrollOffset = document.body.scrollTop;
	}
	
	var posX = (winW - int_Width)/2;
	//var posY = (winH - int_Height)/2;
	div_CentralDiv.style.width = int_Width+'px';
	//div_CentralDiv.style.height = int_Height+'px';
	div_CentralDiv.style.zIndex = 99;
	div_CentralDiv.innerHTML = str_InnerHTML;
	//div_CentralDiv.style.backgroundColor = '#FFFFFF';
	//div_CentralDiv.style.display = 'none';
	//var posX = (winW - div_CentralDiv.offsetWidth)/2;
	
	div_CentralDiv.style.left = posX+'px';
	
	document.body.appendChild(div_CentralDiv);
	
	var posY = int_ScrollOffset + ((winH - div_CentralDiv.offsetHeight)/2);
	if (posY < 0) { posY = 0; }
	div_CentralDiv.style.top = posY+'px';
	
	if (bol_FadeIn) {
		div_CentralDiv.style.display = 'none';
		new Effect.Appear(	div_CentralDiv,
							{
								duration:0.5,
								afterFinish: function() {
									var posY = int_ScrollOffset + ((winH - div_CentralDiv.offsetHeight)/2);
									if (posY < 0) { posY = 0; }
									div_CentralDiv.style.top = posY+'px';
									refreshScreenGreyOut();
								}
							}
						);
	} else {
		div_CentralDiv.style.display = 'block';
		var posY = (winH - div_CentralDiv.offsetHeight)/2;
		div_CentralDiv.style.top = posY+'px';
		refreshScreenGreyOut();
	}
	return div_CentralDiv;
}


function updateRoundedBoxColour(div_Container, str_NewColour) {
	if (isValidColour(str_NewColour)) {
		//alert(div_Box);
		var arr_BoxChildren = div_Container.childNodes;
		//alert(arr_BoxChildren);
		for (i=0;i<arr_BoxChildren.length;i++) {
			//alert( arr_BoxChildren[i].nodeType + arr_BoxChildren[i].nodeName);
			if (arr_BoxChildren[i].nodeType == 1 && arr_BoxChildren[i].nodeName == 'DIV') {
				arr_BoxDivs = arr_BoxChildren[i].childNodes;
				var int_DivsProcessed = 0;
				for (j=0;j<arr_BoxDivs.length;j++) {
					//alert(arr_BoxDivs[j].nodeName);
					if (arr_BoxDivs[j].nodeType == 1 && arr_BoxDivs[j].nodeName == 'DIV') {
						int_DivsProcessed++;
						if (int_DivsProcessed != 1 && int_DivsProcessed != 13) {
							arr_BoxDivs[j].style.backgroundColor = str_NewColour;
						}
					}
				}
			}
		}
	}
}

function updateRoundedBoxBorderColour(div_Container, str_NewColour) {
	if (isValidColour(str_NewColour)) {
		//alert(div_Box);
		var arr_BoxChildren = div_Container.childNodes;
		//alert(arr_BoxChildren);
		for (i=0;i<arr_BoxChildren.length;i++) {
			//alert( arr_BoxChildren[i].nodeType + arr_BoxChildren[i].nodeName);
			if (arr_BoxChildren[i].nodeType == 1 && arr_BoxChildren[i].nodeName == 'DIV') {
				arr_BoxDivs = arr_BoxChildren[i].childNodes;
				var int_DivsProcessed = 0;
				for (j=0;j<arr_BoxDivs.length;j++) {
					//alert(arr_BoxDivs[j].nodeName);
					if (arr_BoxDivs[j].nodeType == 1 && arr_BoxDivs[j].nodeName == 'DIV') {
						int_DivsProcessed++;
						if (int_DivsProcessed != 1 && int_DivsProcessed != 13) {
							arr_BoxDivs[j].style.borderColor = str_NewColour;
						} else {
							arr_BoxDivs[j].style.backgroundColor = str_NewColour;
						}
					}
				}
			}
		}
	}
}


function isValidColour(str_Colour) {
	var reHexColour = "^\#[0-9a-fA-F]{6}$";
	
	if (str_Colour.match(reHexColour)) {
		return true;
	} else {
		str_ColourNames = ",AliceBlue,AntiqueWhite,Aqua,Aquamarine,Azure,Beige,Bisque,Black,BlanchedAlmond,Blue,BlueViolet,Brown,BurlyWood,CadetBlue,Chartreuse,Chocolate,Coral,CornflowerBlue,Cornsilk,Crimson,Cyan,DarkBlue,DarkCyan,DarkGoldenRod,DarkGray,DarkGreen,DarkKhaki,DarkMagenta,DarkOliveGreen,Darkorange,DarkOrchid,DarkRed,DarkSalmon,DarkSeaGreen,DarkSlateBlue,DarkSlateGray,DarkTurquoise,DarkViolet,DeepPink,DeepSkyBlue,DimGray,DodgerBlue,FireBrick,FloralWhite,ForestGreen,Fuchsia,Gainsboro,GhostWhite,Gold,GoldenRod,Gray,Green,GreenYellow,HoneyDew,HotPink,IndianRed,Indigo,Ivory,Khaki,Lavender,LavenderBlush,LawnGreen,LemonChiffon,LightBlue,LightCoral,LightCyan,LightGoldenRodYellow,LightGreen,LightGrey,LightPink,LightSalmon,LightSeaGreen,LightSkyBlue,LightSlateGray,LightSteelBlue,LightYellow,Lime,LimeGreen,Linen,Magenta,Maroon,MediumAquaMarine,MediumBlue,MediumOrchid,MediumPurple,MediumSeaGreen,MediumSlateBlue,MediumSpringGreen,MediumTurquoise,MediumVioletRed,MidnightBlue,MintCream,MistyRose,Moccasin,NavajoWhite,Navy,OldLace,Olive,OliveDrab,Orange,OrangeRed,Orchid,PaleGoldenRod,PaleGreen,PaleTurquoise,PaleVioletRed,PapayaWhip,PeachPuff,Peru,Pink,Plum,PowderBlue,Purple,Red,RosyBrown,RoyalBlue,SaddleBrown,Salmon,SandyBrown,SeaGreen,SeaShell,Sienna,Silver,SkyBlue,SlateBlue,SlateGray,Snow,SpringGreen,SteelBlue,Tan,Teal,Thistle,Tomato,Turquoise,Violet,Wheat,White,WhiteSmoke,Yellow,YellowGreen,";
		if (str_ColourNames.toLowerCase().indexOf(','+str_Colour.toLowerCase()+',') > -1) {
			return true;
		} else {
			return false;
		}
	}
}



function openLargeImage(str_ID, str_FilePath, str_Caption, str_BorderColour, str_BackgroundColour) {
	var str_HTML = '';
	
	img_Preload = new Image();
	img_Preload.onload=function(){
		var int_BoxWidth = img_Preload.width;
		if (int_BoxWidth < 100) {
			int_BoxWidth = 100;
		}
		str_HTML = '\
			<div style="width:'+(int_BoxWidth+24)+'px;">\
				<div style="background-color:'+str_BorderColour+';overflow:hidden;height:1px;margin:0px 9px;"></div><div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:1px;border-left:3px solid '+str_BorderColour+';border-right:3px solid '+str_BorderColour+'; margin:0px 6px;"></div><div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:1px;border-left:2px solid '+str_BorderColour+';border-right:2px solid '+str_BorderColour+'; margin:0px 4px;"></div><div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:1px;border-left:1px solid '+str_BorderColour+';border-right:1px solid '+str_BorderColour+'; margin:0px 3px;"></div><div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:2px;border-left:1px solid '+str_BorderColour+';border-right:1px solid '+str_BorderColour+'; margin:0px 2px;"></div><div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:3px;border-left:1px solid '+str_BorderColour+';border-right:1px solid '+str_BorderColour+'; margin:0px 1px;"></div><div style="border-left:1px solid '+str_BorderColour+';border-right:1px solid '+str_BorderColour+';background-color:'+str_BackgroundColour+';padding:0px 10px 0px 10px;overflow:hidden;zoom:1;">\
					<div style="width:'+(int_BoxWidth+2)+'px;">\
						<div style="width:22px;float:right;margin:0 0 2px 0;"><img src="/images/icons/icon_remove.gif" width="20" height="20" style="border:1px solid '+str_BorderColour+';cursor:pointer;" onclick="closeLargeImage(\''+str_ID+'\');" /></div>\
						<img src="'+str_FilePath+'" width="'+img_Preload.width+'" height="'+img_Preload.height+'" style="border:1px solid '+str_BorderColour+';" onclick="closeLargeImage(\''+str_ID+'\');" />\
					</div>\
					<div style="margin: 5px 0 0 0;">\
					'+str_Caption+'\
					</div>\
				</div>\
				<div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:3px;border-left:1px solid '+str_BorderColour+';border-right:1px solid '+str_BorderColour+'; margin:0px 1px;"></div><div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:2px;border-left:1px solid '+str_BorderColour+';border-right:1px solid '+str_BorderColour+'; margin:0px 2px;"></div><div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:1px;border-left:1px solid '+str_BorderColour+';border-right:1px solid '+str_BorderColour+'; margin:0px 3px;"></div><div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:1px;border-left:2px solid '+str_BorderColour+';border-right:2px solid '+str_BorderColour+'; margin:0px 4px;"></div><div style="overflow:hidden;background-color:'+str_BackgroundColour+';height:1px;border-left:3px solid '+str_BorderColour+';border-right:3px solid '+str_BorderColour+'; margin:0px 6px;"></div><div style="background-color:'+str_BorderColour+';overflow:hidden;height:1px;margin:0px 9px;"></div>\
			</div>\
		';
		
		if (!obj_PageGreyOut) {
			obj_PageGreyOut = greyOutScreen();
		}
		
		openCentralDiv(int_BoxWidth+25, str_HTML, true, str_ID);
		
		return false;
	}
	
	img_Preload.src = str_FilePath;
}

function closeLargeImage(str_ID) {
	var imageDiv = document.getElementById(str_ID);
	imageDiv.parentNode.removeChild(imageDiv);
	closeScreenGreyOut();
}

function greyOutScreen(bol_FadeIn) {
	if (bol_FadeIn == null) {
		bol_FadeIn = true;
	}
	var int_Height = getPageHeight();
	var div_GreyOut = document.createElement('div');
	div_GreyOut.style.position = 'absolute';
	div_GreyOut.style.width = '100%';
	div_GreyOut.style.height = int_Height+'px';
	div_GreyOut.style.zIndex = 99;
	div_GreyOut.style.backgroundColor = '#000000';
	div_GreyOut.style.left = 0;
	div_GreyOut.style.top = 0;
	div_GreyOut.style.opacity = 0.5;
	div_GreyOut.style.filter = 'alpha(opacity=50)';
	
	document.body.appendChild(div_GreyOut);
	
	if (bol_FadeIn) {
		div_GreyOut.style.display = 'none';
		new Effect.Appear(	div_GreyOut,
							{
								duration:0.5,
								from:0.0,
								to:0.6
							}
						);
	} else {
		div_GreyOut.style.display = 'block';
	}
	
	return div_GreyOut;
}

function closeScreenGreyOut() {
	if(obj_PageGreyOut) {
		obj_PageGreyOut.parentNode.removeChild(obj_PageGreyOut);
		obj_PageGreyOut = null;
	}
}
function refreshScreenGreyOut() {
	if(obj_PageGreyOut) {
		var int_Height = getPageHeight();
		obj_PageGreyOut.style.height = int_Height+'px';
	}
}

function showHintDiv(divId) {
	showDiv = document.getElementById(divId);
	showDiv.style.visibility = 'visible';
}

function hideHintDiv(divId) {
	showDiv = document.getElementById(divId);
	showDiv.style.visibility = 'hidden';
}

function updateExampleRoyalty(str_SalePrice, num_CommissionRate, num_CommissionFee, str_PreviewSpanId, str_CurrencyChar) {
	span_RoyaltyPreview = document.getElementById(str_PreviewSpanId);
	
	if (str_SalePrice.match('^[0-9]*\\.{0,1}[0-9]{0,2}$')) {
		var num_Price = parseFloat(str_SalePrice);
		num_Price = num_Price.toFixed(2);
		if (!isNaN(num_Price)) {
			var num_Royalty = ((Math.floor(num_Price * (100-num_CommissionRate))/100) - num_CommissionFee);
			if (num_Royalty < 0) {
				num_Royalty = 0.00;
			}
			span_RoyaltyPreview.innerHTML = str_CurrencyChar+num_Royalty.toFixed(2);
			if (num_Royalty < 0.01) {
				span_RoyaltyPreview.style.backgroundColor = '#FFCCCC';
			} else {
				span_RoyaltyPreview.style.backgroundColor = '#AACCFF';	
			}
		} else {
			span_RoyaltyPreview.innerHTML = 'ERROR';
			span_RoyaltyPreview.style.backgroundColor = '#FFCCCC';
		}
	} else {
		span_RoyaltyPreview.innerHTML = 'ERROR';
		span_RoyaltyPreview.style.backgroundColor = '#FFCCCC';
	}
}


function openLocaleSelector(obj_OpenAt, int_LeftOffset, int_TopOffset) {
	
	var div_LocaleSelector;
	var objPos = getRelativePos(obj_OpenAt);
	
	div_LocaleSelector = document.createElement('div');
	div_LocaleSelector.id = 'div_LocaleSelector';
	div_LocaleSelector.style.width = '180px';
	div_LocaleSelector.style.height = '150px';
	div_LocaleSelector.style.position = 'absolute';
	div_LocaleSelector.style.backgroundColor = '#FFFFFF';
	div_LocaleSelector.style.border = '1px solid black';
	div_LocaleSelector.style.padding = '5px';
	div_LocaleSelector.style.fontSize = '10pt';
	div_LocaleSelector.style.fontWeight = 'bold';
	div_LocaleSelector.style.display = 'block';
	div_LocaleSelector.onmouseout = 	function(e_Event) {
											if (!e_Event) {
												var e_Event = window.event;
											}
											onMouseOutIgnoreChildren(this, e_Event, 'document.body.removeChild(document.getElementById(\'div_LocaleSelector\'));');
										}
	div_LocaleSelector.style.left = (objPos.offsetLeft+int_LeftOffset) + 'px';
	div_LocaleSelector.style.top = (objPos.offsetTop+int_TopOffset) + 'px';
	div_LocaleSelector.innerHTML = '	\
										<p><strong>Select region/currency</strong></p> \
										<table border="0" cellpadding="3" cellspacing="0"> \
											<tr><td><img src="/images/icons/flag_mini_uk.gif" onclick="changeUserLocale(1)" style="cursor:pointer;" border="0" /></td><td>&pound; GBP</td></tr> \
											<tr><td><img src="/images/icons/flag_mini_us.gif" onclick="changeUserLocale(2)" style="cursor:pointer;" border="0" /></td><td>$ USD</td></tr> \
											<tr><td><img src="/images/icons/flag_mini_eu.gif" onclick="changeUserLocale(3)" style="cursor:pointer;" border="0" /></td><td>&euro; EUR</td></tr> \
											<tr><td><img src="/images/icons/flag_mini_ca.gif" onclick="changeUserLocale(4)" style="cursor:pointer;" border="0" /></td><td>$ CAD</td></tr> \
										</table> \
									';
	document.body.appendChild(div_LocaleSelector);
}


function changeUserLocale(int_Locale) {
	var str_ThisURL = encodeURIComponent(window.location.href);
	
	window.location.href = '/change_locale.php?locale='+int_Locale+'&fromurl='+str_ThisURL;
}

function isChildOf(obj_Child, obj_Parent) {
	if( obj_Child != null ) {
		while(obj_Child.parentNode) {
			if((obj_Child = obj_Child.parentNode) == obj_Parent) {
				return true;
			}
		}
	}
	return false;
}

function onMouseOutIgnoreChildren(obj_Parent, e_Event, str_Code) {
	var obj_MouseOver = null;
	if(e_Event && e_Event.toElement) {
		obj_MouseOver = e_Event.toElement;
	} else if(e_Event && e_Event.relatedTarget) {
		obj_MouseOver = e_Event.relatedTarget;
	}
	if(!isChildOf(obj_MouseOver, obj_Parent) && obj_Parent != obj_MouseOver) {
		eval(str_Code);
	}
}


function loadHomePromo(promoId) {
	if (homePromoTimer) {clearTimeout(homePromoTimer);}
	Effect.Fade('homePromoBox', { duration: 0.2, from: 1.0, to: 0.1, afterFinish: function() {
		switch (promoId) {
			case 1:
				document.getElementById('homePromoBox').style.background = 'url(/images/template/home_promo_1.png)';
				document.getElementById('homePromo_Description').innerHTML = ' \
					<ul>\
						<li>Sell your music online, worldwide and in several currencies</li>\
						<li>Free &amp; instant account</li>\
						<li>Quick &amp; easy interface, automatic creation of your song samples</li>\
					</ul>\
				';
				document.getElementById('homePromo_BigText').innerHTML = 'Your music for sale online in minutes';
				document.getElementById('a_homePromoStep_1').className = 'homePromoSelectedStep';
				document.getElementById('a_homePromoStep_2').className = 'homePromoStep';
				document.getElementById('a_homePromoStep_3').className = 'homePromoStep';
				document.getElementById('a_homePromoStep_4').className = 'homePromoStep';
				homePromoTimer = setTimeout('loadHomePromo(2);', 4000);
				break;
			case 2:
				document.getElementById('homePromoBox').style.background = 'url(/images/template/home_promo_2.png)';
				document.getElementById('homePromo_Description').innerHTML = ' \
					<ul>\
						<li>Free account and no subscription fees</li>\
						<li>Low commission rates</li>\
						<li>We only make money when you do</li>\
					</ul>\
				';
				document.getElementById('homePromo_BigText').innerHTML = 'Your account costs you nothing';
				document.getElementById('a_homePromoStep_1').className = 'homePromoStep';
				document.getElementById('a_homePromoStep_2').className = 'homePromoSelectedStep';
				document.getElementById('a_homePromoStep_3').className = 'homePromoStep';
				document.getElementById('a_homePromoStep_4').className = 'homePromoStep';
				homePromoTimer = setTimeout('loadHomePromo(3);', 4000);
				break;
			case 3:
				document.getElementById('homePromoBox').style.background = 'url(/images/template/home_promo_3.png)';
				document.getElementById('homePromo_Description').innerHTML = ' \
					<ul>\
						<li>Choose your page colours</li>\
						<li>Choose your own sale prices</li>\
						<li>Upload & caption unlimited artwork</li>\
					</ul>\
				';
				document.getElementById('homePromo_BigText').innerHTML = 'Your own fully-customisable MP3 shop page';
				document.getElementById('a_homePromoStep_1').className = 'homePromoStep';
				document.getElementById('a_homePromoStep_2').className = 'homePromoStep';
				document.getElementById('a_homePromoStep_3').className = 'homePromoSelectedStep';
				document.getElementById('a_homePromoStep_4').className = 'homePromoStep';
				homePromoTimer = setTimeout('loadHomePromo(4);', 4000);
				break;
			case 4:
				document.getElementById('homePromoBox').style.background = 'url(/images/template/home_promo_4.png)';
				document.getElementById('homePromo_Description').innerHTML = ' \
					<ul>\
						<li>Use our web widgets to sell your music on anywhere on the web!</li>\
						<li>Customise each widget’s size, colours, heading and sale items</li>\
						<li>All at no extra cost</li>\
					</ul>\
				';
				document.getElementById('homePromo_BigText').innerHTML = 'Your songs for sale on your own website';
				document.getElementById('a_homePromoStep_1').className = 'homePromoStep';
				document.getElementById('a_homePromoStep_2').className = 'homePromoStep';
				document.getElementById('a_homePromoStep_3').className = 'homePromoStep';
				document.getElementById('a_homePromoStep_4').className = 'homePromoSelectedStep';
				homePromoTimer = setTimeout('loadHomePromo(1);', 4000);
				break;
		}
		Effect.Appear('homePromoBox', { duration: 0.5, from: 0.1, to: 1.0 });
	}});
}


function playIconHover(imgIcon, bolHover) {
	if (imgIcon.src.indexOf('icon_play.gif') > -1 || imgIcon.src.indexOf('icon_play_hover.gif') > -1) {
		if (bolHover) {
			imgIcon.src = '/images/icons/icon_play_hover.gif';
		} else {
			imgIcon.src = '/images/icons/icon_play.gif';
		}
	}
}

function trolleyIconHover(imgIcon, bolHover) {
	if (imgIcon.src.indexOf('icon_trolley.gif') > -1 || imgIcon.src.indexOf('icon_trolley_hover.gif') > -1) {
		if (bolHover) {
			imgIcon.src = '/images/icons/icon_trolley_hover.gif';
		} else {
			imgIcon.src = '/images/icons/icon_trolley.gif';
		}
	}
}