/*************************
	COOKIE MANAGEMENT
*************************/
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);
}	

/*************************
	EVENT MANAGEMENT
*************************/

function addEvent(obj, type, callback){
	if(obj.attachEvent){
		obj.attachEvent('on'+type, callback);
	}
	else if(obj.addEventListener){
		obj.addEventListener(type, callback, false);
	}
	else{
		var oldcallback = ob['on'+type] ? ob['on'+type] : null;
		
		ob['on'+type] = function(){
			if(oldcallback) oldcallback();
			callback();
		}
	}	
}

/*************************
	CLASS MANAGEMENT
*************************/

function addClassName(elm, classname){
   	elm.className += (elm.className ? ' ' : '') + classname;   
}

function removeClassName(elm, classname){
	var replacement = classname;
	var r = new RegExp(replacement, 'gi');

	elm.className = elm.className.replace(r,'');
}

/*************************
	POPUP MANAGEMENT
*************************/

function openWindow(url, name, w, h, x, y, _scroll){
	
	var screenWidth = screen.width;
	var screenHeight = screen.height;
	
	var wW = (w!=null) ? w : screenWidth-50;
	var wH = (h!=null) ? h : screenHeight-150;
		
	var wX = (x!=null) ? x : Math.round(screenWidth/2-(wW/2));
	var wY = (y!=null) ? y : Math.round(screenHeight/2-(wH/2));
    
    var name = (name != null) ? name : 'dyPopup';
    
    var scrollbar = (_scroll != null) ? _scroll : 0;

         if( url.indexOf('html/legal') == -1 ){
			var win  = window.open( url );
	 	}
	  	else{

    var win = window.open(url, name, 'resizable=0,scrollbars='+scrollbar+',status=0,location=0,toolbar=0,menubar=0,width='+wW+',height='+wH+',screenX='+wX+',screenY='+wY+',left='+wX+',top='+wY+'');
}

    return win;
}

function openLegal(url){
	var w = 400;
	var h = 500;
	
	openWindow(url, 'caciquelegal', w, h, null, null, 1);
}

function abrePopup(url,target,parametros) {
	var screenWidth = screen.width;
	var screenHeight = screen.height;
	
	var params = parametros.split(',');
	var param, w, h, x, y, _scroll;
	
	for(var i = 0; i < params.length; i++){
		param = params[i].split('=');

		if(param[0].toLowerCase() == 'width'){
			w = parseInt(param[1], 10);
		}
		if(param[0].toLowerCase() == 'height'){
			h = parseInt(param[1], 10);
		}			
		
		if(param[0].toLowerCase() == 'x'){
			x = parseInt(param[1]);
		}			
		
		if(param[0].toLowerCase() == 'y'){
			y = parseInt(param[1]);
		}		
		
		if(param[0].toLowerCase() == 'scrollbars'){
			_scroll = parseInt(param[1]);
		}				
	}
	
	var wW = (w!=null) ? w : screenWidth-50;
	var wH = (h!=null) ? h : screenHeight-150;
		
	var wX = (x!=null) ? x : Math.round(screenWidth/2-(wW/2));
	var wY = (y!=null) ? y : Math.round(screenHeight/2-(wH/2));
    
    var name = (target != null) ? target : 'caciquePopup';
    
    var scrollbar = (_scroll != null) ? _scroll : 0;

    var win = window.open(url, name, 'resizable=0,scrollbars='+scrollbar+',status=0,location=0,toolbar=0,menubar=0,width='+wW+',height='+wH+',screenX='+wX+',screenY='+wY+',left='+wX+',top='+wY+'');

}

function setError(id){
	var p = document.getElementById('error').getElementsByTagName('p')[0];
	p.innerHTML = ERRORS[id];
	toggle('error');
}

function toggle(id){
	var elm = document.getElementById(id);
	var display = (elm.style.display == 'block') ? 'none' : 'block';
	elm.style.display = display;
}

function initLegalLinks(){
	var footer = document.getElementById('htmlfooter') || document.getElementById('footer');
	
	if(!footer){
		return false;
	}
	
	var legallinks = footer.getElementsByTagName('a');
	for(var i = 0; i < legallinks.length; i++){
		legallinks[i].onclick = function(){
			openLegal(this.href);
			return false;
		}
	}
}

addEvent(window, 'load', initLegalLinks);


/********************************************
	FLASH
********************************************/
function initReplaceFlashContent(){
    var obs = document.getElementsByTagName('object');
    var len = obs.length;
    
	for (var i = 0; i < len; i++) {
		rewriteObjectHTML(obs[i]);
		obs[i].style.visibility = 'visible';
	}
}

function rewriteObjectHTML(node){
	
	var data = node.getAttribute('data');
	var flashVars = node.getAttribute('flashVars');
	
	var paramsString = '<param name="movie" value="'+data+'" />';
	if(flashVars){
		paramsString += '<param name="flashVars" value="'+flashVars+'" />';
	}
	
	var text = node.parentNode.innerHTML;

	//Create a test string in lowercase to avoid capitalization
	var testText = text.toLowerCase();

	//TODO: This maybe could be done with a RegExp...
	var start = testText.indexOf('<object');
	//Get rid of this part.
	testText = testText.substring(start);
	//Now the first '>' is the closing of '<object'
	var end = testText.indexOf('>') + 1;
	//Add start to end since we have substringed testText
	end += start;

	//Now we have stored everything untill closing 'object'
	var newText = text.substring(0, end);

	//Add params
	newText += paramsString;

	//Get the rest of the string
	end = testText.indexOf('</object>');
	//Again, add start to end
	end += start;

	newText += text.substring(end);
	
//	var newText = '<object type="application/x-shockwave-flash" data="/swf/escuchalallamada.swf" width="1050" height="640"><param name="movie" value="/swf/escuchalallamada.swf" /></object>';
	
	node.parentNode.innerHTML = newText;

}

