// Runs the script when page loads 
function OSGradient() {
	this.initialize.apply(this, arguments);
}

OSGradient.applyGradient = function(style, element) {
	var gradient = new OSGradient(style);
	gradient.applyGradient(element);
};
// Create a gradient for each element that has a divStyle.
// +divstyle.js+ sets the divStyles automatically.  This function does
// nothing if +divstyle.js+ has not been loaded, unless JavaScript
// code has explicitly set the +divStyle+ properties of any HTML
// Elements.
OSGradient.applyGradients = function() {
	try {DivStyle.initialize()} catch(e) {}
    var elements = document.getElementsByTagName('div');
    for (var i = 0, e; e = elements[i++]; ) {
        var style = e.divStyle;
        if (style && style.gradientStartColor)
            OSGradient.applyGradient(style, e);
    }
};

// Number of bands necessary for a smooth gradient, by component.
// The max of the color range and height are pinned to this.
OSGradient.maxBands = [192, 192, 96];

// The following properties need to be set in order for style.zIndex
// to work in IE.  This function is called the first time that a
// gradient is attached to an element.
OSGradient.setBodyStyle = function() {
	OSGradient.setBodyStyle = function() {}
    var style = document.body.style;
    style.position = 'relative';
    style.left = 0;
    style.top = 0;
    style.zIndex = 0;
};

//
// Instance methods
//

OSGradient.prototype.initialize = function(style) {
	this.style = style;
};

OSGradient.prototype.applyGradient = function(e) {
	 var width = e.offsetWidth;
	 var height = e.offsetHeight;


	var navTable = document.getElementById('Leftnavigation_leftNav');
	var navCell = document.getElementById('fred');
	height = navCell.offsetHeight - (navTable.offsetHeight-navCell.offsetHeight);
	e.style.height = height;
	   
	var gradientElement = (this.createCanvasGradient(e, width, height) ||
						   this.createGradientElement(width, height));
    //OSGradient.setBodyStyle();
	if (!navigator.appName.match(/Netscape/)){
	 	this.attachGradient(e, gradientElement);
	}

	
};

OSGradient.prototype.createCanvasGradient = function(e, width, height) {

	var canvas = document.createElement('canvas');
	var ctx;
	// Return null if canvas isn't supported.  The caller will
	// fall back on divs.
	try {ctx = canvas.getContext('2d')} catch (e) {return null}

	
	// Safari requires the following prior to rendering
	e.appendChild(canvas);
	

	if (navigator.appVersion.match(/Konqueror|Safari|KHTML/))
		canvas.style.position = 'fixed';
    
	canvas.setAttribute('width', width);
    canvas.setAttribute('height', height);


	
	var style = this.style;
    var c0 = style['gradient-start-color'];
    var c1 = style['gradient-end-color'];
    var r = style['border-radius'];
	
	ctx.beginPath();
	ctx.moveTo(0,r);
	//arcTo() produces NS_ERROR_NOT_IMPLEMENT in Firefox 1.5; use arc() instead:
	//ctx.arcTo(0,0,r,0,r);
	ctx.arc(r,r,r,Math.PI,-Math.PI/2,false);
	ctx.lineTo(width-r,0);
	//ctx.arcTo(width,0,width,r,r);
	ctx.arc(width-r,r,r,-Math.PI/2,0,false);
	ctx.lineTo(width,height-r);
	//ctx.arcTo(width,height,width-r,height,r);
	ctx.arc(width-r,height-r,r,0,Math.PI/2,false);
	ctx.lineTo(r,height);
	//ctx.arcTo(0,height,0,height-r,r);
	ctx.arc(r,height-r,r,Math.PI/2,Math.PI,false);
	ctx.clip();
	var g = ctx.fillStyle = ctx.createLinearGradient(0,0,0,height);
	g.addColorStop(0, OSUtils.color.long2css(c0));
	g.addColorStop(1, OSUtils.color.long2css(c1));
	ctx.rect(0,0,width,height);
	ctx.fill();
	return canvas;
};

