function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* mail crypto */
function geo() {
	if (!document.getElementsByTagName) // Check for browser support
			return false;
	if (rot13) // Initiate ROT13 only if needed
		var map = rot13init(); 
	var tooltip_js_on = '';
	var tooltip_js_off = '';
	var links = document.getElementsByTagName('a'); // Get all anchors
	function geo_decode(anchor) { // function to recompose the orginal address
		var href = anchor.getAttribute('href');
		if (href != null) {
			var address = href.replace(/.*contact\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
			var linktext = anchor.innerHTML; // IE Fix
			if (href != address) {
				anchor.setAttribute('href','mailto:' + (rot13 ? str_rot13(address,map) : address)); // Add mailto link	
				anchor.innerHTML = linktext; // IE Fix
			}
		}
	}
	for (var l = 0 ; l < links.length ; l++) { // Loop through the anchors
		var anchor = links[l];
		var href = anchor.getAttribute('href');
		if (href != null) {
			var address = href.replace(/.*contact\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
			var linktext = anchor.innerHTML; // IE Fix
			if (href != address) {
				anchor.setAttribute('href','mailto:' + (rot13 ? str_rot13(address,map) : address)); // Add mailto link	
				anchor.innerHTML = address; // IE Fix
			}	

			links[l].onclick = function() { // Encode links when clicked
				geo_decode(this);
			}
			links[l].onmouseover = function() { // Display tooltip when links are hovered
				if (this.getAttribute('title') == tooltip_js_off) { // Set custom tooltip if specified
					this.setAttribute('title',tooltip_js_on);
					geo_decode(this); // Encode links when hovered (so that the address appears correctly in the browser's status bar)
				}
			}
		}
	}
}

var rot13 = 0;

function rot13init() {
	var map = new Array();
	var s = "abcdefghijklmnopqrstuvwxyz";
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i)] = s.charAt((i+13)%26);
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
	return map;
}

function str_rot13(a,map) {
	var s = "";
	for (var i = 0 ; i < a.length ; i++) {
		var b = a.charAt(i);
		s += (b>='A' && b<='Z' || b>='a' && b<='z' ? map[b] : b);
	}
	return s;
}


function controlValueOnFocus(control) {
	c = $(control);
	if (c.val() == c.defaultValue) {
		c.val() = '';
	}
}

function controlValueOnBlur(control) {
	if (control.value == '') {
		control.value = control.defaultValue;
	}
}

function submitForm( id ) {
	form = $('#' + id + ' form');
	if (form) {
		var options = {
			 target: '#' + id,
			 beforeSubmit: function(formData, jqForm, options) {
			 	$( '#' +  id + ' div.status' ).addClass('rolling');
			 	$( '#' +  id + ' div.content' ).hide();
			 }
		};
		form.ajaxSubmit(options);
	}
}


var hovered = false;

function hovershow(hover) {
	if (!hovered) {
		hovered = hover;
	}
	if (hover != hovered) {
		$(hovered).hide();
	}
	$(hover).show();
}

function hoverhide(hover) {
	if (hover) {
		$(hover).hide();
		hovered = false;
	}
}

function hoverinit(item) {

    $("a." + item).bind("mouseover",
      function () {
      	hovershow('#sub-' + item, this);
      }
    );
    $("a." + item).bind("mouseout",
      function () {
      	hoverhide('#sub-' + item, this);
      }
    );
    
    $("#sub-" + item).bind("mouseover",
      function () {
      	hovershow('#sub-' + item, this);
      }
    );
    $("#sub-" + item).bind("mouseout",
      function () {
      	hoverhide('#sub-' + item, this);
      }
    );
}


var profiles =
{

	window600:
	{
		height:700,
		width:600,
		center:1
	},
	
	window580:
	{
		height:650,
		width:580,
		center:1
	}
}

        function popup(url) {
            winPopup =  window.open(url,'popup','scrollbars=yes,resizable=yes,status=yes,titlebar=yes');
        }

        function popup(url, winName, features) {
            winPopup =  window.open(url, winName, features);
        }

        function popupcenter(url, w, h, addFeatures) {
            leftVal = (screen.width - w) / 2;
            topVal = (screen.height - h) / 2;

            _addFeatures = addFeatures;
            if (addFeatures != '') {
              _addFeatures += ',';
            }
            winPopup =  window.open(url, 'popup', _addFeatures+'width='+w+',height='+h+',left='+leftVal+',top='+topVal);
            winPopup.focus();
        }
        
        
			function myTextExtraction(node) {
			            return node.childNodes[0].innerHTML; 
        	} 
			



$(document).ready(function(){
	/* Init Menus */
	hoverinit('menu-l1');
	hoverinit('menu-l2');
	hoverinit('menu-l3');

	hoverinit('menu-l4');
	hoverinit('menu-l5');

	hoverinit('menu-l7');
	hoverinit('menu-l8');
	hoverinit('menu-l9');

	/* Convert Media 
	$('a.audio').media( { width: 400, height: 20 } );
	$('a.video').media();
	$('a.media').media();
	*/
	
	/* Disconnect Button */
	$('a#logout').click( function(e) {
		document.forms[0].submit();
	});

	$('.popup').popupwindow(profiles);
	
	a = readCookie('size');
	if (a) {
		if (a == '1') {
			$('.main-col p').css('font-size', '11px');
			$('.main-col p').css('line-height', '16px');
		}
		if (a == '2') {
			$('.main-col p').css('font-size', '14px');
			$('.main-col p').css('line-height', '19px');
		}
		if (a == '3') {
			$('.main-col p').css('font-size', '16px');
			$('.main-col p').css('line-height', '22px');
		}
	}
	
	/* Text size widget */
	$('#size-1').click(function() {
		$('.main-col p').css('font-size', '11px');
		$('.main-col p').css('line-height', '16px');
		eraseCookie('size');
		createCookie('size', '1');
	});

	$('#size-2').click(function() {
		$('.main-col p').css('font-size', '14px');
		$('.main-col p').css('line-height', '19px');
		eraseCookie('size');
		createCookie('size', '2');
	});

	$('#size-3').click(function() {
		$('.main-col p').css('font-size', '16px');
		$('.main-col p').css('line-height', '22px');
		eraseCookie('size');
		createCookie('size', '3');
	});
	
	/* GIR */
	$('#girdataselect').change(function(){
		$('#girtrimesterlist').load("/WVotesSelect/gettrimesterlist/" + $('#girdatalanguage').val() + '/' + $('#girdataselect').val() );
		eraseCookie('girsem');
		createCookie('girsem', $('#girdataselect').val());
	});
	
	girsem = readCookie('girsem');
	if (girsem) {
		$('#girdataselect').val(readCookie('girsem'));
		$('#girtrimesterlist').load("/WVotesSelect/gettrimesterlist/" + $('#girdatalanguage').val() + '/' + $('#girdataselect').val() );
	}
	else {
		var d = new Date();
		eraseCookie('girsem');
		createCookie('girsem', d.getFullYear());
		$('#girtrimesterlist').load("/WVotesSelect/gettrimesterlist/" + $('#girdatalanguage').val() + '/' + $('#girdataselect').val() );
	}

	/* Decrypt mails */
	geo();

});

