$(document).ready(document_ready);

function menu_item_click () {
    $('.globalNavMain').removeClass('selected');
    $(this).parent().parent().addClass('selected');
    reset_contact_form();
    $('.contentItem').hide();
    var obj = $(this).parent().parent();
    var index = obj.index();
    if (obj.index() == 11) { /* client login: float right reverses index of text and separator */
        index = 7;
    } else {
        index = index / 2;
    }
    if (index == 0) {
        $('.contentItem').eq(index).show().find('.panelPage').show();
    } else {
        obj = $('.contentItem').eq(index);
        obj.show();
        $('a',obj.find('.sectionNav').eq(0)).eq(0).click();
    }
}

function menu_item_mouseover () {
}

function menu_item_out () {
}

function item_click () {
    $('.sectionNavMain').removeClass('selected');
    $(this).parent().parent().addClass('selected');
    reset_contact_form();
    $('.panelPage').hide();
    var obj = $(this).parent().parent();
    var index = obj.index()/2;
    var obj = $(this).parent().parent().parent().parent().parent();
    var panel = $('.panelPage',obj).eq(index);
    panel.show();
    var minheight = 800;
    if (panel.css("height")===undefined || parseFloat(panel.css("height")) < minheight) {
        panel.css("height",minheight);
    }
}

function item_mouseover () {
}

function item_out () {
}

function reset_contact_form () {
    $('#send_button').hide();
    $('#Cancel').click();
    $('.simpleCaptchaSelected').removeClass('simpleCaptchaSelected');
    $('.contact_message').remove();
    $('#contact').show();
    $('#loading').hide();
}

function send_email () {
    $('#Send').hide();
    $("#loading").show();
    $.post("/send.php", $("#contact").serialize(),
           function(data){
               $("#contact").slideUp("normal", function() {
                                         $("#contact").before('<div class="contact_message"><h2>Thank you! Your email has been sent.</h2></div>');
                                     });
           }
          );
    return false;  // suppress the form action
}

function news_in () {
    return false;
}

function news_out () {
    return false;
}

function to_alliances() {
    $('#alliances a').eq(0).click();
    /* copied from menu_item_click. find a way to pass parameters to event handlers */
    var obj = $('#alliances');
    var index = obj.index()/2;
    if (index == 0) {
        $('.contentItem').eq(index).show().find('.panelPage').show();
    } else {
        obj = $('.contentItem').eq(index);
        obj.show();
        $('a',obj.find('.sectionNav').eq(0)).eq(0).click();
    }
    window.scrollTo(0,0);
    return false;
}

function to_news() {
    $('#company a').eq(0).click();
    $('#news a').eq(0).click();
    window.scrollTo(0,0);
    return false;
}

function products_contactus() {
    sitemap_goto(7);
    $('input[name="subject"]').val('Product demonstration request');
    window.scrollTo(0,0);
    return false;
}

function sitemap_goto(m,s) {
    if (s===undefined) { s = 1; }
    m = m - 1; s = s - 1;
    $('.globalNavMain').removeClass('selected');
    $(this).parent().parent().addClass('selected');
    reset_contact_form();
    $('.contentItem').hide();
    if (m == 0) {
        $('.contentItem').eq(m).show().find('.panelPage').show();
    } else {
        obj = $('.contentItem').eq(m);
        obj.show();
        $('a',obj.find('.sectionNav').eq(0)).eq(s).click();
    }
    return false;
}

function document_ready_cont () {
    $("#growl").notify();
    $("#contact").validate({submitHandler: function(form) {
                                send_email();
                            }});
    $('#send_button').hide();
    $('#captcha')
        .bind('loaded.simpleCaptcha', function() { /* on load */ })
        .bind('select.simpleCaptcha', function(data) {
                  $('#send_button').show();
              })
        .simpleCaptcha({introText: 'Please click on the <span class="captchaText"></span>.'});
    $("#contact").ajaxError(function() {
                                $('#loading').hide();
                                $('#Send').show();
                                $('#Cancel').click();
                            });
    //$('.newsitem').hover(news_in, news_out);
    $('#contact_us').click(function () {
                               $('.message').remove();
                               $('#loading').hide();
                               $('#Send').show();
                               $('#contact').show(); });

    $('#home_news h1').click(to_news);
    $('#home_alliances h1').click(to_alliances);
    $('#home_alliances a').click(to_alliances);
    $('#to_news').click(to_news);
    $('#home_news a').click(to_news);

    $('#more_company').click(function() {
                                 $('#company a').eq(0).click();
                                 window.scrollTo(0,0);
                                 return false;});
    $('.email_info').append(email_link('info','iqcs.com','small'));
    $('.email_sales').append(email_link('sales','iqcs.com','small'));
    $('.email_hr').append(email_link('jobs','iqcs.com','small'));

    $('.globalNav a').filter(function (index) { return index != 7; }).click(menu_item_click).mouseover(menu_item_mouseover).mouseout(menu_item_out);
    $('.sectionNav a').click(item_click).mouseover(item_mouseover).mouseout(item_out);

    $('img[alt=screencap]').click(products_contactus);
}

