var isIE = (navigator.appName.indexOf("Microsoft") != -1);

// --------------------------------------------------------------------

function ambil ()
{
	//alert ("ambil");
	var id = window.location.hash.replace("#", "");
	
	//alert ("ID: " +id);
		return id;
}

function get_url_prefix ()
{
	var id = base_path;
	return id;
}

function coba ()
{
	var gal = $("gallery").className;
	//alert ("gallery: " + gal);
}

function $(id) { return document.getElementById(id); }
var filename;
var links;
function LoadhotLinks()
{
	  //alert (filename + "---" + base_url);
    //var base_url = 'http://192.168.0.254/panoverse/';
   //var base_url = 'http://panoverse.gotphp.com/staging/';
   // alert('loadhotlinks ');
    $j.post(base_url+"manage/get_nav_contents", { filename: filename,base_url:base_url },
        function(data){
           // alert("Data Loaded: " + data);
            //Split data on '|'
            var next = 0;
			if ($('lastID').innerHTML)
            	$('lastID').innerHTML = data;
           // alert('length '+links.length);
 /*           if(links.length > 1)
                {
                    for(var i=0;i < links.length;i++)
                    {
                       // alert('val '+links[i]);
                        setTimeout( "$('mymovie').highlightLink('"+links[i]+"')", next);
                        if(i == 0) { pause(3000) };
                        next += 3000;
                    }
                }
            else
                {
                    //alert('else');
                    setTimeout( "$('mymovie').highlightLink('"+data+"')", next);
                }

                if(links.length > 0)
                setTimeout( "$('mymovie').unhighlightLink()", next);
    
   */  
}
       
    );
}

function pause(numberMillis)
{
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true)
    {
    now = new Date();
    if (now.getTime() > exitTime)
    return;
    }
}

function show(div)
{
	//div.style.visibility = "visible";
/* 	div.style.display = ""; */
	$j('#'+div).show();
}

function hide(div)
{
	//div.style.visibility = "hidden";
/* 	div.style.display = "none"; */
	$j('#'+div).hide();
}

function setVisible(div, flag)
{
/* 	(flag ? show : hide)(div); */

	if (flag) {
		$j('#'+div).show();
	} else {
		$j('#'+div).hide();
	}
}

/* function show$(id) { show($(id)); } */
function show$(id) { show(id); }

/* function hide$(id) { hide($(id)); } */
function hide$(id) { hide(id); }

/* function setVisible$(id, flag) { setVisible($(id), flag); } */
function setVisible$(id, flag) { setVisible(id, flag); }

var currentID;

var leftWidth = 320;

function editEnabled()
{
	//return (window.location.search.indexOf("edit=true") != -1);
	return true;
}

function HandlerMode(div, event, handler)
{
	this.div = div;
	this.event = event;
	this.handler = handler;
}

HandlerMode.prototype.set = function()
{
	//if(this.div.attachEvent) this.div.attachEvent("on"+this.event, this.handler);
	//if(this.div.addEventListener) this.div.addEventListener(this.event, this.handler, false);
}

HandlerMode.prototype.clear = function()
{
	if(this.div.detachEvent) this.div.detachEvent("on"+this.event, this.handler);
	if(this.div.removeEventListener) this.div.removeEventListener(this.event, this.handler, false);
}

var lastSeenStartHelp = 0;
var lastSeenStartHelpCounter = 0;
var links = '';

function showNewVisitorWelcomeHelpInterval() {

	    links = $('lastID').innerHTML.split('|')
		
	//alert (file_default_num);
	if ($("mymovie-" + file_default_num))
	{
      if(lastSeenStartHelp < links.length) {
               if(typeof $("mymovie-" + file_default_num).highlightLink2 == 'function'){
                 $("mymovie-" + file_default_num).highlightLink2(links[lastSeenStartHelp], links.length);
                 lastSeenStartHelp++;   
               }
       } else {
               if(lastSeenStartHelpCounter == 0) {
                       lastSeenStartHelp = 0;
                       lastSeenStartHelpCounter = 1;
               } else {
					   $("mymovie-" + file_default_num).unhighlightLink2();
					   clearInterval (internalID);
                       //hide$('null_icon');
               }
       }
	}
	else
	{
		//alert ("asd");
		clearInterval (internalID);
	}
	
}
var globalHistoryPrev, globalHistoryNext;

