/*
.........................
:: Javascript a medida. ::
.........................
 */

// Globals and Constants

    var MAX_FONT = 1.2;
    var MIN_FONT = 1;
    var LIMIT_FONT = MIN_FONT;
   

// inicialize values DOMREADY
		
	Window.onDomReady(function() {

	textChange = new fx.Style('content', 'font-size', {unit: 'em', duration: 500, transition: Fx.Transitions.sineInOut});

	rememberTextChange();

	});

// GLOBAL ONLOAD - General actions
    window.addEvent('load', function() {

        // Text Resize

        $('sizeUp').addEvent('click', function(){

            if(LIMIT_FONT < MAX_FONT) {

                textChange.custom(MIN_FONT,MAX_FONT);

                LIMIT_FONT = MAX_FONT;

                Cookie.set("textChange", MAX_FONT);

            }

        });

        $('sizeDown').addEvent('click', function(){

            if(LIMIT_FONT > MIN_FONT) {

                textChange.custom(MAX_FONT,MIN_FONT);

                LIMIT_FONT = MIN_FONT;

                Cookie.set("textChange", MIN_FONT);
			}
        });
		
		/* Unobtrusive Flash Objects (UFO) */
		var hasObjectInside = (($E('object','left') || $E('embed','left')));
		
		// checking whether we have to do a replacement of the flash
		if (hasObjectInside && isIE()) {
		
			// Is an object or an embed tag?
			if (hasObjectInside.toString().indexOf('object') != -1) {
				source = hasObjectInside.getProperty('data');
			} else if (hasObjectInside.toString().indexOf('embed') != -1) {
				source = hasObjectInside.getProperty('src');
			}
				
			var FO = { movie:""+source+"", width:"215", height:"290", majorversion:"6", build:"0", xi:"true" };
			UFO.create(FO, "left");
		}
		
		/* Access form labels init. */
		setTimeout(initOverLabels, 50);
		
    });
	
	// creating new windows events on links
	window.addEvent('load', targetBlanks);
	
	// Emulate CSS pseudoclases with Javascript
	//window.addEvent('load', suckerfish(sfFocus, 'INPUT', 'contactForm'));

	
// ***************************
//       FUNCTIONS starts here
// ***************************

	function targetBlanks() {
		
		var As = $('content').getElements('a'); // get all anchors within content
		
		As.forEach( function(el) {
			if (el.getProperty('rel') == 'newWindow') {
				el.onclick = function() {
					window.open(this.href);
					return false;
				}
			}
		});
		
	}

	function rememberTextChange() {

        var cookie = Cookie.get("textChange"); // Return the text size

        if (cookie) { // if already there's a cookie

            textChange.set(cookie); // asign font size from the cookie value

            LIMIT_FONT = cookie;    // so... target the new font size limit

        }

    }

    function initOverLabels () {

      if (!document.getElementById) return;      

      var labels, id, field;

      // Set focus and blur handlers to hide and show 
      // labels with 'overlabel' class names.

      labels = document.getElementsByTagName('label');

      for (var i = 0; i < labels.length; i++) {

        if (labels[i].className == 'overlabel') {

          // Skip labels that do not have a named association
          // with another field.

          id = labels[i].htmlFor || labels[i].getAttribute('for');

          if (!id || !(field = document.getElementById(id))) {

            continue;

          } 

          // Change the applied class to hover the label 
          // over the form field.

          labels[i].className = 'overlabel-apply';

          // Hide any fields having an initial value.

          if (field.value !== '') {

            hideLabel(field.getAttribute('id'), true);

          }

          // Set handlers to show and hide labels.
		  
          field.onfocus = function () {

            hideLabel(this.getAttribute('id'), true);

          };

          field.onblur = function () {

            if (this.value === '') {

              hideLabel(this.getAttribute('id'), false);

            }

          };

          // Handle clicks to label elements (for Safari).

          labels[i].onclick = function () {

            var id, field;

            id = this.getAttribute('for');

            if (id && (field = document.getElementById(id))) {

              field.focus();

            }

          };

        }

      }

    };

    function hideLabel (field_id, hide) {

      var field_for;

      var labels = document.getElementsByTagName('label');

      for (var i = 0; i < labels.length; i++) {

        field_for = labels[i].htmlFor || labels[i].getAttribute('for');

        if (field_for == field_id) {

          labels[i].style.textIndent = (hide) ? '-1000px' : '0px';

          return true;

        }

      }

    }
 
 
/*
	detect IE
*/

function isIE()
{
    var userAgent   =   navigator.userAgent.toLowerCase();
    return (document.all && userAgent.indexOf('msie')!=-1);
}