/**
* @version		$Id: caption.js,v 1.25 2009/04/24 16:22:05 huvarga Exp $
* @copyright	Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (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;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (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;
    }

}
/**
 * JCaption javascript behavior
 *
 * Used for displaying image captions
 *
 * @author		Johan Janssens <johan.janssens@joomla.org>
 * @package		Joomla
 * @since		1.5
 * @version     1.0

var JCaption = new Class({
	initialize: function(selector)
	{
		this.selector = selector;
		if(!document.getElementById("lead"))
		{
		var images = $$(selector);
		images.each(function(image){ this.createCaption(image); }, this);
		}
	},

	createCaption: function(element)
	{
		var caption   = document.createTextNode(element.title);
		var container = document.createElement("div");
		var container2 = document.createElement("div");
		//var text      = document.createElement("p");
		var width     = element.getAttribute("width");
		var align     = element.getAttribute("align");
		var br = document.createElement("br");

		if(!width) {
			width = element.width;
		}

		//text.appendChild(caption);
		element.parentNode.insertBefore(container, element);
		container2.appendChild(element);
		container.appendChild(container2);
		container.appendChild(br);
		if ( element.title != "" ) {
			container.appendChild(caption);
		}
		container.className   = this.selector.replace('.', '_');
		container.className   = container.className + " " + align;
		container.setAttribute("style","float:"+align);
		container.style.width = width + "px";

	}
});

document.caption = null
window.addEvent('load', function() {
  var caption = new JCaption('img.caption')
  document.caption = caption
});
 */
 
 var showfolders = new Class({
	initialize: function()
	{
		
		
		var links = document.getElementsByTagName('a');
		for(i=0; i< links.length; i++)
		{
			if(links[i].className == "contentpagetitle" ) {this.createFoldericons(links[i], "article");}
			//if(links[i].className == "jobadlink") {this.createFoldericons(links[i], "job");}
			if(links[i].href.indexOf("allas.monster.hu/getjob.aspx") !=-1) { 
				this.createFoldericons(links[i], "job");
			}
		}
		
		
		
		
		
	},
	
	
	createFoldericons: function(thelink, type)
	{
		
			var img = document.createElement("img");
			var thetitle = this.trim(thelink.innerHTML);
			img.src= "http://rhea.jobpilot.hu/palyacsucs/templates/jobpilot/img/to_jobfolder.gif";
			img.className = "to_folder_icon";
			img.title = "Elmentés a jobMappába"
			if(checkifExists(thelink))
			{
				img.style.display = "none"
			}
			img.id = thelink;
			var toFolderLink = document.createElement("div");
			img.onclick=function() {to_folder(thelink, thetitle, type)};
			
			thelink.parentNode.insertBefore(img,thelink);

		
	},
	
	trim: function(str, chars) {
	    return this.ltrim(this.rtrim(str, chars), chars);
	},
	
	ltrim: function(str, chars) {
	    chars = chars || "\\s";
	    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	},
	
	rtrim: function(str, chars) {
	    chars = chars || "\\s";
	    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}
	

});



function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


addLoadEvent(function() {

	var cssStr = "#jobfolder {text-align: right;margin-top: 5px;} #jobfolder_content{border: solid 1px #990000;background-color: #EEEEEE;	position: absolute; left: -120px;	width: 250px;	max-height: 300px;	z-index: 99;	filter:alpha(opacity=90);	-moz-opacity:0.9;	-khtml-opacity: 0.9;	opacity: 0.9;	text-align: left;}#jobfolder_content .title{	background-color: #DDDDDD; margin-top: 0px;	font-weight: bold;	font-size: 11pt;	padding: 3px 3px 3px 5px;}#jobfolder_content .lister{border-bottom: solid 1px #990000;padding: 0px;	color: #990000;	font-weight: bold;	font-size: 10pt;	margin:  0px;	text-align: left;	height: 100px; overflow-x: hidden; overflow-y: scroll;} .linka, .linkb { width: 190px; float: left; } .icona, .iconb { width: 20px; float: right; } .rowa, .rowb{ clear: both; padding-top: 1px; padding-bottom: 5px; padding-left: 5px; padding-right: 10px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #D8D8D8; width: 220px; float: left; } .rowa, .icona, .linka{   background-color: #E6E6E6; } .rowb, .iconb, .linkb{  background-color: #E1E1E1; } ";
	var style = document.createElement("style");
	style.setAttribute("type", "text/css");
	if(style.styleSheet){// IE
	style.styleSheet.cssText = cssStr;
	} else {// w3c
	var cssText = document.createTextNode(cssStr);
	style.appendChild(cssText);
	}

	document.getElementsByTagName("head")[0].appendChild(style);

	var folder = document.getElementById("jobfolder");
	var folderPanel = document.getElementById("jobfolder_content");


	   var caption = new showfolders()
	    document.caption = caption;
	    
	    
	   var folderPanel = document.getElementById("jobfolder_content");
	   var folder= document.getElementById("jobfolder");
	   positions = findPos(folder)

	   
	  initializeFolder();
  
});