function onLoad()
{
  

	//setInterval(showNewVisitorWelcomeHelpInterval, 600);
  if (editEnabled())
	{
		var c = readCookie("gallery");//alert (c);
		gallery = (c && (c != "")) ? c.split("%2C") : [];
		//alert('length '+gallery.length);
        for (var i in gallery) {
 			//alert('main id '+gallery[i]);
            addGalleryImage(gallery[i]);
		}
	}

	var id = window.location.hash.replace("#", "");//alert ("11111: " + id + "---" + isIE);
	if ((id == "") && editEnabled())
	{
		var i = window.location.search.indexOf("id=");
		if (i != -1)
			id = window.location.search.substr(i + 3);
	}
	if ((id == "") && !editEnabled())
		id = gallery[gallery.length - 1] || "0OxmORJu";

	window.getFirstID = function()
	{//alert ("11111->get: " + id);
		for (var i in (temp = ["left_column", "description"]))
		{
			new HandlerMode($(temp[i]), "mousemove", function()
			{
				$("mymovie").setMouseIn(false);
			}).set();
		}
		return id;
	}
	/*alert('basepath '+base_path);
	var so = new SWFObject(base_path + "viewer.swf?" + Math.random(), "mymovie", "600", "400", "8", "dddddd");
	so.write("flash");

	alert('flash'+so.write("flash"));
	(so.write("flash")) ? sleep(1) : alert('not loaded') ;
	setTimeout('so.write("flash")',1000);


 	$("add_image_file").value = ""; 
 	updateForm(); 
	*/
	if (editEnabled())
	{
		show$("actions_dialog");
		hide$("recent");
	}

	if (!isIE)
	{
        //alert('! IE');
		//setInterval(function()
		//{
			if ((window.location.hash != "") && (window.location.hash != ("#" + currentID))){
				loadNav(window.location.hash.replace("#", ""));
            }
				//}, 4000);
	}
    else
        {loadNav(window.location.hash.replace("#", ""));
          /*alert('curr '+id + "---" + base_path);
		  swfobject.embedSWF(base_path + "viewer.swf?" + Math.random(), "mymovie", "800", "400", "10.0.0");*/
/*
          var so = new SWFObject(base_path + "viewer.swf?" + Math.random(), "mymovie", "800", "400", "10", "#336699");
	      so.addVariable("id", id);
		  so.addVariable("urlPrefix", "/");
          so.write("flash");
		  alert ("asd: " + so.write("flash"));*/
		  
        }

}

function updateForm()
{
	// $("add_image_ok").disabled = !($("add_image_file").value || $("add_image_url").value);
}

var script = false;
var description = false;

var randomKey = Math.random();

var historyStack = [], historyPosition = -1;

