// Custom utility functions

function toTitleCase(str) {
    return str.substr(0,1).toUpperCase() + str.substr(1).toLowerCase();
}      

function formatTitle(title) {
    return 'SWFAddress Website' + (title != '/' ? ' / ' + toTitleCase(title.substr(1, title.length - 2).replace(/\//g, ' / ')) : '');
}


// Custom SWFAddress and Ajax handling

function getTransport() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}



function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function appear(content, value) {
    if (typeof value == 'undefined') value = 0;
    if (value > 1) return;
    var property = content.filters ? 'filter' : 'opacity';
    content.style[property] = content.filters ? 'alpha(opacity=' + value*100 + ')' : value;
    setTimeout(function () {appear(content, value + .1)} , 50);
}

function updateChange(xhr) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
		var strarray = xhr.responseText.split("~~~");
		//alert(xmlHttpReq.responseText);
		document.productimage.src=strarray[0];
		document.getElementById("producttitle").innerHTML = strarray[1];
		// document.getElementById("productcopy").innerHTML = strarray[2];
		// alert(strarray[6]);

		if (strarray[7]!=''){

			var myArray = strarray[7].split(",");

			for (var i=0; i<myArray.length; i++) {

				MM_preloadImages(myArray[i]);

			}
		}
		if (strarray[6]!=''){
					document.getElementById("productnumber").innerHTML = 'Reference #: <b>W'+strarray[5]+'</b><br>'+strarray[6];
		} else {
					document.getElementById("productnumber").innerHTML = 'Reference #: <b>W'+strarray[5]+'</b>';
		}
		currentpid = strarray[5];

		if (strarray[4]=='1'){
			// document.getElementById("productprice").innerHTML = '$'+strarray[3];
		}
        } else {
            alert('Error: ' + xhr.status + '!');
        }
    }
}

function handleChange(event) {
    var parameters = '';
    for (var p in event.parameters) {
        parameters += '&' + p + '=' + event.parameters[p];
    }
    // alert();

	if (p=='id'){
    var xhr = getTransport();
    xhr.onreadystatechange = function() {
        updateChange(xhr);
    }

	var url = '/viewprod.php?swfaddress=' + event.path + parameters;
    xhr.open('get', url, true);
    xhr.send('');
	} else {

		if (p=='page'){
		
		var strarray = event.parameters['page'].split("-");
		currentpage = parseInt(strarray[0]);
		document.getElementById("page1").innerHTML = strarray[0];
		frames['prodframe'].location.href = '/productlisting.php?id='+strarray[1]+'&page='+strarray[0];

		}
	}
    //SWFAddress.setTitle(formatTitle(event.path));    
}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);