// JavaScript base
// by Daniel Koch - <daniel at fazendosistemas.com.br>

// clear menu
function menu_out(element) {
    menu_over_hide(element, true);
    hide_submenu(element);    
}

// disable all itens on menu
function menu_scan_off()
{
    var element = $('menu_ul');
    elementA = $A(element.getElementsByTagName('li'));
    elementA.each (
        // turn off each element
        function(subelement, index) {
            menu_out(subelement);
        }
    );
}

// do actions when mouse over on menu
function menu_over(element) {
    // first, clear menu
    menu_scan_off();    
    // hide context
    menu_over_hide(element);    
    // unhide submenu
    unhide_submenu(element);
}

// (un)hide element
function menu_over_hide(element, unhide) {

    // parsing actions
    if(unhide) {
        hideAction = 'block';
    } else {
        hideAction = 'none';
    }
    

    // getting all p elements from menu
    pNodes = element.getElementsByTagName('p');
    pNodesA = $A(pNodes);
    
    // getting all img elements from menu
    imgNodes = element.getElementsByTagName('img');
    imgNodesA = $A(imgNodes);
    
    // DRY!!
    funcaoVarre = function(current_element, index) {
        current_element.style.display = hideAction;
    }
    
    // running
    imgNodesA.each(funcaoVarre);
    pNodesA.each(funcaoVarre);    

}

function unhide_submenu(element) {
    // getting ul elements at menu
    ulNodes = element.getElementsByTagName('ul');
    ulNodesA = $A(ulNodes);
    
    ulNode = ulNodesA[0];
    if(!ulNode) return;
    
    // getting li elements from submenu
    liNodes = ulNode.getElementsByTagName('li');
    liNodesA = $A(liNodes);
    
    // first event, put the first LI with 131px on margin-left
    if(liNodes[0]) {
        // show it
        liNodes[0].style.display = 'block';
        
        // moving
        ulNode.style.marginLeft = "131px";
        
        // display all itens
        liNodesA.each(function(element, index) {
            element.style.display = 'block';
        });
    }   
}

function hide_submenu(element) {

    // getting ul elements at menu
    ulNodes = element.getElementsByTagName('ul');    
    ulNodesA = $A(ulNodes);
    
    ulNode = ulNodesA[0];
    if(!ulNode) return;
    
    // back to 0px on margin-left
    ulNode.style.marginLeft = "0px";
    
    // getting li elements from submenu
    liNodes = ulNode.getElementsByTagName('li');
    liNodesA = $A(liNodes);
    
    // hidding all li elements
    liNodesA.each(
        function(current_item, index) {
            current_item.style.display = 'none';
        }
    );

}

// get a random number
function getRandom(min, max) {
    var randomNum = Math.random() * (max-min); 
    // Round to the closest integer and return it
	return(Math.round(randomNum) + min); 
}


// reload the clients_image
function changeClientsImage(maxClientsImages) {    
    var randomNum = getRandom(1, maxClientsImages);
    $('clients_image').src = "images/clients/" + randomNum + ".gif";
}

function changeNewsImage(maxNewsImages) {    
	var randomNum = getRandom(1, maxNewsImages);

	$('news_image').src = "/site/images/press/" + randomNum + ".jpg";

	switch(randomNum){
		case 1:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=IME001-012')";
			break;
		}
		/*
		case 1:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=IME001-004')";
			break;
		}
		case 2:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=cnndez05')";
			break;
		}
		case 3:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=cnnjun06')";
			break;
		}
		case 4:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=cnnago06')";
			break;
		}
		case 5:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=bloomset06')";
			break;
		}
		case 6:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=cnnoct06')";
			break;
		}
		case 7:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=cnnnov06')";
			break;
		}
		case 8:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=cnndez06')";
			break;
		}
		case 9:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=cnndez06a')";
			break;
		}
		case 10:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=cnnjul07')";
			break;
		}
		case 11:	{
			$('url_news').href =  "javascript:popUpVideo('/site/sections/news/popup.php?movie=internet_security')";
			break;
		}*/
		

		
		
		
	}
}

function changeTestimonialsImage(maxTestimonialImages) {    
    var randomNum = getRandom(1, maxTestimonialImages);
	$('testimonial_image').src = "images/testimonials/" + randomNum + ".jpg";
	if(randomNum == 1){
		$('url_testimonial').href =  "/site/sections/clients/testimonials.php";
	}else{
		$('url_testimonial').href =  "/site/sections/clients/testimonials" + (randomNum - 1) + ".php";
	}

}

// starting vars
activateItem = activateSubItem = activateMenu = false;

function returnMenuActive()
{
    if (activateMenu) {
        element = $(activateMenu);
        menu_over(element);
    }
}

function activeElement(element_name) {
    if(element_name) {
        element = $(element_name);
        element.className = "active";
    }
}

function tabelaformatos(){
    window.open("http://insercao.uol.com.br/p6_formatos.php","formatos","width=370,height=450,scrollbars=no");
}

// display popUp
function popUp(arquivo) {
    var width = 514;
    var height = 397;
    var left = 40;
    var top = 50;
    var iePath = "";
    
    // browser detect
    if(navigator.appName == "Microsoft Internet Explorer") {
        iePath = "../../";
    }
    
    window.open(iePath+arquivo,'popUp','width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no,fullscreen=no'); 
}

function popUpAd(arquivo, type, file) {
    var width = 488;
    var height = 100;
    var left = 40;
    var top = 50;
    var iePath = "";
    
    // browser detect
    if(navigator.appName == "Microsoft Internet Explorer") {
        iePath = "../../";
    }
    
    window.open(arquivo+'?type='+type+'&file='+file,'popUp','width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no,fullscreen=no'); 
}

// display popUpVideo
function popUpVideo(arquivo) {
    var width = 215;
    var height = 194;
    var left = 40;
    var top = 50;
    var iePath = "";
    
    // browser detect
    if(navigator.appName == "Microsoft Internet Explorer") {
        iePath = "../../";
    }
    
    window.open(iePath+arquivo,'popUp','width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no,fullscreen=no'); 
}