folderContent = new Array(); 
loadFolder();

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}


function initializeFolder()
{
	deleteFolderItem("undefined");
	var articles = new Array();
	var jobs = new Array();
	var folderPanel = document.getElementById("jobfolder_content");

	folderPanel.innerHTML = "";
	
	for(i=0; i < folderContent.length; i++)
	{	
		switch(folderContent[i][2])
		{
	
			case "article": articles[articles.length] = new Array(folderContent[i][0],folderContent[i][1])
									break;
			case "job": 	jobs[jobs.length] = new Array(folderContent[i][0],folderContent[i][1])
									break;
		}
	}	
	
	var icon = document.getElementById("jobfolder_img");
	
	if(folderContent.length > 0)
	{
		icon.src = "http://rhea.jobpilot.hu/palyacsucs/modules/mod_jobfolder/img/jobfolder_open.gif";
		var intro = document.getElementById("jobfolder_intro");
  		if(intro) {folderPanel.removeChild(intro);}
	}
	else
	{
		icon.src = "http://rhea.jobpilot.hu/palyacsucs/modules/mod_jobfolder/img/jobfolder_closed.gif";
		var intro = document.createElement("div");
		intro.className = "lister";
		intro.style.marginTop = "15px";
		intro.style.textAlign = "center";
		intro.id = "jobfolder_intro";
		intro.innerHTML = 'A jobMappa az Ön által érdekesnek talált cikkek és állások gyűjtésére szolgál.<br>Kattintson a link mellett található <img class="to_folder_icon" src="http://rhea.jobpilot.hu/palyacsucs/templates/jobpilot/img/from_jobfolder.gif"> iconra a tároláshoz!';
		folderPanel.appendChild(intro);
		folderPanel.style.display= "none";
	}
	
	
	if(articles.length > 0)
	{
		var aTitle = document.createElement("div");
		aTitle.className = "title";
		aTitle.innerHTML = "Cikkek:"
		var aLinkDiv = document.createElement("div");
		aLinkDiv.className = "lister";
		
		
		for(i=0; i < articles.length; i++)
		{
			if(i % 2)
			{
				createListerRow( articles[i][0], articles[i][1], aLinkDiv, "b");
			}
			else
			{
				createListerRow( articles[i][0], articles[i][1], aLinkDiv, "a");
			}
		}
	
		
		folderPanel.appendChild(aTitle);
		folderPanel.appendChild(aLinkDiv);
	}
	
	if(jobs.length > 0)
	{
		var jTitle = document.createElement("div");
		jTitle.className = "title";
		jTitle.innerHTML = "Állások:"
		var jLinkDiv = document.createElement("div");
		jLinkDiv.className = "lister";
		
		
		for(i=0; i < jobs.length; i++)
		{
			
			if(i % 2)
			{
				createListerRow( jobs[i][0], jobs[i][1], jLinkDiv, "b");
			}
			else
			{
				createListerRow( jobs[i][0], jobs[i][1], jLinkDiv, "a");
			}
		}
		folderPanel.appendChild(jTitle);
		folderPanel.appendChild(jLinkDiv);
	}
	
		var closebutton = document.createElement("div");
		closebutton.style.textAlign = "right";
		closebutton.innerHTML = 'Bezár [x]';
		closebutton.style.paddingRight = "5px";
		closebutton.style.fontWeight = "bold";
		closebutton.style.cursor="pointer";
		closebutton.onclick=function() {opencloseFolder()};
		folderPanel.appendChild(closebutton);
	
}