OSGradient.prototype.makeSpan = function(x, y, width, height, color, opacity) {
	var properties = {position: 'absolute',
					  left: x+'px',
					  top: y+'px',
					  width: width+'px',
					  height: height+'px',
					  // for IE:
					  'font-size': 1,
					  'line-height': 0,
					  background: color};

	if (opacity != undefined) properties.opacity = opacity;
	var style = [];
	for (var p in properties)
		style.push(p + ':' + String(properties[p]));
	// IE requires the &nbsp;
	return '<div style="'+style.join(';')+'">&nbsp;</div>';
};

OSGradient.prototype.createGradientElement = function(width, height) {
	var style = this.style;
    var c0 = style['gradient-start-color'];
    var c1 = style['gradient-end-color'];
    var r = style['border-radius'];
	
	function xAt(y) {
		var dy = Math.max(r-y, y-(height-r));
		if (dy >= 0)
			return r - Math.sqrt(r*r-dy*dy);
		return 0;
	};
	
	var bands = 0;
	for (var shift = 24; (shift -= 8) >= 0; )
		bands = Math.max(bands,
						 1+Math.min(Math.abs(c0 - c1) >> shift & 255,
									OSGradient.maxBands[2-shift/8]));
	bands = Math.max(bands, height);
	
	var transitions = [];
	for (var i = 0; i <= bands; i++)
		transitions.push(Math.floor(i * height / bands));
	
	if (r) {
		var tops = [];
		var bottoms = [];
		var lastx = null;
		for (var y = 0; y <= r; y++) {
			var x = Math.ceil(xAt(y));
			if (x == lastx) continue;
			lastx = x;
			transitions.push(y);
			transitions.push(height-y);
		}
		transitions.sort(function(a,b){return a-b});
	}
	OSUtils.Array.removeDuplicates(transitions);
	
    var spans = [];
    for (var i = 0; i < transitions.length-1; i++) {
        var y = transitions[i];
        var h = transitions[i+1] - y;
        var x = Math.ceil(xAt(y));
        var color = OSUtils.color.interpolate(c0, c1, y/height);
		spans.push(this.makeSpan(x, y, width-2*x, h,
								 OSUtils.color.long2css(color)));
    }
	
    var g = document.createElement('div');
    g.innerHTML = spans.join('');
	if (true) {
	g.style.position = 'absolute';
    g.style.left = '0px';
    g.style.top = '0px';
    g.style.width = "20%";
    g.style.height = '100%';
    g.style.zIndex = -1;
	}
	
	return g;
};

OSGradient.prototype.attachGradient = function(parent, gradient) {
	gradient.style.position = 'absolute';
    gradient.style.left = '0px';
    gradient.style.top = '0px';
	// Setting the canvas's dimensions erases its contents in Firefox,
	// even though it's the SAME DIMENSIONS.
	if (gradient.width != parent.offsetWidth) 
		gradient.width = parent.offsetWidth;
	if (gradient.height != parent.offsetHeight) 
		gradient.height = parent.offsetHeight;
	gradient.style.zIndex = -1;
    if (!parent.style.position.match(/absolute|relative/i)) 
		parent.style.position = 'relative';	
	// The canvas parent has already been set, for Safari.
	if (gradient.parentNode != parent) 
		parent.appendChild(gradient);
};

/*
 * Utilities
 */ 
try {OSUtils} catch(e) {OSUtils = {}}
if (!OSUtils.color) {OSUtils.color = {}}
if (!OSUtils.Array) {OSUtils.Array = {}}

// 0x123456 -> "#123456"
OSUtils.color.long2css = function(n) {
  var a = "0123456789ABCDEF";
  var s = '#';
  for (var i = 24; (i -= 4) >= 0; )
    s += a.charAt((n>>i) & 0xf);
  return s;
};

