﻿// Class Country
// This class encapsulates data about language versions, or sites where GrECo Internet is available
// It is used for rendering the dropdown menu where you choose the language
function Country(name, url, flag_id, pagesList, title, close, lang){
	// Name of the country you see next to the flag
	this.name = name;
	// URL of the welcome page of the language version
	this.url = url;
	// Name of the img file that contains the flag image
	this.flag_id = flag_id;
	// Name of the sharepoint document library where all pages are stored. Different languages have different names of the Pages doc. lib.
	// But this is obsolete because currently, all sites are in English
	this.pagesList = pagesList;
	// Similar as pagesList, title defines how the title column of the Pages document library is named in different languages.
	// It is also obsolete, besause all sites are now in English
	this.title = title;
 	// close defines the text of the inside the Close button in different languages.
	// This is useful in translating the text for various editors
	this.close = close;
	// This text was displayed next to the flag of the current site.
	// It is obsolete because te text eas removed from the page and only the flag is displayed.
	this.choose = lang;
}

// An array of items of type Country
var states = [];
states[0] = new Country('България',				'/bg/','flag_bg',	'Pages', 'Title', 'Close', 		'Language');
states[1] = new Country('Česká Republika', 		'/cs/', 'flag_CZ', 	'Pages', 'Title', 'Zavřít', 	'Jazyk');
states[2] = new Country('English',				'/en/','flag_En',	'Pages', 'Title', 'Close', 		'Language');
states[3] = new Country('Hrvatska',				'/hr/','flag_hr',	'Pages', 'Title', 'Zatvoriti', 	'Jezik');
states[4] = new Country('Казахстан',			'/kz/','flag_kz',	'Pages', 'Title', 'Close', 		'Language');
states[5] = new Country('Lietuva',				'/lt/','flag_lt',	'Pages', 'Title', 'Close', 		'Language');
states[6] = new Country('Magyarország',			'/hu/','flag_hu',	'Pages', 'Title', 'Közeli',		'Nyelv');
states[7] = new Country('Österreich', 			'/at/', 'flag_de', 	'Pages', 'Title', 'Schliessen', 'Sprache');
states[8] = new Country('Polska',				'/pl/',	'flag_PL',	'Pages', 'Title', 'Blisko',		'Language');
states[9] = new Country('România',				'/ro/','flag_ro',	'Pages', 'Title', 'Close', 		'Language');
states[10] = new Country('Россия',				'/ru/','flag_ru',	'Pages', 'Title', 'близко', 	'язык');

states[11] = new Country('Slovenija',			'/si/','flag_si',	'Pages', 'Title', 'Close', 		'Language');
states[12] = new Country('Slovenská Republika',	'/sk/','flag_SK',	'Pages', 'Title', 'Close', 		'Language');
states[13] = new Country('Srbija',				'/rs/','flag_rs',	'Pages', 'Title', 'Blizu', 		'Jezik');
states[14] = new Country('Україна',				'/ua/','flag_ua',	'Pages', 'Title', 'Close', 		'Language');
states[15] = new Country('Узбекистан',			'/uz/','flag_uz',	'Pages', 'Title', 'Close', 		'Language');

// This ensures that the title of the Pages document library is 'Pages' and that the title column of the library is 'Title'
// Not really necessary if all countries have these fields created correctly
for(i=0; i<states.length; i++){states[i].pagesList = "Pages"; states[i].title = "Title";} 

// Defines what the name of the site (language version) is in the URL with '/' and '/'. For example, it can be /at/, /en/  		  
var lang = "";	
// The same as lang, only without '/' and '/'. For example, it can be at, de, cs.
var only_lang = "";
// URL of the current page, but without the name of the page, for example 'http://d207vs1/de/Seiten/'
var urlStart = "";
// URL of the site (language version), for example http://d207vs1/de/
var urlSite = "";
// URl of the site collection, for example 'http://d207vs1/'
var urlBase = ""
//
var fullName = "";
var fullCollPath="";
var name = "";
// Title of the current page that can be seen in the browser. It is retieved with document.title
var pageTitle = "";
// Index that defines the Country instance in the states array. It can be thought of as the current language
var langNumber = 100;
// 
var flagSrc = "";
// Name of the browser the user uses, for exmaple it can be 'IE', 'Netscape' etc.
var browser = "";
// Name of the current user
var user="";
// Defines whether the dropdown memu where the user chooses the language is displayed or not
var menu_active = false;
// 
var fullUrl = "", sPath = "", urlStart = "", sPage = "", pageName = "";
// Defines items from the 'Navigation' list that will be visible when the editor creates a new button (in the AllButtons list).
// All other wil be invisible
var goodNav = ['Services', 'Karriere', '(None)', 'Career', 'News'];
// Defines parts in the URL that are cut from the URL when the user is on a Country page (a page that displayes a map of europe).
var orezatUrl = ['?', '/', '/Countries/'];