function createListerRow(url,title,lister, a_or_b)
{
			var Link = document.createElement("a");
			var linka = document.createElement("div");
			linka.className = "linka";
			var linkb = document.createElement("div");
			linkb.className = "linkb";
			var rowa = document.createElement("div");
			rowa.className = "rowa";
			var rowb = document.createElement("div");
			rowb.className = "rowb";
			var icona = document.createElement("div");
			icona.className = "icona";
			var iconb = document.createElement("div");
			iconb.className = "iconb";
			var img = document.createElement("img");
			img.src= "http://rhea.jobpilot.hu/palyacsucs/templates/jobpilot/img/from_jobfolder.gif";
			var deleteLink = document.createElement("a");
			deleteLink.href = "javascript:deleteFolderItem('" + url + "')";
			deleteLink.appendChild(img);
			
			Link.href = url;
			Link.innerHTML=title;
			Link.target = "_blank";
			if(a_or_b == "a")
			{
				linka.appendChild(Link);
				rowa.appendChild(linka);
				icona.appendChild(deleteLink);
				rowa.appendChild(icona);
				lister.appendChild(rowa);
			}
			else
			{
				linkb.appendChild(Link);
				rowb.appendChild(linkb);
				iconb.appendChild(deleteLink);
				rowb.appendChild(iconb);
				lister.appendChild(rowb);
			}
			
}

function deleteFolderItem(url)
{
	var temp = new Array();
	var changed = false;

	for(j=0; j < folderContent.length; j++)
	{
		if(folderContent[j][0] != url) { temp[temp.length] = new Array(folderContent[j][0],folderContent[j][1],folderContent[j][2]); }
		else { changed = true; }
	}
	folderContent = temp;
	if(changed)
	{
		if(document.getElementById(url))
		{
			document.getElementById(url).style.display = "inline";
		}
		saveFolder(false);
	}
}

function checkifExists(url)
{
	var exists = false;

	for(j=0; j < folderContent.length; j++)
	{
		if(folderContent[j][0] == url) { exists = true; }
	}
	return exists;
}

function saveFolder(isFirst)
{
	var tempCookie = "jobFolder=";
	var isnull = true;
	
	for(i=0; i < folderContent.length; i++)
	{
		if(folderContent[i][0] != "" && folderContent[i][0] != "undefined") { tempCookie+="|"+(folderContent[i][0])+"^"+(Url.encode(folderContent[i][1]))+"^"+(folderContent[i][2]); isnull = false;}
	}		
	
	
		var expire = new Date ();
		expire.setTime (expire.getTime() + (6 * 24 * 3600000)); 
		expire = expire.toGMTString();
		finalCookie = tempCookie+"; path=/; expires="+expire;  	
		document.cookie = finalCookie;
		var cookiesOn = false;
			if(isFirst)
			{
				checkForCookie = document.cookie.split(";");

					for (x=0; x < checkForCookie.length; x++)
						{
						if (checkForCookie[x].indexOf("jobFolder") >= 0) {cookiesOn = true;}
						}
					if (cookiesOn == false) alert("Engedélyezni kell a cookie-kat a jobMappa használatához!");
			}
	
	 initializeFolder();
	

}

function folderItem(url,title)
{
	this.url = url;
	this.title = title;
	
}

function loadFolder()
	{
  		folderString = null;
		tempArray = document.cookie.split(";");
	
			
		for (tA = 0; tA < tempArray.length; tA++)
		{
			if (tempArray[tA].indexOf('jobFolder=') > -1) //found the Collectors section
			{
				tPos = tempArray[tA].indexOf("=")+2;
				folderString = tempArray[tA].substring(tPos,tempArray[tA].length); //striping out "Collectors=^"

			}
		}
		
		
		if (folderString != null && folderString != "")
		{		
			tempArray = folderString.split("|");
			if (tempArray.length > 0)
			{
				current = new Array();
				
				for (i=0; i < tempArray.length; i++)
				{
		
					current = tempArray[i].split("^");
					folderContent[i]= new Array(current[0], Url.decode(current[1]), current[2]);
					
				}
			}
		}
		
	}

function to_folder(thelink, thetitle, type)
{
	isFirst = false;
	if(folderContent.length == 0)
	{
		isFirst = true;
	}
	folderContent[folderContent.length] = new Array(thelink, thetitle, type);
	
	saveFolder(isFirst);
	
	if(document.getElementById(thelink))
		{
			document.getElementById(thelink).style.display = "none";
	}
}

function opencloseFolder()
{
	
	var folder = document.getElementById("jobfolder_content");
	if(folder.style.display == "none")
	{
		folder.style.display = "block";
	}
	else
	{
		folder.style.display = "none";
	}
	
}