// (a,b,0)->a; (a,b,1)->b; a and b are rrggbb color numbers.
OSUtils.color.interpolate = function(a, b, s) {
  var n = 0;
  for (var i = 24; (i -= 8) >= 0; ) {
    var ca = (a >> i) & 0xff;
    var cb = (b >> i) & 0xff;
    var cc = Math.floor(ca*(1-s) + cb*s);
    n |= cc << i;
  }
  return n;
};

// Modify +ar+ in-place to remove consecutive duplicates.
OSUtils.Array.removeDuplicates = function(ar) {
	var i = 0, j = 0;
	while (j < ar.length) {
		var v = ar[i] = ar[j++];
		if (!i || ar[i-1] != v) i++;
	}
	ar.length = i;
	return ar;
};


function drawGradients(objId, grad1, grad2) {
		var oElement = document.getElementById(objId);	
		var style = {'gradient-start-color': eval(grad1),'gradient-end-color' : eval(grad2), 'border-radius' : 0 };
        OSGradient.applyGradient(style, oElement);
}


matchColumns=function(divs){ 
     var contDivs,maxHeight,divHeight,d; 
     // get all <div> elements in the document 
     contDivs=[]; 
     // initialize maximum height value 
     maxHeight=0; 
     // iterate over all <div> elements in the document 
     for(var i=0;i<divs.length;i++){ 
          // make collection with <div> elements with class attribute 'container' 
		  //alert(divs[i].className)
          if(/\bgradientLeftNav\b/.test(divs[i].className)){ 
				//alert('true')
                d=divs[i]; 
                contDivs[contDivs.length]=d; 
                // determine height for <div> element 
                if(d.offsetHeight){ 
                     divHeight=d.offsetHeight; 					
                } 
                else if(d.style.pixelHeight){ 
                     divHeight=d.style.pixelHeight;					 
                } 
                // calculate maximum height 
                maxHeight=Math.max(maxHeight,divHeight); 
          } 
     } 
     // assign maximum height value to all of container <div> elements 
     for(var i=0;i<contDivs.length;i++){ 
          contDivs[i].style.height=maxHeight + "px"; 
     } 
}


function createGradient(divs) {
	if(!document.getElementById)return;
	
	objArray = getGradientObjects(divs);
	if(!objArray.length) return;
	
	for(i=0;i<objArray.length;i++) {
				
		params = ['gradientRightSide','ffffff','ffffff','vertical'];
		
		var width = objArray[i].offsetWidth;
		var height = objArray[i].offsetHeight;

		//resize div
		objArray[i].style.height = height;

		//set height so the borders resize correctly
		var rhs = document.getElementById('rhsOuter');
		rhs.style.height = height;	


		if(document.all && !window.opera) {
			objArray[i].style.width = width + "px";
			params[3] == "horizontal"?gType = 1:gType = 0;
			objArray[i].style.filter = "progid:DXImageTransform.Microsoft.Gradient(GradientType="+gType+",StartColorStr=\"#" + params[1] + "\",EndColorStr=\"#" + params[2] + "\")";
		} else 
		{
			colorArray = createColorPath(params[1],params[2]);
			x=0;y=0;
			if(params[3] == "horizontal") {
				w=Math.round(objArray[i].offsetWidth/colorArray.length);
				if(!w)w=1;
					h = height;
			} else {
				h = Math.round(height/colorArray.length);
				if(!h) h =1;
				w = width;
			}

			makeGrandParent(objArray[i]);
			tmpDOM = document.createDocumentFragment();
			for(p=0;p<colorArray.length;p++) {
				g = document.createElement("div");
				g.setAttribute("style","position:absolute;z-index:0;top:" + y + "px;left:" + x + "px;height:" + h + "px;width:" + w + "px;background-color:rgb(" + colorArray[p][0] + "," + colorArray[p][1] + "," + colorArray[p][2] + ");");
				params[3] == "horizontal"?x+=w:y+=h;
				tmpDOM.appendChild(g);
				if(y>=height || x >= width) break;
			}
			//the algotithm above is a little rough so we'll pad out any inaccuracies in the gradient
			//by adding the last last gradient until we read the full height
			while (y<height)
			{
				g = document.createElement("div");
				g.setAttribute("style","position:absolute;z-index:0;top:" + y + "px;left:" + x + "px;height:" + h + "px;width:" + w + "px;background-color:rgb(" + colorArray[colorArray.length-1][0] + "," + colorArray[colorArray.length-1][1] + "," + colorArray[colorArray.length-1][2] + ");");
				params[3] == "horizontal"?x+=w:y+=h;
				tmpDOM.appendChild(g);
			}
			objArray[i].appendChild(tmpDOM);
			tmpDOM = null;
		}

	}
}