// This line uses jQuery to start this block of code as soon as the window is loaded
$(document).ready(function(){
	// Call the function that will take care fo all the steps that need to be done, like get teh language of the page, 
	//  creating the dropdown menu where you choose language, hiding some web part settings etc.
	getLanguage();
});

// Obsolee fucntion, it is not used anywhere
function getfullCollPath(){	
	var strfullUrl = unescape(window.location.toString());
	var strPath = unescape(window.location.pathname);
	var strcollection = strPath.substring(0,strPath.indexOf('/',1) + 1)
	return strfullUrl.substring(0,strfullUrl.indexOf(strcollection)) + strcollection;
}

// Obsolee fucntion, it is not used anywhere
function getServerPath(){	
	var strfullUrl = unescape(window.location.toString());
	var strPath = unescape(window.location.pathname);
	return strfullUrl.substring(0,strfullUrl.indexOf(strPath));
}

// The main function that will take care fo all the steps that need to be done, like get teh language of the page, 
//  creating the dropdown menu where you choose language, hiding some web part settings etc.
function getLanguage(){	
	// Get the HTML element that contains the countries menu
	var Lmenu = document.getElementById("FLAGS");
	// Hide it, because we don't want it to be visible when the page loads.
	Lmenu.style.display = "none";
	// Get the current URL with all parameters and everything. For example 'http://greco.eu/sk/Pages/Countries/Slovakia.aspx?Office=default'
	fullUrl = unescape(window.location.toString());
	// Get the current URL without parameters and the sitecollection name. For example '/sk/Pages/Countries/Slovakia.aspx'
	sPath = unescape(window.location.pathname);
	//alert("sPath: " + sPath + " fullUrl: " + fullUrl);
	// Get the URL of the current site For example /sk/, /en/
	var collection = sPath.substring(0,sPath.indexOf('/',1) + 1)
	// URL of the current site. For example 'http://greco.eu/sk/'
	fullCollPath = fullUrl.substring(0,fullUrl.indexOf(collection)) + collection;
	// name of the current site with the name of th ePages document library. For example, '/sk/Pages/'
	urlStart = sPath;
	urlStart = orezUrl(sPath);
	// Name of the current page. For example 'Welcome.aspx'
	sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	//alert("sPath: " + sPath + "\ncolelction: " + collection + "\nurlStart: " + urlStart + "\nfullCollPath: " + fullCollPath + "\nsPage: " + sPage);
	//pageName = sPage.substring(0, sPage.indexOf('.'));
	// Convert the URL to lowercase
	urlLowCase = fullUrl.toLowerCase();
	// Position of the site name part (/en/, /at/) of the URL
	var pos = -1;
	// Find out what browser the user uses
	getBrowser();
	
	// Loop through the arry of the array of Country instances
	for(i=0; i<states.length; i++){
		// Get the position of the site name part (/en/, /at/) of the URL
		pos = urlLowCase.indexOf(states[i].url);
		// If the position was found
		if(pos != -1){
			// Set the langNumber to the current position in the Country array so that we know which Country item to use
			langNumber = i;
			// Get the nam of the site with '/' (like /at/)
			lang = states[i].url;
			// Get the name of the imag with the flag
			flagSrc = states[i].flag_id;
			// 
			only_lang = states[i].url.substring(1, states[i].url.lengh-1);//supportedLangs[i].length-1);
			// Some pages might have their name in the url set to '/Pages - name.aspx' so the 'Pages - ' part has to be removed from the page name.
			// It's just a precautionary mesure, because no current pages behave this way
			if(document.title.substring(0, states[i].pagesList.length) == states[i].pagesList)						
				pageTitle = document.title.substring(states[i].pagesList.length + 3);
			else
				pageTitle = document.title;
			
			//alert(document.title + "  Page Title: " + pageTitle);
			urlBase = urlLowCase.substring(0, pos);
			urlSite = urlLowCase.substring(0, pos) + lang;
			urlStart = urlLowCase.substring(0, pos) + lang + states[i].pagesList + '/';			
			//alert("Your lang is: " + supportedLangs[i] + " at position " + pos + " \n Your URL starts with " + urlStart 
			//			+ " \n Your Site UL is " + urlSite + "\nYour image is: " + flagSrc);
						
	        break;
		}
	}	
	//getUser();
	// Call a function that will hide all webpart properties except the custom ones (for example Translation in a lot of pages)
	hideWebpartProperties();	
	hideInfo();
	// Call a function that will write correct data into the dropdown menu where the user chooses the language version
	write_countries(langNumber);
	// Hides a webpart with data for indexers (Twin)
	hideTwinWebParts();
	
	// createContent is true for pages like Services, Portrait, News, Career and Contact
	// If it is defined, a method called createPageContent is called that will take care of the content creation.
	// Other pages do not have this variable
	if(typeof createContent === 'undefined'){}
	else{
		createPageContent();
	}
	// createContent is true for some create and edit forms in a list (Contacts, Documents for example)
	// If it is defined, a method called changeListForm is called that will for example hide or disable some of the fields,
	//  so that the editor has his task easier
	if(typeof listFormScript === 'undefined'){}
	else{
		changeListForm();
	}
	
	/*
    * NoSpamEmail plugin
    * Copyright (c) 2009 AlejTech
    * Licensed under the LGPL License
    */
    // This function hides emails from spambots.
    // It looks for span elements that hane email class and reformats them so that they are harder to find by spambots but they look the same
    (function($) {
        $.fn.nospamemail = function(options) {
            return this.each(function() {
                var _email = $('span', this).text();
                $(this).empty().append('<a class="tab" href="mailto:' + _email + '">' + _email + '</a>');
            });
        }
    })(jQuery);	
	
	// Call the function that hides emails from spambots on all span elements that have the 'email' class
	$(function() {
	    $('span.email').nospamemail();
    })
}

