var htmlPath = '';//'/media/resource/viewer/901/';
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars[hash[0]] = hash[1];
    }
	
    return vars;
}

function vfmFPoint(el) {
    if( document.all ) {
		return document.all(el);
	} else {
		return document.getElementById(el);
	}
}

function replaceExisitingValues(opt, rep) {
    for(var name in rep) {
        if(rep[name] != '') {
            opt[name] = rep[name];
        }
    }
	if(!opt.bgcolor) opt.bgcolor = 'FFFFFF';
	document.bgColor = opt.bgcolor;
    return opt;
}

function loadPanorama(url, width, height, x, y) {
	if($.browser.msie) {
		width++;
		height++;
    }
    $('#IframeDiv').css({width: width, height: height, left: x, top: y});
    $('#vrframe').attr("src", htmlPath+'panorama/panFrame.html?url='+url);
}

function unloadPanorama() {
    $('#IframeDiv').css('display', 'none');
    $('#vrframe').attr("src", htmlPath+'panorama/panFrame.html');
}

function loadSwf(params, width, height, x, y) {
	var randomnumber=Math.floor(Math.random()*11);
	if($.browser.msie) {
		width++;
		height++;
    }
	$('#IframeDiv').css({width: width, height: height, left: x, top: y});
    $('#vrframe').attr("src", htmlPath+'swf/index.html?rand='+randomnumber+'&width='+width+'&height='+height+'&'+params);
}

function unloadSwf() {
    $('#IframeDiv', top.document ).css('display', 'none');
	$('#vrframe', top.document ).attr("src", htmlPath+'panorama/panFrame.html');
}

function loadUrl(url, width, height){
	width = width? width : 1000;
	height = height ? height : 600;
    window.open(url,'htmlWindow','width='+width+',height='+height+', location=yes, toolbar=yes, resizable=yes, scrollbars=yes');
}

function setLanguages(width, height, x, y, options) {
	if(width && height) {
		$('#language').css({width: width, height: height, left: x, top: y, display: 'block'});
	}
	if(options) {
		var myOptions = new Object();
		for(var i=0; i < options.length; i++) {

			myOptions[options[i].id] = options[i].label;
			if(options[i].id == 0) {
				$("#languageBox").addOption(myOptions, true)
			} else {
				$("#languageBox").addOption(myOptions, false)
			}
		}
	}
}

function selectLanguage() {
	var foundLang = false;
	var url;
	var params;
	
	if(window.location.href.indexOf('?') > 0) {
		url = window.location.href.substring(0, window.location.href.indexOf('?')+1);
		params = getUrlVars();
	} else {
		url = window.location.href+'?';
	}
	
	if(params) {
		for(var name in params) {
			if(name == 'iso_lang') {
				foundLang = true;
				params[name] = $("#languageBox").selectedValues();
			}
			if(name != 'lang' && name != null) {
				url += name + '='+params[name]+'&';
			}
		}
	}
	
	if(!foundLang) {
		url += 'iso_lang='+ $("#languageBox").selectedValues();
	}

	window.location = url;
}

function utf8_encode(string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }

    }

    return utftext;
}

function utf8_decode(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
			
        }

        return string;
 }