function getGradientObjects(a) {
	objs = new Array();
	for(i=0;i<a.length;i++) {
		c = a[i].className;
		if(c != "") if(c.indexOf("gradientRightSide") == 0) objs[objs.length] = a[i];
	} 
	return objs;
}
	
function createColorPath(color1,color2) {
	colorPath = new Array();
	colorPercent = 1.0;
	do {
		colorPath[colorPath.length]=setColorHue(longHexToDec(color1),colorPercent,longHexToDec(color2));
		colorPercent-=.01;
	} while(colorPercent>0);
	return colorPath;
}
		
function setColorHue(originColor,opacityPercent,maskRGB) {
	returnColor=new Array();
	for(w=0;w<originColor.length;w++) returnColor[w] = Math.round(originColor[w]*opacityPercent) + Math.round(maskRGB[w]*(1.0-opacityPercent));
	return returnColor;
}

function longHexToDec(longHex) {
	return new Array(toDec(longHex.substring(0,2)),toDec(longHex.substring(2,4)),toDec(longHex.substring(4,6)));	
}

function toDec(hex) {	
	return parseInt(hex,16);
}
	
function makeGrandParent(obj) {
	disp = document.defaultView.getComputedStyle(obj,'').display;
	disp == "block"?nSpan = document.createElement("div"):	nSpan = document.createElement("span");
	mHTML = obj.innerHTML;
	obj.innerHTML = "";
	nSpan.innerHTML = mHTML;
	nSpan.setAttribute("style","position:relative;z-index:10;");
	obj.appendChild(nSpan);
}

function doLeftNav(){
	//This executes if page is having valid left hand navigation
	var navTable = document.getElementById('Leftnavigation_leftNav');
	var rhs = document.getElementById('RightHandAreadiv');
	var y ;
	if (navTable!=null || rhs!=null)
	{
		//our gradients will be applied to divs
		//get all the divs, we will check this later to see which have gradient styles applied 
		 divs=document.getElementsByTagName('div'); 
		 //resize our left navigation to the page height before we apply the gradient
		 matchColumns(divs);
		 //apply the gradient to the leftnavigation
		 var ele=document.getElementById('Leftnavigation_leftNavFirstCell');
			if (ele.currentStyle)
				y = ele.currentStyle['display'];
			else if (window.getComputedStyle)
				y = document.defaultView.getComputedStyle(ele,null).getPropertyValue('display');

			if(y!='none')
			{
			  drawGradients('leftnavgrad',0xf8f8f8,0xf8f8f8);
		  }
		 if (rhs!=null)
		{
		var ele=document.getElementById('rhsOuter');
		if (ele!=null)
		{
		 //apply the gradient to the right hand sides
		 createGradient(divs);
		var rhsBottom = document.getElementById('rhsBottom')
		rhsBottom.className=rhsBottom.className+'On'
		
		}
		}
	}
}

// try to run via prototype js
// if this fails run directly from onload event
try {
	Event.observe(window,"load",doLeftNav)
} catch (error) {
	window.onload=doLeftNav;
}

 function popuphome()  
	{
        window.open('http://www.zurich-connect.es/connect/pruebanos/seguro-directo/popup_alerta.htm','ventana','width=530, height=355, toolbars=no, status=no');
        }