function openMenu(title, objId, amount) {
    // close all menus first.
    $('rightMenu').getElements('div[class=menu-body]').each(function(elem, idx) { elem.style.height = '0px'; });

    // show selected menu.
    if (amount > 0)
        $('menu_body_' + objId).style.height = (amount * 20 + 10) + 'px';

    $('rightMenu').getElements('strong').each(function(elem, idx) { elem.className = 'menu-item'; });

    // highlight menu.
    title.className = 'menu-item active';
}
function ChangeHeadLinePic(productId, imgFileName)
{
    document.getElementById('hdImage').src = siteRoot + 'images/products/' + productId + '/' + imgFileName;
}
function getProducts(pageIndex, amount, isMultiple) {
    var url;
    if (isMultiple)
        url = siteRoot + 'ajax/GetMultiPagedProducts.aspx';
    else
        url = siteRoot + 'ajax/GetPagedProducts.aspx';
    
    var myRequest = new Request(
    {
        method: 'get',
        url: url,
        onSuccess: function(responseText, responseXML) {
                    var cont = $('ProductsDiv');
                    cont.set('opacity',0);
                    cont.innerHTML = responseText;
                    cont.fade('in');
        }
    }).send('idx=' + pageIndex + '&amount=' + amount + (isMultiple ? '&pId=' + $('pId').value : ''));  
}

function changeChart(idx)
{
    // locate current visible chart
    var charts = $('divChart').getElements('img');
    
    var currentChart;
    
    charts.each(function(item, index){ if (item.getStyle('display') == 'inline') currentChart = item; } ); 
}

function topMenuActive(idx)
{
    if (idx == 1) {
        $('topMenu1').set('class','active');
        $('topMenu3').set('class','');
    }
    else {
        $('topMenu1').set('class','');
        $('topMenu3').set('class','active');
    }
}

// --------------------------------- Chart change functions ---------------------------------
var curIdx;

function cngImg(img1, img2)
{   
    var ef1 = new Fx.Morph(img1, {duration: 200, transition: Fx.Transitions.Sine.easeOut});

    ef1.start({
        'opacity': 0
    }).chain(function() {
        $(img1).setStyle('display', 'none');
        $(img2).set('opacity', 0);
        //if ($(img2).style.visibility != 'hidden') {
            $(img2).setStyle('display', 'inline');
            $(img2).fade('in');
        //}
    });
}

function cng(idx)
{
    cngImg('chartImg'+curIdx, 'chartImg'+idx);
    curIdx = idx;
}

function initChartChange(idx)
{
    curIdx = idx;
}

// --------------------------------- Headlines change functions ---------------------------------
var hdIdx = 0;
var idx1
var idx2;
var img1,img2;
var hdInterval;
var paused;
var autoEf;
var hideIdx;

function cngHL() {
    
    idx1 = hdIdx
    idx2 = hdIdx+1;
    
    if ($('hdImage'+idx2) == undefined) idx2 = 0;
    
    img1 = 'hdImage'+idx1;
    img2 = 'hdImage'+idx2;
    
    autoEf = new Fx.Morph(img1, {duration: 500, transition: Fx.Transitions.Sine.easeOut});

    autoEf.start({
        'opacity': 0
    }).chain(function() 
    {
        $(img1).setStyle('display', 'none');
        $(img2).set('opacity',0);
        $(img2).setStyle('display','block');
        $(img2).fade('in');
        highLight(idx2,false);
        hdIdx = idx2;
    });    
}

function initHeadLine() {
    hdIdx = 0;
    if ($('hdImage1') == undefined) return; // in case of one headline or less - disable the timer.
    
    highLight(0,true);
    
    // check if it's one of the products page.
    if ($('hdImage') != undefined) {}
        hdInterval = setInterval('cngHL()', 5000);
}