// This function writes correct data into the dropdown menu where the user chooses the language.
function write_countries(language){
	// If the current country doesn't have a flag, insert a transparent flag.
	if(flagSrc.length == 0 )
		flagSrc = "blankFlag"; // blankFlag.gif is a transparent image
	// Look for the img element where the flag is and set its source
	document.getElementById("lang_image2").src = "/PublishingImages/" + flagSrc + ".gif";
	// Get the dropdown menu
    var chooser = document.getElementById("choose");
    // Get teh indes of the first closing tag
    //var index = chooser.innerHTML.indexOf('>');
    // Get all elements from the dropdown menu ('li' elements)
    var zoznam = $("ul#FLAGS li");
    // Loop through the list of li elements from the dropdown menu
    for(i=0; i<zoznam.length; i++){
      	// get the anchor tag inside the'li' element
      	var item = $("A", zoznam[i])[0];
      	// Find its closing '>' tag
      	var index = item.innerHTML.indexOf('>');
      	// Define the item's target location (it will be a welcome page of the country)
      	item.href = urlBase + states[i].url + states[i].pagesList;
      	// Write the country name into the link
      	item.innerHTML = item.innerHTML.substring(0,index+1) + states[i].name;
    }
}

// Get the browser of the user
function getBrowser(){
	browser = navigator.appName;
}

// This function gets the name of the current user. It does not work nor is it used anywhere
function getUser(){
	//debugger;
	try{
		var zz9 = $("a#zz9_Menu")[0];
		if(browser == "Microsoft Internet Explorer")
			user = zz9.innerText;
		else
			user = zz9.text;
		user = user.substring(user.indexOf("\\")+1, user.length);
		alert(user);
	}catch(err){}
}

// This function hides all webpart properties except the custom ones (for example Translation in a lot of pages)
function hideWebpartProperties(){
	//if(user != "Pavlikk" && user != "Ilcik" && user != "Hesteric" && user != "Fabo"){
		//alert("Skryvam");
		// If the user has Explore, do it this way
		if(browser == "Microsoft Internet Explorer"){
			// Get the default webpart properties (luckily ully, custom webpart properties are rendered a bit differently)
			var unwantedDivs = $("div.ms-ToolPaneBody div.ms-TPBody");
			// If there are any of those unwanted webpart properties, hide them
			if(unwantedDivs.length > 0){
				unwantedDivs[0].style.display = "none";
			}
		}
		// If the user has another browser, do it in another way
		else{
			// Get the default webpart properties (luckily, custom webpart properties are rendered a bit differently)
			var unwantedDivs = $("div.ms-ToolPaneBody div.ms-TPBody");
			// If there are any of those unwanted webpart properties, hide them			
			if(unwantedDivs.length > 0 && unwantedDivs[0].childNodes.length > 2){
				unwantedDivs[0].style.display = "none";
			}
		}
	//}
}

