var intervalID, opacity, height, amount, copyingAsset, assetinfofetched = false, assetInfoHovering = false;
var hoverTimeout = 750;

function sizedpopup(url, top, left, width, height) {
	return accessPopup(url, 'viewassetchild2', 'top='+top+',left='+left+',width='+width+',height='+height+',resizable=yes,scrollbars=yes');
}
function popup(url) {
	return accessPopup(url, 'viewassetchild', 'top=200,left=200,width=600,height=500,resizable=yes,scrollbars=yes');
}
function assetpopup(url) {
	return accessPopup(url, 'viewassetchild', 'top=0,left=0,width=1000,height=720,resizable=yes,scrollbars=yes');
}

function GenericProfilePopup(url, windowname, width, height) {
    window.open(url, windowname, "height=" + height + ",width=" + width + ",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=yes");
}

function cvpopup(url) {	return assetpopup(url); }

function reply(parentoid, assetoid, type) {
    var toOpen = 'comment/comment.aspx?parentoid=' + parentoid + '&assetoid=' + assetoid;
    if(typeof(type) != "undefined")
        toOpen += "&type=" + type;
	popup(toOpen);
}

function replyGWBlog(parentoid, assetoid, type) {
    var toOpen = 'gateway/comment.aspx?parentoid=' + parentoid + '&assetoid=' + assetoid;
    if (typeof (type) != "undefined")
        toOpen += "&type=" + type;
    //popup(toOpen);
    accessPopup(toOpen, 'replygwblog', 'top=200,left=200,width=600,height=500,resizable=yes,scrollbars=yes');
}

function editGWBlog(oid, assetoid) {
    //popup('gateway/comment.aspx?oid=' + oid + '&assetoid=' + assetoid);
    accessPopup('gateway/comment.aspx?oid=' + oid + '&assetoid=' + assetoid, 'editgwblog', 'top=200,left=200,width=600,height=500,resizable=yes,scrollbars=yes');
}

function popupProfiler(url){
    window.open(url,"babywindow","height=600,width=1000,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=yes");
}
function feedback(assetoid) {
	popup('comment/comment.aspx?parentoid=' + assetoid + '&assetoid=' + assetoid + '&mode=feedback');
}
function edit(oid, assetoid) {
	popup('comment/comment.aspx?oid=' + oid + '&assetoid=' + assetoid);
}
function remove(oid, assetoid) {
	accessPopup('viewasset/removeobject.aspx?oid=' + oid + '&assetoid=' + assetoid, 'viewassetchild', 'top=200,left=200,width=440,height=200,resizable=yes');
}
function removelink(oid, parentoid) {
	accessPopup('viewasset/removelink.aspx?oid=' + oid + '&parentoid=' + parentoid, 'viewassetchild', 'top=200,left=200,width=440,height=200,resizable=yes');
}
function removeweblink(oid, linkid) {
    accessPopup('viewasset/removelink.aspx?parentoid=' + oid + '&linkid=' + linkid, 'viewassetchild', 'top=200,left=200,width=440,height=200,resizable=yes');
}
function editpopup(url) {
	return accessPopup(url, 'baby', 'top=200,left=200,width=650,height=450,resizable=yes,scrollbars=yes');
}
function smallpopup(url) {
	return accessPopup(url, 'viewassetchild', 'top=200,left=200,width=400,height=300,resizable=no,scrollbars=no');
}
function flickrpopup(url){
    return accessPopup(url,"babywindow","height=600,width=900,toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizeable=1");
}
function openChild(url){
    return accessPopup(url,"babywindow","height=600,width=600,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizeable=0");
}
function download(url) {
	window.open(url, 'viewassetchild', '', null);
}

function autoShowComments() {
    if (location.href.indexOf("#commentsanchor") > 0) {
        var el = document.getElementById("commentscontent");
        if (el != null) {
            doSetVisible(el, true, false);            
        }
    }
}