function loadNav(id, dontPushHistory, forceReload)
{
    //alert('dontPushHistory '+dontPushHistory);
    //alert('forceReload '+forceReload);
	if (!navEnabled())
	{
		return;
	}

	var idWasCurrent = (id == currentID);

	if (idWasCurrent && !forceReload)
	{
		return;
	}

	if (isIE && !dontPushHistory)
	{
		if (historyPosition < (historyStack.length - 1))
		{
			historyStack.splice(historyPosition + 1, historyStack.length - historyPosition - 1);
		}
		historyStack.push(id);
		historyPosition += 1;
	}

	currentID = id;
	var head = document.getElementsByTagName("head").item(0);
	window.nav = function(arg)
	{
		//alert('window nav '+url_prefix);
		$("mymovie").setUrlPrefix(url_prefix);
		$("mymovie").setNavigationInfo(arg);
		//alert('before if '+id);
		if (!editEnabled())
		{
			truncateGallery(0);
			var pause = 500;
			for (var id2 in arg.links)
			{
				alert('addtogallery1');
                addToGallery(id2);

				pause += 200;
				setTimeout( "$('mymovie').highlightLink('" + id2 + "')", pause);
			}
		}
		else
		{
			//alert('addtogallery2');
            addToGallery(id);
			//alert('here');
            setTimeout( "$('mymovie').highlightLink('" + id + "')", 500);
		}
		description = (arg.description ? utf8Decode(base64Decode(arg.description)) : "")
		description = description.replace(/<[^\/a]/g, "").replace("&", "&amp;");
        $("description").innerHTML = description;
		show$("current_image_actions");
	}
	if (script) {
		head.removeChild(script);
	}
	//alert('basepath '+base_path+'baseurl '+base_url);
	script = document.createElement("script");
	script.setAttribute("src",  base_path + "data/" + id + "/nav.js?" + (editEnabled() ? Math.random() : randomKey));
	script.setAttribute("id", id);
	head.appendChild(script);
	//alert('buat Nav.js2 window.location '+id);
	window.location.hash = "#" + id;
/* 	$("go_to_edit").href = "./?edit=true#" + id; */
/* 	$("go_to_edit").href += id; */
/* 	$("go_to_view").href = "./#" + id; */
       //alert('urlprefix');
        $("permalink").value = base_url+"index.php/viewer/index#" + id;
        $("embed_textarea").value =
		'<script src="'+base_url+'assets/js/jquery-1.3.1.js" type="text/javascript"></script>\n'+
       '<script type="text/javascript">var $j = jQuery.noConflict();</script>\n'+
       '<script src="'+base_url+'assets/js/main.js" type="text/javascript"></script>\n'+
       '<script src="'+base_url+'assets/js/cookie.js" type="text/javascript"></script>\n'+
       '<script type="text/javascript">window.onload = function () { onLoad_viewer();}</script>\n'+
        '<object width="400" height="300">\n' +
		'<param name="movie" value="'+base_url+'viewer.swf"></param>\n' +
		'<param name="FlashVars" id="flash" value="id=' + id + '&urlPrefix='+base_url+'"></param>\n' +
		'<embed name="mymovie" id="mymovie" src="'+base_url+'viewer.swf" FlashVars="id=' + id + '&urlPrefix='+base_url+'" type="application/x-shockwave-flash" width="400" height="300"></embed>\n' +
		'</object>'
		;

/* 	document.title = "Panoverse - Albums with 3D transitions"; */

	if (isIE && !idWasCurrent)
	{
		historyDisabled = true;
		//$("history_iframe").src = base_path + "history_back.html";
	}


    if(document.getElementById('show_next') != null && document.getElementById('show_next').value == 2)
        {
            $j("#tabs").tabs('select', 2);
            toggle_hotspots();
            document.getElementById('show_next').value = '';
        }
       
        LoadhotLinks(id);
}

function onLoad_viewer()
{
	var myElement = document.getElementById("flash");
    //alert('element '+myElement.value);
    var element_split = myElement.value.split('&');
    var get_element = element_split[0].split('id=');
   // alert('element id '+get_element[1]);
	var id = get_element[1];//alert ("22222: " + id);
	//alert('id '+id);
	if ((id == "") && editEnabled())
	{
		var i = window.location.search.indexOf("id=");
		if (i != -1)
			id = window.location.search.substr(i + 3);
	}
	if ((id == "") && !editEnabled())
		id = gallery[gallery.length - 1] || "0OxmORJu";

	window.getFirstID = function()
	{//alert ("22222->get: " + id);
		//alert('in window');
        for (var i in (temp = ["left_column", "description"]))
		{
			new HandlerMode($(temp[i]), "mousemove", function()
			{
				$("mymovie").setMouseIn(false);
			}).set();
		}
		return id;
	}

    if (editEnabled())
	{
		show$("actions_dialog");
		hide$("recent");
	}

	if (!isIE)
	{
        loadNav_viewer(id);

	}

}

