var home_img_fade = 0.9;
var home_img_appear = 1.0;

function control_in(control, text){ if (text == control.value) control.value = ''; }
function control_out(control, text){ if ('' == control.value) control.value = text;}

Event.observe(window, 'load', function() {
    if (navigator.userAgent.indexOf("MSIE 6") > 0){
        $$("li.top").each(
            function(s){
                s.observe('mouseover', function() {
                    this.addClassName('hover');
                });

                s.observe('mouseout', function(evt) {
                    evt = evt ? evt : window.event;
                    var toEl = evt.toElement;
                                        
                    if (toEl != this){                    
                        while (null != toEl){
                            if (toEl == this) return;
                            toEl = toEl.parentNode;
                        }
                    }    
                        
                    if (null == toEl || this == toEl){                
                        this.removeClassName('hover');
                    }    
                });
            }
        );
                

        $$(".header-menu ul.main").each(
            function(s){
                s.observe('mouseover', function() {
                    if (!$(document.body).hasClassName('hide-selects')){
                        $(document.body).addClassName('hide-selects');
                    }    
                });

                s.observe('mouseout', function(evt) {
                    evt = evt ? evt : window.event;
                    var toEl = evt.toElement;
                    
                    while (null != toEl){
                        if (toEl == this) break;
                        toEl = toEl.parentNode;
                    }
                    
                    

                    if (null == toEl){
                        if ($(document.body).hasClassName('hide-selects')){                        
                            $(document.body).removeClassName('hide-selects');
                        }    
                    }
                });
            }
        );

    }
});

function addloadfunc(f){
        if(window.attachEvent){
                window.attachEvent("onload", function () {setTimeout(f,500);});
        } else 
                if(document.addEventListener || window.addEventListener){
                        if(document.addEventListener){
                                document.addEventListener("load", f, false);   
                        };

                        if(window.addEventListener){
                                window.addEventListener("load", f, false); 
                        };

                } 
                else {
                        if(typeof window.onload == "function"){
                                var fOld = window.onload;
                                window.onload = function(){ fOld(); f(); };
                        } 
                        else {
                                window.onload = f;
                        };
                };
} 



function doc_scroll_top(){
    
    if ( window.pageYOffset ) { return window.pageYOffset; }
    
    if ( document.documentElement && document.documentElement.scrollTop ) {return document.documentElement.scrollTop;}
    if ( document.body ) { return document.body.scrollTop; }

    return 0;
}

function doc_height() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function doc_width() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
        Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
        Math.max(D.body.clientWidth, D.documentElement.clientWidth)
    );
}

function viewport_height() {
    if (typeof window.innerWidth != 'undefined') {
        return window.innerHeight;
    } 
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) { // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
       return document.documentElement.clientHeight;
    } else  { // older versions of IE
       return document.getElementsByTagName('body')[0].clientHeight;
    }
}

function temp_popup_on(h2){
    $$('#popup h2 span')[0].update(h2);

    popup_on();
}


function popup_on(layerId){	if (!layerId) { layerId = 'popup'}	
    // hide selects
    $$('select').each(
            function(s){
        s.style.display = 'none';
      }
    );

    // show popup    
    draw_popup = function (layerId) {        var winWidth    = $(layerId).getWidth();
        var winHeight   = $(layerId).getHeight();
        
        $(layerId).style.top  = 15 + doc_scroll_top() +'px';
        $(layerId).style.left = '15px';
        $(layerId).style.display = '';    
        
        if (doc_width() > winWidth){
            $(layerId).style.left = parseInt((doc_width() - winWidth)/2) + 'px'
        }

        if (viewport_height() > winHeight) $(layerId).style.top = parseInt(doc_scroll_top() + (viewport_height() - winHeight)/2) + 'px';
        else { 
            var minheight = parseInt(doc_height() - winHeight);
            if (minheight > doc_scroll_top()+50) $(layerId).style.top = doc_scroll_top()+50 + 'px';
            else $(layerId).style.top = minheight + 'px';
        }        };
    draw_popup(layerId);    if (window.onresize) { window.onresize = draw_popup(layerId); }    if (!$('shadow').visible()) {
        $('shadow').style.height = doc_height() + 'px';        $('shadow').style.width = doc_width() + 'px';             $('shadow').style.display = '';            	new Effect.Opacity('shadow', { duration: 0.5, from: 0.2, to: 0.7 });    }
}function showShadow(){	$('shadow').style.height = doc_height() + 'px';	$('shadow').style.width = doc_width() + 'px';	$('shadow').style.display = '';	$('shadow').style.opacity = 0.7;	$('shadow').style.visibility = 'visible';	$('shadow').style.zIndex = 0;		$('shadowHeader').style.height = '98px';	$('shadowHeader').style.width = '1000px';	$('shadowHeader').style.display = '';	$('shadowHeader').style.opacity = 0.7;	$('shadowHeader').style.visibility = 'visible';	$('shadowHeader').style.zIndex = 200;		$('shadowFooter').style.height = '56px';	$('shadowFooter').style.width = '1000px';	$('shadowFooter').style.display = '';	$('shadowFooter').style.opacity = 0.7;	$('shadowFooter').style.visibility = 'visible';	$('shadowFooter').style.zIndex = 200;}function hideShadow(){	$('shadow').style.display = 'none';	$('shadow').style.visibility = 'hidden';		$('shadowHeader').style.display = 'none';	$('shadowHeader').style.visibility = 'hidden';		$('shadowFooter').style.display = 'none';	$('shadowFooter').style.visibility = 'hidden';}
function popup_off(layerId){	if (!layerId) { layerId = 'popup'}	
    $('shadow').style.display = 'none';
    $(layerId).style.display = 'none';

    // hide selectes
    $$('select').each(
            function(s){
        s.style.display = '';
      }
    );
    window.onresize = null;
    
    new Effect.Opacity('shadow', { duration: 0.1, from: 0.7, to: 0.2 });
}