function findSection(node) {
    var section;
    
    if (node.tagName != "A") {
	    section = node;
	} else {
	    section = node.parentNode.nextSibling;
	}
	
	while (section.nodeType != 1) {
		section = section.nextSibling;
	}
	
	return section;
}

function setVisible(node) {
    var section = findSection(node);
	
	if (section.style.display == 'none') {
	    doSetVisible(node, true, true);
	} else {
	    doSetVisible(node, false, true);
	}
}

function doSetVisible(node, visible, userClick) {
    var section = findSection(node);
	var heading;
	
    if (node.tagName != "A") {
	    heading = node.previousSibling;
	} else {
	    heading = node.parentNode;
	}
	
	var link = null;
	for (var i = 0 ; i < heading.childNodes.length; i++) {
	    if (heading.childNodes[i].name == 'hidesectionlink') {
	        link = heading.childNodes[i];
	    }
	}
	
	var tree = false;	
	var timeout = 999;
	
	if (section.id == "treecontent") tree = true;
	if (tree) timeout = 1;	
	
	if (visible == true) {
	    if (link != null) link.innerHTML = 'Hide';
	    section.style.display = 'block';
	    if (userClick) SetCookie(section.id, 'open', timeout);
	} else {
	    if (link != null) link.innerHTML = 'Show';
	    section.style.display = 'none';
	    if (userClick) SetCookie(section.id, 'closed', timeout);
	}
	
	if (tree) showTree();
}

function doSetBoxStatus(boxid) {
    var el = document.getElementById(boxid);
    if (typeof(el) != "undefined" && el != null){
        if (ReadCookie(boxid) == 'open'){
            doSetVisible(el, true, false);
        }
    }
    
    if(ReadCookie("linkscontent") == 'closed'){
        var links = document.getElementById("linkscontent");
        if(typeof(links) != "undefined" && links != null){
            setVisible(links);
        }
    }
}

function setBoxStatus(){
    doSetBoxStatus("treecontent");
    doSetBoxStatus("commentscontent");
    doSetBoxStatus("pagecommentscontent");
    doSetBoxStatus("notescontent");
    doSetBoxStatus("reviewscontent");
    doSetBoxStatus("linkscontent");
}

function SetCookie(cookieName,cookieValue,nDays) {
    var today = new Date();
    var expire = new Date();   
    if (nDays==null || nDays==0) nDays=1;
    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}
function ReadCookie(cookieName) {
    var theCookie=""+document.cookie;
    var ind=theCookie.indexOf(cookieName);
    if (ind==-1 || cookieName=="") return ""; 
    var ind1=theCookie.indexOf(';',ind);
    if (ind1==-1) ind1=theCookie.length; 
    var cookieVal = unescape(theCookie.substring(ind+cookieName.length+1,ind1))
    return cookieVal;
}

function showTree() {
	if ((document.getElementById('treecontent').innerHTML == "")) {
		document.getElementById('treecontent').innerHTML = "<img class=\"loading\" src=\"images/webfolio/loading.gif\" />";
		PebbleLearning.Pebblepad.AjaxService.getTree(globalAssetOid, showTreeCallback, showTreeError);
	}
}
function showTreeCallback(result) {
	document.getElementById('treecontent').innerHTML = result;
}
function showTreeError(result) {
    if (confirm('Sorry, the tree view too large to open inline.  Do you wish to open it in a new window?')) {
        popup('treeview.aspx?oid=' + globalAssetOid + '&type=all');
    }
    document.getElementById('treecontent').innerHTML = '';
}
function printAsset(oid, type) {
    popup('printasset.aspx?oid=' + oid + '&type=' + type + '&printmode=true');
}
function printSet(oid, type, setid) {
    popup('printasset.aspx?oid=' + oid + '&type=' + type + "&setid=" + setid + '&printmode=true');
}
function downloadAsset(oid, type) {
    popup('printasset.aspx?word=true&oid=' + oid + '&type=' + type);
}
function refreshComments() {
    document.getElementById("refreshBtn").click();
}