function document_ready () {
    var year=new Date().getYear();
    if (year<1000) year+=1900;
    $('.year').append(year);
    make_clock();
    $('.contentItem').eq(0).show().addClass('selected');
    $('.slideshow2').cycle({ fx: 'fade', timeout: 5000, before: onBefore });
/*
    $.ajax({ url: "/index3",
             context: $('#contentBox'),
             dataType: "html",
             success: function (data, status, req) {
                 //alert('content status '+status);
                 $(this).append(data);
                 document_ready_cont();
             },
             complete: function (req, status) {
                 //alert('content complete '+status);
             }});
*/
    document_ready_cont();
    $.ajax({ url: "/slideshow",
             context: $('#home_slideshow'),
             dataType: "html",
             success: function (data, status, req) {
                 //alert('slideshow status '+status);
                 $(this).append(data);
                 $('.slideshow').cycle({ fx: 'fade', timeout: 7000 });
             },
             complete: function (req, status) {
                 //alert('slideshow complete '+status);
             }});
}

function onBefore () {
    var text = $(this).find('img')[0].alt;
    $('#home_aboutus h1 a').text(text);
    if (text == "Our Products") {
        $('#home_aboutus h1 a').click(function () { sitemap_goto(3,1) });
    } else {
        $('#home_aboutus h1 a').click($(this).find('a')[0].onclick);
    }
}

function make_clock () {

    var date = new Date();
    var notNY = ((date.getTimezoneOffset() != 240) && /* New York DST */
                 (date.getTimezoneOffset() != 300));  /* New York EST */
    var clock_local;
    try {
        clock_local = new Clock ('clock_local');
    } catch (x) {
        return;
    }
    clock_local.Set('clock.digital',false);
    clock_local.Set('clock.numbers',true);
    clock_local.Set('clock.background',false);
    clock_local.Set('clock.shadow',false);
    clock_local.Set('clock.hands.tail',false);
    clock_local.Set('clock.text.size',8);
    clock_local.Set('clock.gutter',0);
    clock_local.Set('clock.centerpin',false);
    clock_local.Set('clock.strokestyle','#004566'); // 003366
    clock_local.Set('clock.seconds.style','red');
    clock_local.Set('clock.timezone',notNY?'Local':false);
    clock_local.Set('clock.tzoffset',false);
    clock_local.Draw();
    DrawClock(clock_local);

    if (notNY) {
        $('#header_bottom').css('margin-top', '30px');
        var clock_ny = new Clock ('clock_ny');
        clock_ny.Set('clock.digital',false);
        clock_ny.Set('clock.numbers',true);
        clock_ny.Set('clock.background',false);
        clock_ny.Set('clock.shadow',false);
        clock_ny.Set('clock.hands.tail',false);
        clock_ny.Set('clock.text.size',8);
        clock_ny.Set('clock.gutter',0);
        clock_ny.Set('clock.centerpin',false);
        clock_ny.Set('clock.strokestyle','#004566');
        clock_ny.Set('clock.seconds.style','red');
        clock_ny.Set('clock.timezone','New York');
        clock_ny.Set('clock.tzoffset',-4);
        clock_ny.Draw();
        DrawClock(clock_ny);
    } else {
        $('#clock_ny').remove();
        $('#clock_local').css('margin-bottom', '-30px').css('margin-right', '-30px');
    }
}

function DrawClock(clock) {
    clock.Draw();
    setTimeout(function () { DrawClock(clock) },1000);
}

function email_link (euser,ehost,eclass,edesc) {
    if (eclass) {
        eclass=' class="'+eclass+'"';
    } else {
        eclass='';
    }
    if (edesc===undefined) {
        edesc=euser+'@'+ehost;
    }
    var retEval='<a'+eclass+' style="color: #3b73b9;" href="'+'mail'+'to:'+euser+'@'+ehost+'">'+edesc+'<\/a>';

    return retEval;
}

