// from unradio.js

// returns bool if cookie for name exists
function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i,j) == arg) {
			return true;
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return false;
}

// returns string value of cookie by name
function getCookieReturnVal(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i,j) == arg) {
			return getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return false;
}

function createNavBar(lang, account_lbl, logoff_lbl, login_lbl, register_lbl) {

	var returnText = "";	

	var separator = "|";

	if (lang == "zh") {
		separator = "";
	}

	if (lang == "ar") {

		if (getCookie('s')) {
			returnText = "<li class=\"group2\"> " + separator + " <a class=\"body\" href=\"/accounts/index.html?app=1\">" + account_lbl + "</a> " + separator + " <a class=\"body\" href=\"/accounts/logout.html\">" + logoff_lbl + "</a> </li>";
		} else {
			returnText = "<li class=\"group2\"> " + separator + " <a class=\"body\" href=\"/accounts/login.html?app=1\">" + login_lbl + "</a> " + separator + " <a class=\"body\" href=\"/accounts/register.html\">" + register_lbl + "</a> </li>";
		}
	
	} else {

		if (getCookie('s')) {
			returnText = "<li class=\"group2\"><a class=\"body\" href=\"/accounts/index.html?app=1\">" + account_lbl + "</a> " + separator + " <a class=\"body\" href=\"/accounts/logout.html\">" + logoff_lbl + "</a> " + separator + " </li>";
		} else {
			returnText = "<li class=\"group2\"><a class=\"body\" href=\"/accounts/login.html?app=1\">" + login_lbl + "</a> " + separator + " <a class=\"body\" href=\"/accounts/register.html\">" + register_lbl + "</a> " + separator + " </li>";
		}

	}	

	return returnText;

}

function createNavBarWoLi(lang, account_lbl, logoff_lbl, login_lbl, register_lbl) {

        var returnText = "";

        var separator = "|";

        if (lang == "zh") {
                separator = "";
        }

        if (lang == "ar") {

                if (getCookie('s')) {
                        returnText = separator + " <a class=\"body\" href=\"/accounts/index.html?app=1&lang=en\">" + account_lbl + "</a> " + separator + " <a class=\"body\" href=\"/accounts/logout.html?app=1&lang=en\">" + logoff_lbl + "</a>";
                } else {
                        returnText = separator + " <a class=\"body\" href=\"/accounts/login.html?app=1&lang=en\">" + login_lbl + "</a> " + separator + " <a class=\"body\" href=\"/accounts/register.html?app=1&lang=en\">" + register_lbl + "</a>";
                }

        } else {

                if (getCookie('s')) {
                        returnText = "<a class=\"body\" href=\"/accounts/index.html?app=1&lang=en\">" + account_lbl + "</a> " + separator + " <a class=\"body\" href=\"/accounts/logout.html?app=1&lang=en\">" + logoff_lbl + "</a> " + separator ;
                } else {
                        returnText = "<a class=\"body\" href=\"/accounts/login.html?app=1&lang=en\">" + login_lbl + "</a> " + separator + " <a class=\"body\" href=\"/accounts/register.html?app=1&lang=en\">" + register_lbl + "</a> " + separator ;
                }

        }

        return returnText;

}



function createDownloadLink(lang, filename, download_lbl) {

	var returnText = "";	

	if (getCookie('s')) {

		// returnText = "<a class=\"content\" href=\"http://downloads.unmultimedia.org/photo/ltd/high/" + dir + "/" + filename + ".jpg?s=" + getCookieReturnVal('s') + "&save\">High-resolution image</a>";

		if (lang == "ru") {
			returnText = "<a href=\"" + filename + "&s=" + getCookieReturnVal('s') + "\"><img class=\"toolbox_image\" src=\"http://downloads.unmultimedia.org/radio/GUI/BTN/btn_DownloadSave.gif\" alt=\"\"/></a><a href=\"" + filename + "&s=" + getCookieReturnVal('s') + "\">" + download_lbl + "</a>";
		} else {
                        returnText = "<a href=\"" + filename + "\">" + download_lbl + "</a><a href=\"" + filename + "\"><img class=\"toolbox_image\" src=\"http://downloads.unmultimedia.org/radio/GUI/BTN/btn_DownloadSave.gif\" alt=\"\"/></a>";
		}

	} else {

                if (lang == "ru") {
			returnText = "<a href=\"/accounts/login.html\"><img class=\"toolbox_image\" src=\"http://downloads.unmultimedia.org/radio/GUI/BTN/btn_DownloadSave.gif\" alt=\"\"/></a><a href=\"/accounts/login.html\">" + download_lbl + "</a>\n";
		} else {
			returnText = "<a href=\"/accounts/login.html\">" + download_lbl + "</a><a href=\"/accounts/login.html\"><img class=\"toolbox_image\" src=\"http://downloads.unmultimedia.org/radio/GUI/BTN/btn_DownloadSave.gif\" alt=\"\"/></a>\n";
		}

	}

	return returnText;

}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function toggleSnToolbox() {
	var sn = document.getElementById('sn_toolbox');

	if (sn.className == "sn") {
		sn.className = "sn_hidden";
	} else {
		sn.className = "sn";
	}
}

function de_click() {
	u=location.href;
	t=document.title;
	window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=' + encodeURIComponent(u) + '&amp;title=' + encodeURIComponent(t), 'delicious','toolbar=no,width=550,height=550');
	return false;
}

function de_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=' + encodeURIComponent(u) + '&amp;title=' + encodeURIComponent(t), 'delicious','toolbar=no,width=550,height=550');
        return false;
}