function highLight(idx, firstTime)
{
    // the main page hack (not h2 & ul elements)
    if ($('hdH21') == undefined) return;
    
    var i=0;
    
    if (!firstTime)
        while($('hdH2'+i) != undefined)
        {        
            $('hdH2'+i).setStyle('background-color', '#FFFFFF');
            $('hdUL'+i).setStyle('background-color', '#FFFFFF');
            i++;
        }
    
    $('hdH2'+idx).setStyle('background-color', '#EEEDFD');
    $('hdUL'+idx).setStyle('background-color', '#EEEDFD');
}

function showHeadline(idx) {
    if (idx == hdIdx) return;

    if (autoEf != undefined) autoEf.cancel();

    hideIdx = 0;
    while ($('hdImage' + hideIdx) != undefined) {
        $('hdImage' + hideIdx).setStyle('display', 'none');
        hideIdx++;
    }
    
    $('hdImage'+idx).setStyle('display','block');
    $('hdImage'+idx).set('opacity',0);    
    $('hdImage' + idx).fade('in');
    
    highLight(idx, false);
    clearInterval(hdInterval);
    hdIdx = idx;
}

// --------------------------------- -------------------------- ---------------------------------
window.addEvent('domready', function() {
    initHeadLine();
});

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        }
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")";
}
function setMenu(linkId)
{
    var myRequest = new Request(
    {
        method: 'get',
        url: siteRoot + 'ajax/SetMenu.aspx'        
    }).send('linkId=' + linkId);
}
function setMenuAndNavigate(linkId, url) {
    var myRequest = new Request(
    {
        method: 'get',
        url: siteRoot + 'ajax/SetMenu.aspx',
        onComplete: function(t) { location.href = url; }
    }).send('linkId=' + linkId);
}
function setSubMenuAndNavigate(linkId, url) {
    var myRequest = new Request(
    {
        method: 'get',
        url: siteRoot + 'ajax/SetSubMenu.aspx',
        onComplete: function(t) { location.href = url; }
    }).send('linkId=' + linkId);
}
function highlightIndice(idx) {
    var i=1;
    
    while($('ind'+i) != undefined)
    {        
        $('ind'+i).setStyle('background-color', '');
        i++;
    }
    
    $('ind'+idx).setStyle('background-color', '#EEEDFD');
}
function validEmail(address) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(address)
}
// ----------------------------- Products ajax fetch -----------------------
function getProductHeadlines(productId) {
    var url = siteRoot + 'ajax/GetProductHeadlines.aspx';

    var myRequest = new Request(
    {
        method: 'get',
        url: url,
        onSuccess: function(responseText, responseXML) {
            $('headlinesHolder').set('html', responseText);
            clearInterval(hdInterval);
            initHeadLine();
        }
    }).send('productId=' + productId);
}
function getProductContent(productId) {
    var url = siteRoot + 'ajax/GetProductContent.aspx';

    var myRequest = new Request(
    {
        method: 'get',
        url: url,
        onSuccess: function(responseText, responseXML) {
            $('contentHolder').set('html', "<div>" + responseText + "</div>");
        }
    }).send('productId=' + productId);
}
function setMixedProduct(productId) {
    $('tBox').getElements('td').each(function(item, index) { item.setStyle('borderColor', '#e6e6e5'); });
 
    var td = $('td_' + productId);
    td.setStyle('borderColor', '#6980CB');
}
function selectProduct(productId) {
    setMixedProduct(productId);
    getProductHeadlines(productId);
    getProductContent(productId)
}
function validXss(form) {
    var reg = /((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+((\%3E)|>)/;
    var coll = $(form).getElements('input');
    for(i=0;i<coll.length;i++)
    {
        if (coll[i].type == 'text')
        {
            if (reg.test(coll[i].value))
                return false;
        }        
    }
    coll = $(form).getElements('textarea');
    for(i=0;i<coll.length;i++)
    {
        if (reg.test(coll[i].value))
            return false;
    }
    return true;
}