function preCopyAsset(oid) {
    copyingAsset = oid;
    document.getElementById("copyli").style.display = "none";
	PebbleLearning.Pebblepad.AjaxService.getTreeCount(oid, preCopyAssetCallback);
}
function preCopyAssetCallback(result) {
    var text = i18n['WillCopy'];
    //if (result == 1) plural = "";
    text = text.replace("{{result}}", result);
    
	if (confirm(text)) {
	    copyAsset();
	} else {
	    document.getElementById("copyli").style.display = "";
	}
}
function copyAsset() {
	PebbleLearning.Pebblepad.AjaxService.copyObject(copyingAsset, copyAssetCallback);
}
function copyAssetCallback(result) {
	alert(i18n['AssetCopied']);
}

function clearNotifications(oid) {
    PebbleLearning.Pebblepad.AjaxService.clearNotifications(oid, refreshComments);
}

/* Blog specific */
function newthought(blogoid) {
	window.open('blog/thought.aspx?blogoid=' + blogoid, 'newpost', 'top=50,left=50,width=600,height=500,resizable=yes,scrollbars=yes');
}
function editthought(thoid) {
	window.open('blog/thought.aspx?oid=' + thoid, 'newpost', 'top=50,left=50,width=600,height=500,resizable=yes,scrollbars=yes');
}
function removethought(thoid, blogoid) {
	window.open('gateway/unpublish.aspx?oid=' + blogoid + '&assetoid=' + thoid, 'newpost', 'top=50,left=50,width=400,height=150,resizable=yes,scrollbars=yes');
}
function newGWBlogEntry(blogoid) {
    window.open('gateway/newblogentry.aspx?blogoid=' + blogoid, 'newpost', 'top=50,left=50,width=600,height=500,resizable=yes,scrollbars=yes');
}
function editGWBlogEntry(assetoid) {
    window.open('gateway/newblogentry.aspx?oid=' + assetoid, 'newpost', 'top=50,left=50,width=600,height=500,resizable=yes,scrollbars=yes');
}
/* End blog */

/* Video */
function startVideo(url, contentType, w, h, target) {
    var el = document.getElementById(target);    
    if (el != null) {    
        var plugins = Plugin.getPluginsForMimeType(contentType);
        var plugin = null;

        //Favour Windows Media plugin
        for (var i = 0 ; i < plugins.length ; i++) {
            if (plugins[i] == "Windows Media") {
                plugin = plugins[i];
            }
        }
        
        if (plugins.length > 0) {
            if (plugin == null) plugin = plugins[0];
            Plugin.embed(plugin, {width: w, height: h, src: url, autoplay: "true", controller: "true", scale: "Aspect", type: contentType, activeXType: contentType}, target);
        }else{
            alert("Sorry but you don't appear to have a plugin that can play " + contentType);
        }
    }
}
/* End video */


function innerGetAssetInfoContent(assetoid) {
    if (assetInfoHovering) {
        if (!assetinfofetched) {
            PebbleLearning.Pebblepad.AjaxService.getAssetInfo(assetoid, getAssetInfoSuccess, getAssetInfoFailure);
        }
    }
}

function getAssetInfoContent(assetoid) {
    assetInfoHovering = true;
    setTimeout('innerGetAssetInfoContent(' + assetoid + ');', hoverTimeout);
}

function getAssetInfoSuccess(response) {
    assetinfofetched = true;
    document.getElementById("assetinfo").innerHTML = response;
}

function getAssetInfoFailure(response) {
    alert("Couldn't fetch info: " + response);
}

function showToolTip(el) {
    if (el == "assetinfo") {
        setTimeout('innerShowToolTip("' + el + '");', hoverTimeout);
        assetInfoHovering = true;
    } else {
        innerShowToolTip(el);
    }
}

function innerShowToolTip(el) {
    if (el == "assetinfo") {
        if (!assetInfoHovering) {
            return;
        }
    }
    var element = document.getElementById(el);
    element.style.display = "inline";
}

function hideToolTip(el) {
    if (el == "assetinfo") {
        assetInfoHovering = false;
    }
    var element = document.getElementById(el);
    element.style.display = "none";
}