function di_click() {
	u=location.href;
	t=document.title;
	window.open('http://digg.com/remote-submit?phase=2&url=' + encodeURIComponent(u) + '&title=' + encodeURIComponent(t), 'digg', 'toolbar=no,width=650,height=550');
	return false;
}

function di_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('http://digg.com/remote-submit?phase=2&url=' + encodeURIComponent(u) + '&title=' + encodeURIComponent(t), 'digg', 'toolbar=no,width=650,height=550');
        return false;
}


function fb_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function fb_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
        return false;
}


function yb_click() {
	u=location.href;
	t=document.title;
	window.open('http://buzz.yahoo.com/buzz?guid=' + encodeURIComponent(u) + '&headline=' + encodeURIComponent(t), 'yahoobuzz', 'toolbar=no,width=790,height=600');
	return false;
}

function yb_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('http://buzz.yahoo.com/buzz?guid=' + encodeURIComponent(u) + '&headline=' + encodeURIComponent(t), 'yahoobuzz', 'toolbar=no,width=790,height=600');
        return false;
}

function me_click() {
        u=location.href;
        t=document.title;
        window.open('http://meneame.net/submit.php?url=' + encodeURIComponent(u), 'meneame','toolbar=no,width=790,height=550');
        return false;	
}

function me_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('http://meneame.net/submit.php?url=' + encodeURIComponent(u), 'meneame','toolbar=no,width=790,height=550');
        return false;
}

function so_click() {
        u=location.href;
        t=document.title;
        window.open('http://www.sonico.com/share.php?url=' + encodeURIComponent(u), 'sonico','toolbar=no,width=650,height=550');
        return false;
}

function so_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('http://www.sonico.com/share.php?url=' + encodeURIComponent(u), 'sonico','toolbar=no,width=650,height=550');
        return false;
}

function tw_click() {
        u=location.href;
        t=document.title;
        window.open('http://twitter.com/home?status=' + encodeURIComponent(u), 'twitter','toolbar=no,width=750,height=550');
        return false;
}

function tw_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('http://twitter.com/home?status=' + encodeURIComponent(u), 'twitter','toolbar=no,width=750,height=550');
        return false;
}

function go_click() {
        u=location.href;
        t=document.title;
	window.open('http://www.google.com/bookmarks/mark?op=add&hl=pt&title=' + encodeURIComponent(t) + '&bkmk=' + encodeURIComponent(u));
        return false;
}

function go_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('http://www.google.com/bookmarks/mark?op=add&hl=pt&title=' + encodeURIComponent(t) + '&bkmk=' + encodeURIComponent(u));
        return false;
}


function my_click() {
        u=location.href;
        t=document.title;
        window.open('http://www.myspace.com/Modules/PostTo/Pages/?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t));
	return false;
}

function my_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('http://www.myspace.com/Modules/PostTo/Pages/?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t));
        return false;
}

function wi_click() {
        u=location.href;
        t=document.title;
        window.open('https://favorites.live.com/quickadd.aspx?marklet=1&mkt=pt-br&url=' + encodeURIComponent(u) + '&title=' +encodeURIComponent(t) );
        return false;
}

function wi_click_permalink(permalink) {
        u=permalink;
        t=document.title;
        window.open('https://favorites.live.com/quickadd.aspx?marklet=1&mkt=pt-br&url=' + encodeURIComponent(u) + '&title=' +encodeURIComponent(t) );
        return false;
}

function ru_me_click() {
        u=location.href;
        t=document.title;
 		window.open('http://memori.ru/link/?sm=1&u_data[url]=' + encodeURIComponent(u) + '&u_data[name]=' +encodeURIComponent(t) );
 	return false;
}

function ru_ya_click() {
        u=location.href;
        t=document.title;
        window.open('http://zakladki.yandex.ru/userarea/links/addfromfav.asp?bAddLink_x=1&lurl=' + encodeURIComponent(u) + '&lname='+encodeURIComponent(t) );
	return false;
}

function ru_lj_click() {
        u=location.href;
        t=document.title;
        window.open('http://www.livejournal.com/update.bml?usescheme=nonavigation');
	return false;
}

// from unphoto.js

function createDownloadLink(dir, filename) {

	var returnText = "";	

	if (getCookie('s')) {
		returnText = "<a class=\"download_link\" href=\"http://downloads.unmultimedia.org/photo/ltd/high/" + dir + "/" + filename + ".jpg?s=" + getCookieReturnVal('s') + "&save\"><img src=\"http://www.unmultimedia.org/GUI/btn/btn_UNPH_v2.0_download.gif\" alt=\"Login for Download\"></a>";
		returnText += "<a class=\"download_link\" href=\"http://downloads.unmultimedia.org/photo/ltd/high/" + dir + "/" + filename + ".jpg?s=" + getCookieReturnVal('s') + "&save\">Download Hi-Res Image</a>";
	} else {
		returnText = "<img src=\"http://www.unmultimedia.org/GUI/btn/btn_UNPH_v2.0_download.gif\" alt=\"Login for Download\"></a>";
		returnText += "<a class=\"login_link\" href=\"/accounts/login.html\">Login for Download</a>\n";
	}
	//alert(returnText);
	return returnText;

}

function toggleNavBarSublist(id) {

	var menuItem = document.getElementById(id);

	if (menuItem.className == "photo_bottom_nav_bar_column_sublist") {
		menuItem.className = "photo_bottom_nav_bar_column_sublist_hidden";
	} else {
		menuItem.className = "photo_bottom_nav_bar_column_sublist";
	}
}


