 /* Helper Functions */
jQuery.fn._height = jQuery.fn.height;
jQuery.fn._width  = jQuery.fn.width;
jQuery.fn.height = function() {
	if ( this[0] == window ) return self.innerHeight || jQuery.boxModel && document.documentElement.clientHeight || document.body.clientHeight;	
	if ( this[0] == document ) return Math.max( document.body.scrollHeight, document.body.offsetHeight );	
	return this._height(arguments[0]);
};
jQuery.fn.width = function() {
	if ( this[0] == window ) return self.innerWidth || jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth;	
	if ( this[0] == document ) return Math.max( document.body.scrollWidth, document.body.offsetWidth );	
	return this._width(arguments[0]);
};
/* End Helper Functions */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
      
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { 
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


function atmCLL() {

}

function addFlash(file, w, h, id){
	AC_FL_RunContent(
		'codebase', 'https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
		'pluginspage', 'https://www.macromedia.com/go/getflashplayer',
		'width', w,
		'height', h,
		'src', file,
		'movie', file,		
		'scale', 'noScale',
		'wmode', 'transparent',
		'allowfullscreen','true',
		'menu', 'false',
		'allowscriptaccess', 'always',
		'swLiveConnect', 'true',
		'id', id,
		'name', id
	); //end AC code	
}

function linksParceirosCheck() {
	if (jQuery(window).width() < 1000) {
		jQuery('#links_parceiros').hide();
	}
	else {
		jQuery('#links_parceiros').show();
	}
}

function setVitrineLink(url, win, txt) {
	jQuery('#destaque_link_vitrine a').attr('href', url).attr('target', win).text(txt);
}

// Esse objeto controla o over das imagens do menu de sites
MainMenu = {			
	aImgs : [],
	active : 0,
		 
	init : function (imgs, active) {
		this.active = active;
		for (key in imgs){
			this.addImage(key, imgs[key]);
		}
	},
		
	addImage : function(id, src) {
		this.aImgs[id] = new Image();
		this.aImgs[id].src = src;
	},
		
	over : function(id) {
		if(id == this.active) return;
		
		jQuery('div#sites_menu_img img').attr('src', this.aImgs[id].src);
	},
	
	out : function() {
		jQuery('div#sites_menu_img img').attr('src', this.aImgs[this.active].src);
	}
}

jQuery(document).ready(function(){
	jQuery('#main_menu ul > li').hover(function(){		
		var ul = jQuery('ul', this);
		var li_last = jQuery('#main_menu ul > li.last-item');
		var ul_last = jQuery('#main_menu ul > li.last-item > ul');					
		
		if(!ul.attr('bg_w')){
			ul.show().find('li').width(ul.width() + "px");	
			ul.attr('bg_w', '1');			
		}
		else{
			ul.show();	
		}		
		
		if(li_last.length > 0 &&  ul_last.length > 0){
			var ul_last_margin = ul_last.width() - li_last.width() + 2; 
			ul_last.css('margin-left', (-ul_last_margin) + 'px');
		}
		
	}, function(){jQuery('ul', this).hide();});
});

function abrirPopup(link, largura, altura) {
	window.open(link,'','width='+largura+',height='+altura+',top=100,left=200,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0')
}

var xmlHttp; 
function createxmlHttp() {
	try {
		// Firefox, Opera 8.0+, Safari
	    xmlHttp = new XMLHttpRequest();
	    return true;
	      
	} catch (e) {
        // Internet Explorer
        try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            return true;
            
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				return true;
				
			} catch (e) {
				alert("Seu browser nÃƒÆ’Ã‚Â£o suporta AJAX!");
                return false;
			}
		}
	}
}

/**
 * Permite somente digitacao de caracteres numericos
 */ 
function validaCampoNumerico(e) {
	var keyPressed = e.keyCode? e.keyCode : e.charCode;
	return (keyPressed >= 48 && keyPressed <= 57);
}

/**
 * Nao permite os caracteres: ', ", >, <, ;, %
 */
function validaCampoAlfaNumerico(e) {
	
	var keyPressed = e.keyCode? e.keyCode : e.charCode;
	return !(keyPressed == 39 || keyPressed == 34 ||  keyPressed == 37 || keyPressed == 62 || keyPressed == 60 || keyPressed == 59);
	
}

//### HOTSITES ### 
var iframediadasmaes = ""+
"<div style='position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index:999;' id='injectionDiv'>"+
"<div align='center' style='position: absolute; text-align: center; width: 100%;'><div style='width: 1000px;text-align:right; margin:auto;'>"+
"<a onclick='closeHotsite();' href='#'><img src='http://www.claro.com.br/diadasmaes2009/fechar.gif'/></a></div></div>"+
"<iframe src='http://www.claro.com.br/diadasmaes2009/?idlocal=IDLOCAL' scrolling='no'  frameborder=0 width='100%' height='100%'></iframe></div>";
			
var iframediadosnamorados = ""+
"<div style='position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index:999;' id='injectionDiv'>"+
"<iframe src='http://www.claro.com.br/diadosnamorados2009/?idlocal=IDLOCAL' scrolling='no'  frameborder=0 width='100%' height='100%'></iframe></div>";