function home_menu_restore(){  }

var home_timer = '';
var home_old = '';

function home_menu_on(el_id){
    if ($('header').className != el_id){
        if ('' != home_timer){
            clearTimeout(home_timer);
            home_timer = '';
        }            
        
        if ('' == home_old){
            home_old = $('header').className;
        }    
              
        $('header').className = el_id;
                            
        to_exec = "if (home_old != '" + el_id + "') {new Effect.Fade('" + home_old + "-bg', {duration: " + home_img_fade + ", from:0.99, to:0.0}); new Effect.Appear('" + el_id + "-bg', {duration: " + home_img_appear + ", from:0.1, to:1.0});} home_old = '';";
        home_timer = setTimeout(to_exec, 1000);
    }    
}

function home_menu_off(el){}

function slide_up(el_id, is_current){
    if (!is_current) {
        $(el_id).setStyle({top: '18px'});
        $(el_id).setStyle({display: ''});
    } else { $(el_id).setStyle({top: '0px'});}
    
    setTimeout('slide_up_callback("' + el_id + '", ' + is_current + ')', 50);    
}

function slide_down(el_id, is_current){
    if (!is_current) {
        $(el_id).setStyle({top: '-18px'});
        $(el_id).setStyle({display: ''});    
    } else { $(el_id).setStyle({top: '0px'});}
   
    setTimeout('slide_down_callback("' + el_id + '", ' + is_current + ')', 50);    
}

function slide_down_callback(el_id, is_current){   
    if ((!is_current && parseInt($(el_id).getStyle('top')) < 0) || ( is_current && parseInt($(el_id).getStyle('top')) < 18)){
        $(el_id).setStyle({top: (parseInt($(el_id).getStyle('top')) + 2) + 'px'});
        setTimeout('slide_down_callback("' + el_id + '", ' + is_current + ')', 50);    
        return;
    }  
    footer_working=false;
}
function slide_up_callback(el_id, is_current){
    var top = parseInt($(el_id).getStyle('top'));
    if ((!is_current && top > 0) || (is_current && top > -18)){
        $(el_id).setStyle({top: (top - 2) + 'px'});
        setTimeout('slide_up_callback("' + el_id + '", ' + is_current + ')', 50); 
        return;        
    }
    footer_working=false;
}

var footer_working = false;
function footer_next(el_id){
    if (footer_working) return;
    footer_working = true

    el = $(el_id);
       
    el.getElementsBySelector("p.open").each(

        function(s){
            s.removeClassName('open');
            slide_up(s.id, true);

            if (s.next('p')){                   
                slide_up(s.next('p').id, false);
                s.next('p').addClassName('open');
                
            }
            else{
                slide_up(el.getElementsBySelector("p").toArray().first().id);
                el.getElementsBySelector("p").toArray().first().addClassName('open');
            }

        }
    );
    return false;
}

function footer_prev(el_id){
    if (footer_working) return;
    footer_working = true;

    el = $(el_id);

    el.getElementsBySelector("p.open").each(
        function(s){
            s.removeClassName('open');
            slide_down(s.id, true);
            
            if (s.previous('p')){
                slide_down(s.previous('p').id, false);
                s.previous('p').addClassName('open');
            }
            else{
                slide_down(el.getElementsBySelector("p").toArray().last().id);
                el.getElementsBySelector("p").toArray().last().addClassName('open');
            }
        }
    );
    return false;
}



function custom_gallery_on(el_id){
    el = $(el_id);

    if (!el.hasClassName('open')){
        el.addClassName('open');
    }
}