function loadNav_viewer(id, dontPushHistory, forceReload)
{
    //alert('dontPushHistory '+dontPushHistory);
    //alert('forceReload '+forceReload);
	if (!navEnabled())
	{
		return;
	}

	var idWasCurrent = (id == currentID);

	if (idWasCurrent && !forceReload)
	{
		return;
	}

	if (isIE && !dontPushHistory)
	{
		if (historyPosition < (historyStack.length - 1))
		{
			historyStack.splice(historyPosition + 1, historyStack.length - historyPosition - 1);
		}
		historyStack.push(id);
		historyPosition += 1;
	}
	alert ("curr: " + id);
	currentID = id;
	var head = document.getElementsByTagName("head").item(0);
    var base_path = '/staging/';
    var base_url = 'http://panoverse.gotphp.com/staging/';
    var url_prefix = '/staging/';
	window.nav = function(arg)
	{
		$("mymovie").setUrlPrefix(url_prefix);
		$("mymovie").setNavigationInfo(arg);
		//alert('before if '+id);
		if (!editEnabled())
		{
			truncateGallery(0);
			var pause = 500;
			for (var id2 in arg.links)
			{
				//addToGallery(id2);

				pause += 200;
				setTimeout( "$('mymovie').highlightLink('" + id2 + "')", pause);
			}
		}
		else
		{
			//addToGallery(id);
			setTimeout( "$('mymovie').highlightLink('" + id + "')", 1000);
		}
		//description = (arg.description ? utf8Decode(base64Decode(arg.description)) : "")
		//description = description.replace(/<[^\/a]/g, "").replace("&", "&amp;");
		//$("description").innerHTML = description;
		show$("current_image_actions");
	}
	if (script) {
		head.removeChild(script);
	}
	//alert('before script '+id);
	script = document.createElement("script");
	
    script.setAttribute("src",  base_path + "data/" + id + "/nav.js?" + (editEnabled() ? Math.random() : randomKey));
	script.setAttribute("id", id);
	head.appendChild(script);
	//alert('buat Nav.js window.location '+id);
	window.location.hash = "#" + id;
/* 	$("go_to_edit").href = "./?edit=true#" + id; */
/* 	$("go_to_edit").href += id; */
/* 	$("go_to_view").href = "./#" + id; */

       /* $("permalink").value = base_url+"index.php/viewer/index#" + id;
        $("embed_textarea").value =
		'<object width="400" height="300">\n' +
		'<param name="movie" value="'+base_url+'viewer.swf"></param>\n' +
		'<param name="FlashVars" value="id=' + id + '&urlPrefix='+base_url+'"></param>\n' +
		'<embed src="'+base_url+'viewer.swf" FlashVars="id=' + id + '&urlPrefix='+base_url+'" type="application/x-shockwave-flash" width="400" height="300"></embed>\n' +
		'</object>'
		;*/

/* 	document.title = "Panoverse - Albums with 3D transitions"; */

	if (isIE && !idWasCurrent)
	{
		historyDisabled = true;
		$("history_iframe").src = base_path + "history_back.html";
	}


    if(document.getElementById('show_next') != null && document.getElementById('show_next').value == 2)
        {
            $j("#tabs").tabs('select', 2);
            toggle_hotspots();
            document.getElementById('show_next').value = '';
        }
        LoadhotLinks(id);
}

function onLoad_home_viewer(position)
{
	var myElement = document.getElementById("txt_flash"+position);
    
	var id = myElement.value;//alert ("3333: " + id);
	if ((id == "") && editEnabled())
	{
		var i = window.location.search.indexOf("id=");
		if (i != -1)
			id = window.location.search.substr(i + 3);
	}
	if ((id == "") && !editEnabled())
		id = gallery[gallery.length - 1] || "0OxmORJu";

	window.getFirstID = function()
	{//alert ("33333->get: " + id);
		for (var i in (temp = ["left_column", "description"]))
		{
			new HandlerMode($(temp[i]), "mousemove", function()
			{
				$("mymovie").setMouseIn(false);
			}).set();
		}
		return id;
	}

    if (editEnabled())
	{
		show$("actions_dialog");
		hide$("recent");
	}

	if (!isIE)
	{
        //alert('id '+id);
        loadNav_viewer(id);

	}

}

function loadNav1(id, w, h)
{
	$("mymovie").followLink(id, false);
}
var galleryClickHandler = loadNav1;

var editedID = false;



function editLinkCancel()
{
	alert('hi');
    $("mymovie").editLinkCancel();
	hide$("link_dialog_1");
	hide$("link_dialog_2");
	enableNav();
	$("gallery").className = "";
	galleryClickHandler = loadNav1;
}

var navEnabled_ = true;

function navEnabled()
{
	return navEnabled_;
}

function enableNav()
{
	show$("actions_dialog");
	navEnabled_ = true;
}

function disableNav()
{
	hide$("actions_dialog");
	navEnabled_ = false;
}

function editLinkOK()
{
	$("mymovie").editLinkOK();
    document.getElementById('show_next').value = 2; // Edit Tab should be selected.
}



function toggleEdit()
{
	$("mymovie").toggleEdit();
}



function toggleEmbed()
{
	(($("embed").style.display == "none") ? show$ : hide$)("embed");
}