var iframediadospais = ""+
"<div style='position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index:999;' id='injectionDiv'>"+
"<iframe src='http://www.claro.com.br/faleeganhe/hotsite.htm?idlocal=IDLOCAL' scrolling='no'  frameborder=0 width='100%' height='100%'></iframe></div>";

var iframepromo = ""+
"<div style='position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index:999;' id='injectionDiv'>"+
"<iframe src='http://www.claro.com.br/offpromo/hotsite.htm?IDLOCAL' scrolling='no'  frameborder=0 width='100%' height='100%'></iframe></div>";

var iframenatal = ""+
"<div style='position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index:999;' id='injectionDiv'>"+
"<iframe src='http://www.claro.com.br/natal2009/hotsite.htm?IDLOCAL' scrolling='yes'  frameborder=0 width='100%' height='100%'></iframe></div>";

var iframeciclismo = ""+
"<div style='position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index:999;' id='injectionDiv'>"+
"<iframe src='http://www.claro.com.br/clarociclismo/hotsite.htm?IDLOCAL' scrolling='yes'  frameborder=0 width='100%' height='100%'></iframe></div>";

var iframemaes = ""+
"<div style='position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index:999;' id='injectionDiv'>"+
"<iframe src='http://www.claro.com.br/maes2010/hotsite.htm?IDLOCAL' scrolling='yes'  frameborder=0 width='100%' height='100%'></iframe></div>";

var iframeronaldo = ""+
"<div style='position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index:999;' id='injectionDiv'>"+
"<iframe src='http://www.claro.com.br/claroronaldo/hotsite.htm?idlocal=IDLOCAL' scrolling='yes'  frameborder=0 width='100%' height='100%'></iframe></div>";

var iframepais2010 = ""+
"<div style='position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index:999;' id='injectionDiv'>"+
"<iframe src='http://www.claro.com.br/pais2010/hotsite.htm?idlocal=IDLOCAL&tipo=CTIPO&token=CTOKEN' scrolling='yes' frameborder=0 width='100%' height='100%'></iframe></div>";

var intervalhotsite;

function openHotsite( iframehotsite ){
	
	clearInterval(intervalhotsite);
	intervalhotsite = setInterval( function(){
		
		if(document.hash == '#fechar'){
			clearInterval(intervalhotsite);
			document.hash = '';
			closeHotsite();
		}	
		
	}, 1000);
	
	$("#footer").hide();
	$("#content").hide();
	$("body").append(iframehotsite);
	
};

function closeHotsite(){
	
	clearInterval(intervalhotsite);
	
	$('#injectionDiv').remove();
	$('#footer').show();
	$('#content').show();
	
}


$(document).ready(function(){

	var idlocal = String(document.location.href.split('idlocal=')[1]).substring(0,2);

	if(document.location.href.split('flash=/StaticFiles/Flash/landing_ciclismo').length > 1){
		openHotsiteCiclismo(idlocal);
	} else if(document.location.href.split('flash=/StaticFiles/Flash/2010maes_landing_fase2').length > 1){
		openHotsiteDiadasmaes2010(idlocal);
	} else if(document.location.href.split('flash=/StaticFiles/Flash/ronaldo_landing_fase3').length > 1){
		openHotsiteRonaldo(idlocal);
	} else if(document.location.href.split('flash=/StaticFiles/Flash/landingPais2010').length > 1){
		openHotsitePais2010(idlocal);
	}
	
});


// provisory
function openHotsiteDiadasmaes2009(idlocal){
	openHotsite(iframediadasmaes.split('IDLOCAL').join(idlocal));
};

function openHotsiteDiadasmaes2010(idlocal){
	openHotsite(iframemaes.split('IDLOCAL').join(idlocal));
};

function openHotsiteCiclismo(idlocal){
	openHotsite(iframeciclismo.split('IDLOCAL').join(idlocal));
};

function openHotsiteDiadosnamorados2009(idlocal){
	openHotsite(iframediadosnamorados.split('IDLOCAL').join(idlocal));
};

function openHotsiteDiadospais2009(idlocal){
	openHotsite(iframediadospais.split('IDLOCAL').join(idlocal));
};

function openHotsitePromo(idlocal){
	openHotsite(iframepromo.split('IDLOCAL').join(idlocal));
};

function openHotsiteNatal(idlocal){
	openHotsite(iframenatal.split('IDLOCAL').join(idlocal));
};

function openHotsiteRonaldo(idlocal){
	openHotsite(iframeronaldo.split('IDLOCAL').join(idlocal));
};

function openHotsitePais2010(idlocal){
	
	var paisNovo = iframepais2010;
	var tipo2 = $.cookie('tipo');
	var token2 = $.cookie('token');
	
	paisNovo = paisNovo.split('IDLOCAL').join(idlocal);
	paisNovo = paisNovo.split('CTIPO').join(tipo2);
	paisNovo = paisNovo.split('CTOKEN').join(token2);
	
	openHotsite(paisNovo);
	
	$.cookie("token", '');
	$.cookie("tipo", '');
	
};

//### END HOTSITES ### 

