/* $Id: jquery_ext.js 22 2009-09-28 10:51:03Z Ian.H $ */

$(function() {
//    $('div#ticker marquee').marquee('m-ticker').mouseover(function() {
//        $(this).trigger('stop');
//    }).mouseout(function() {
//        $(this).trigger('start');
//    });

    // World clocks
    var now = new Date();
    var thisYear = now.getFullYear();
    var dstEnabled = '0';

    // Calculate DST
    var dstStartDay;
    var dstStartMonth = 3;
    var dstEndDay;
    var dstEndMonth = 10;
    var thisMonth;

    if (thisYear == 2009) {
        dstStartDay = 29;
        dstEndDay = 25;
    } else if (thisYear == 2010) {
        dstStartDay = 28;
        dstEndDay = 31;
    } else if (thisYear == 2011) {
        dstStartDay = 27;
        dstEndDay = 30;
    } else if (thisYear == 2012) {
        dstStartDay = 25;
        dstEndDay = 28;
    } else if (thisYear == 2013) {
        dstStartDay = 31;
        dstEndDay = 27;
    } else if (thisYear == 2014) {
        dstStartDay = 30;
        dstEndDay = 26;
    } else if (thisYear == 2015) {
        dstStartDay = 29;
        dstEndDay = 25;
    } else if (thisYear == 2016) {
        dstStartDay = 27;
        dstEndDay = 30;
    } else if (thisYear == 2017) {
        dstStartDay = 26;
        dstStartDay = 29;
    } else if (thisYear == 2018) {
        dstStartDay = 25;
        dstEndDay = 28;
    } else if (thisYear == 2019) {
        dstStartDay = 31;
        dstEndDay = 27;
    }

    thisMonth = now.getMonth() + 1;

    if ((thisMonth >= dstStartMonth) && (thisMonth <= dstEndMonth)) {
        if ((thisMonth == dstStartMonth) && (now.getDate() >= dstStartDay)) {
            dstEnabled = '1';
        } else if ((thisMonth == dstEndMonth) && (now.getDate() <= dstEndDay)) {
            dstEnabled = '1';
        } else {
            dstEnabled = '1';
        }
    }

    $('#int-time-canada').flash({
        src:    '_assets/clocks/clock_utc.swf',
        width:  72,
        height: 17,
        flashvars: {
            hourDiff: '-5',
            useDST: dstEnabled
        }
    });
    $('#int-time-uk').flash({
        src:    '_assets/clocks/clock_utc.swf',
        width:  72,
        height: 17,
        flashvars: {
            hourDiff: '0',
            useDST: dstEnabled
        }
    });
    $('#int-time-finland').flash({
        src:    '_assets/clocks/clock_utc.swf',
        width:  72,
        height: 17,
        flashvars: {
            hourDiff: '2',
            useDST: dstEnabled
        }
    });
    $('#int-time-uae').flash({
        src:    '_assets/clocks/clock_utc.swf',
        width:  72,
        height: 17,
        flashvars: {
            hourDiff: '4',
            useDST: dstEnabled
        }
    });
    $('#int-time-india').flash({
        src:    '_assets/clocks/clock_utc.swf',
        width:  72,
        height: 17,
        flashvars: {
            hourDiff: '5.5',
            useDST: '0'
        }
    });

    // Flash header image
    $('#flash-logo').flash({
        src:    '_assets/logo_header_00_3.1.swf',
        width:  400,
        height: 150
    });

    $('div.image-row div a').lightBox();

    $('div.sales-dist-section').hide();

    $('ul.sales-dist-list > li > a').live('click', function() {
        $('div.sales-dist-section').hide();

        var item = $(this).attr('href').replace(/#/, '');
        $(this).parent().attr('id', item);

        if ($(this).attr('rel') == 'internal-page') {
            return true;
        } else if ($(this).attr('rel') == 'external-link') {
            window.open($(this).attr('href'));
            return false;
        } else {
            if ($(this).attr('rel') != 'open') {
                $('#' + item + ' > div').fadeIn(1000);
                $(this).attr('rel', 'open');
            } else {
                $('#' + item + '> div').fadeOut(750);
                $(this).attr('rel', 'closed');
            }
        }

        return true;
    });

    // Get anchor name for section to display
    var theUrl = document.location.toString();

    // Check if URL has anchor tag
    if (theUrl.match('#')) {
        // Extract anchor from URI
        var theAnchor = '#' + theUrl.split('#')[1];

        // Open relative section
        $('ul.sales-dist-list > li > a[href="' + theAnchor + '"]').click();
    }

    $('dt.office').click(function() {
        return false;
    });

    // New window links
    $('.nw').click(function() {
        var url = $(this).attr('href');
        window.open(url);
        return false;
    });
});