// This function hides the 'Created by' and 'Created at' info on list forms
function hideInfo(){
	// The text is always in a td element with the class 'ms-descriptiontext'. Find them all
	var unwantedTDs = $("td.ms-descriptiontext");
	// Loop through the found elements and hide them
	for(i=0; i<unwantedTDs.length; i++){
		unwantedTDs[i].style.display = "none";
	}
	//alert(unwantedTDs.length);
}

// This function executes when the country flag on the blue top bar is clicked. It displays or hides the dropdown menu where the user chooses the language
// Since the dropdown menu shows or hides on mouseover, it is not needed
function menu_click(){
	if(menu_active)
		menu_active = false;
	else
		menu_active = true;
	var menuu = document.getElementById("FLAGS");
	if(menu_active){
		menuu.style.display = "block";
	}
	else{
		menuu.style.display = "none";
	}
}

function show_menu(){
	menu = document.getElementById("menu");
	if(menu_active){
		menu.style.display = "block";
	}
	else{
		menu.style.display = "none";
	}
}

// Executes when the mouse enteres the country flag (on the blue top bar)
function menu_over(){
	// The HTML element with id 'FLAGS' contains the dropdown menu where the user chooses the language
	var menu2 = document.getElementById("FLAGS");
	// Show the dropdown menu
	menu2.style.display = "block";
}

// Executes when the mouse passes out of the country flag (on the blue top bar)
function menu_out(){
	// The HTML element with id 'FLAGS' contains the dropdown menu where the user chooses the language
	var menu2 = document.getElementById("FLAGS");
	// Hide the dropdown menu
	menu2.style.display = "none";
}

// This function removes options from dropdown menu in EditForms and WewForms of several lists (for example 'News', 'Office Contacts').
// It limits the number of pages the user can choose from to pages of certain type.
// This function executes when the user edits a field that references the 'Pages' document library (for example 'News', 'Office Contacts').
// This can be done only in pages whose javascript implements this class, like Services, Portrait, News, Contact etc.
function removeOptions(){
	//alert("ADADF");
	// Get the number of options in the select element
	var num = options.length;
	// Loop through the options
	for(i=1; i<num; i++){
		// toDelete indicates ehther the current option will be removed from the select element
		var toDelete = true;
		// Loop through the list of Page instances.
		// This can be done only in pages whose javascript implements this class, like Services, Portrait, News, Contact etc.
		for(j=0; j<pages.length; j++){
			// If the title of the page matches the text in thje option, set toDeleet to false, so that this option won't be deleted
			if(pages[j].title == options[i].text){
				toDelete = false;
			}
		}
		// If toDelete was set to true, remove the option from select element
		if(toDelete){
			// The option is retrieved by its current index, because as the options are removed, their index change
			var ind = options[i].index;
			selectPage.remove(ind);
		}
	}
}

// This function writes into the upper navigation (the orange bar on most of the pages) on the 'NewsDisplay.aspx' page.
// NewsDisplay needs to be treated differently then any other page. It has no buttons and its role is to display one news message.
function Top2NewsDisplay(){
	// Get the element that displays the title of the news article (not in the top orange bar)
	var titleElm = document.getElementById("news_title");
	// 
	var newstitle = getInnerText(titleElm);
	writeInnerText(document.getElementById("level1"), translPageType);
	writeInnerText(document.getElementById("level2"), " > " + newstitle);
	writeInnerText(document.getElementById("level3"), "");
}

// This function removes several string (like '?', '/', '/Countries) from the url and returns the result.
function orezUrl(url){
	var pom = url;
	for(i=0; i<orezatUrl.length; i++){
		index = pom.lastIndexOf(orezatUrl[i]);
		if(index > 0){
			pom = pom.substring(0, index+1);	
		}
	}
	return pom;
}

function createPageslink(url){
	location.href = fullCollPath + 'Pages/' + url;
	return false;
}

// Hides a webpart with data for indexers (Twin)
function hideTwinWebParts(){
	if(typeof(window['cuid']) != "undefined"){
		if(cuid != null){	
			var twinWebPart = $('td#MSOZoneCell_WebPartWPQ2')[0];
			if(twinWebPart != null)
				twinWebPart.style.display = 'none';
		}
	}
}

// Writes text inside a HTML element
function writeInnerHTML(obj, text){
   	obj.innerHTML += text;
}