function custom_gallery_off(el_id){
    el = $(el_id);

    if (el.hasClassName('open')){
        el.removeClassName('open');
    }
}

function test_request(select){
    //".form-upgrade div"
    $$(select).each(
        function(s){

            if (!s.hasClassName('clear')){
                s.addClassName('err');
            }
        }
    );

    return false;
}

function design_gallery(show_id, hide_id){
    show = $(show_id);
    hide = $(hide_id);

    if (show.hasClassName('opacity')){
        show.removeClassName('opacity')
    }
    if (!hide.hasClassName('opacity')){
        hide.addClassName('opacity')
    }

}

function payment_schedule(el_id){
    el = $(el_id);
    
    $$('.payment-schedule .selected').each(
        function(s){
            s.removeClassName('selected');
        }
    );

    el.addClassName('selected');
}

function liveapp_tab(el_id){
    el      = $(el_id);
    el_data = $(el_id + '-data');

    $$('.tab-menu a').each(
        function(s){
            if (el != s){
                s.removeClassName('opened');
            }
        }
    );

    el.addClassName('opened');

    if ('tab5' == el_id){
        $$('.tab-data').each(
            function(s){
                s.addClassName('opened');
            }
        );

        $$(".tab-item").each(
            function(s){
                s.removeClassName('last');
            }
        );

        $$(".tab-item").toArray().last().addClassName('last');
    }
    else{
        $$('.tab-data').each(
            function(s){
                if (s != el_data){
                    s.removeClassName('opened');
                }
            }
        );

        el_data.addClassName('opened');

        el_data.getElementsBySelector(".tab-item").each(
            function(s){
                s.removeClassName('last');
            }
        );
        el_data.getElementsBySelector(".tab-item").toArray().last().addClassName('last');
    }
}

function question_all(el){
    el = Element.extend(el);
    
    if (el.hasClassName('open')){
        el.removeClassName('open');
        el.update('<span>Expand all</span>');
    
        $$(".video-tutorials .left-answers li").each(
            function(s){
                s.removeClassName('open');
            }
        );    
    }
    else{
        el.addClassName('open');        
        el.update('<span>Contract all</span>');
                    
        $$(".video-tutorials .left-answers li").each(
            function(s){
                s.addClassName('open');
            }
        );
    }    

    return false;
}

function question_switch(el){
    el = Element.extend(this.parentNode);

    if (el.hasClassName('open')){
        el.removeClassName('open');
    }
    else{
        el.addClassName('open');
    }

    return false;
}

function question_init(){
    $$(".video-tutorials .left-answers li a").each(
        function(s){
            s.onclick = question_switch;
        }
    );

}

function init_menu(){
}

var tour = 1;

function tour_prev(){
    if (tour > 1){
        $('tour' + tour).style.display = 'none';
        $('tour' + tour + '-bg').style.display = 'none';
        tour = tour - 1;
        $('tour' + tour).style.display = '';
        $('tour' + tour + '-bg').style.display = '';
    }
    
    if (tour > 1){
        $('tour-prev').addClassName('active');
    }
    else{
        $('tour-prev').removeClassName('active');
    }
    
    if ($('tour' + (1 + tour))){
        $('tour-next').addClassName('active');
    }
    else{
        $('tour-next').removeClassName('active');
    }
    
    return false;
}

function tour_next(){
    if ($('tour' + (1 + tour))){
        $('tour' + tour).style.display = 'none';
        $('tour' + tour + '-bg').style.display = 'none';
        tour = tour + 1;
        $('tour' + tour).style.display = '';
        $('tour' + tour + '-bg').style.display = '';
    }
    
    if (tour > 1){
        $('tour-prev').addClassName('active');
    }
    else{
        $('tour-prev').removeClassName('active');
    }
    
    if ($('tour' + (1 + tour))){
        $('tour-next').addClassName('active');
    }
    else{
        $('tour-next').removeClassName('active');
    }
    
    return false;
}

function tmp_subscribe(){
    $$('.footer .form-subscribe').each(
        function(s){
            if (s.hasClassName('err')){
                s.removeClassName('err');
                s.addClassName('confirmed');
            }
            else{
                if(s.hasClassName('confirmed')){
                    s.removeClassName('confirmed');
                    s.addClassName('');                
                }
                else{
                    s.addClassName('err');
                }
            }    
        }
    );

  return false;
}

function webinar_subscribe(){
    $$('.webinar .form-subscribe').each(
        function(s){
            if (s.hasClassName('err')){
                s.removeClassName('err');
                s.addClassName('confirmed');
            }
            else{
                if(s.hasClassName('confirmed')){
                    s.removeClassName('confirmed');
                    s.addClassName('');                
                }
                else{
                    s.addClassName('err');
                }
            }    
        }
    );

  return false;
}