var projectsHover = {
    sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
    interval: 50, // number = milliseconds for onMouseOver polling interval
    over: slideUp, // function = onMouseOver callback (REQUIRED)
    timeout: 50, // number = milliseconds delay before onMouseOut
    out: slideDown // function = onMouseOut callback (REQUIRED)
};
var cobraNewsListHover = {
     sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
     interval: 50, // number = milliseconds for onMouseOver polling interval
     over: slideUpSmall, // function = onMouseOver callback (REQUIRED)
     timeout: 50, // number = milliseconds delay before onMouseOut
     out: slideDownSmall // function = onMouseOut callback (REQUIRED)
};

function getNews( id, amount, image, hoverId ) { }

function getCobraNewsList() {
    $.post( '/news/getNews/' + amount + '/' + image + '/' + Math.random(), {}, function( data ) {
        $( '#' + id ).html( data );
        $( '.cobraNewsList li a' ).hoverIntent( cobraNewsListHover );
        setTimeout( 'getNews("' + id + '","' + amount + '","' + image + '", );', 20000 );
    } );
}

function getCobraHomepageSplash() {
    $.post( '/news/getNews/1/homepageCobra/' + Math.random(), {}, function( data ) {
        element2.html( data );
        setTimeout( 'getCobraHomepageSplash( element2 );', 20000 );
    } );
}

function getAppcoNewsList() {
    $.post( '/news/getNews/6/thumbs/' + Math.random(), {}, function( data ) {
        element3.html( data );
        setTimeout( 'getAppcoNewsList( element3 );', 20000 );
    } );
}

function getAppcoHomepageSplash() {
    $.post( '/news/getNews/1/homepageAppco/' + Math.random(), {}, function( data ) {
        element4.html( data );
        setTimeout( 'getAppcoHomepageSplash( element4 );', 20000 );
    } );
}

function getMembersFeatured() {
    $.post( '/news/getNews/14/main|2|thumbsWide|12/' + Math.random(), {}, function( data ) {
        $( '#tempHidden' ).html( data );
        vars = $( '#tempHidden li' );
        var count = 1;
        $( '#double_feature' ).html( '' );
        $( '#dirty_dozen' ).html( '' );
        $.each( vars, function() {
            if ( count <= 2 ) $( '#double_feature' ).html( $( '#double_feature' ).html() + '<li>' + $( this ).html() + '</li>' );
            else $( '#dirty_dozen' ).html( $( '#dirty_dozen' ).html() + '<li>' + $( this ).html() + '</li>' );
            count++;
        } );
        $( '#dirty_dozen li a' ).hoverIntent( cobraNewsListHover );
        setTimeout( 'getMembersFeatured();', 20000 );
    })
}

function getMembersNewsList() {
    $.post( '/news/getNews/2/main/' + Math.random(), {}, function( data ) {
        $( '#double_feature' ).html( data );
        setTimeout( 'getMembersFeatured();', 20000 );
    } );
}

function rotateImages( parentId ) {
    var currentImage = $( '#' + parentId + ' li.on' );
    var nextImage = currentImage.next();
    currentImage.removeClass( 'on' ).addClass( 'hide' );
    if ( nextImage.length > 0 ) nextImage.addClass( 'on' ).removeClass( 'hide' );
    else $( '#' + parentId + ' li:first' ).addClass( 'on' ).removeClass( 'hide' );
    setTimeout( 'rotateImages("' + parentId + '");', 10000 );
}

$( document ).ready( function() {

    // What we do and who we are rotating images
    if ( $( '#whatWeDoImages' ).length > 0 ) setTimeout( 'rotateImages( "whatWeDoImages" ); rotateImages( "whoWeAre" );', 10000 );

    if ( $( '#js-news' ).length > 0 ) $( '#js-news' ).ticker();

    $( '.membersNewsList li a' ).hoverIntent( cobraNewsListHover );

    if ( $( '.cobraNews' ).length > 0 ) $( '.cobraNews li a' ).hoverIntent( cobraNewsListHover );

    // See more news
    if ( $( '.seeMoreNews' ).length > 0 ) {
        $( '.seeMoreNews li').hide().addClass( 'hidden' );
        $( '.seeMoreNews li').eq( 0 ).show().removeClass( 'hidden' );
        $( '.seeMoreNews li').eq( 1 ).show().removeClass( 'hidden' );
        $( 'a#seeMore' ).click( function() {
            $( this ).fadeOut();
            var hidden = $( '.seeMoreNews li.hidden' );
            $.each( hidden, function() { $( this ).removeClass( 'hidden' ).delay( 50 ).fadeIn(); } );
        } );
    }

    // Main navigation, sub nav roll over images
    if ( $( '#nav li.sub div.dropdown a' ).length > 0 ) {
        $( '#nav li.sub div.dropdown a' ).hover(
            function() {
                src = $( '.imageArea:visible' ).attr( 'src' );
                if ( $( this ).attr( 'name' ) != '' ) $( '.imageArea:visible' ).attr( 'src', '/uploads/resources/images/navigation/' + $( this ).attr( 'name' ) );
            },
            function() { $( '.imageArea:visible' ).attr( 'src', src ); }
        );
        $( '#nav li.sub' ).click( function() { if ( $( this ).child( 'div' ).is( ':visible' ) ) return false; } );
    }

    // Homepage Splash boxes
    if ( $( '#homepageSplash #whoWeAre' ).length > 0 ) $( '#homepageSplash #whoWeAre, #homepageSplash #whatWeDo' ).hoverIntent( projectsHover );

    if ( $( '#nav' ).length > 0 ) {
        $( '#nav li.sub a' ).addClass( 'arrow' );
        $( '#nav div.surround' ).css( 'opacity', 0.5 );
    }

    // Main navigation drop downs
    $( '#nav ul li.sub' ).hover(
        function() {
            var lis = $( 'div.dropdown ul li', this ).length;
            lis = Math.ceil( lis/2 );
            $( 'div.dropdown', this ).css( 'height', ( ( lis * 30 ) + 120 ) + 'px' ).show();
            $( 'div.surround', this ).css( 'height', ( ( lis * 30 ) + 125 ) + 'px' ).show();
            $( this ).addClass( 'on' );
        },
        function() {
            if ( $( 'div.dropdown:visible', this ).length > 0 ) {
                $( 'div.dropdown', this ).hide();
                $( 'div.surround', this ).hide();
            }
            $( '#nav ul li.on' ).removeClass( 'on' );
        }
    );

    $( '#nav ul li.sub div.dropdown' ).blur( function() {
        $( this ).hide();
        $( this ).parent().removeClass( 'on' );
    } );

    if ( $( '#newsGallery' ).length > 0 ) {
        $( 'li.galleryVideo a' ).click( function() {
            $( '#main_image' ).hide();
            var files = $( this ).attr( 'rel' ).split( '___' );
            jwplayer( 'player' ).setup( {
                flashplayer: '/flash/player.swf',
                file: '/uploads/resources/videos/' + files[0],
                image: '/uploads/resources/images/main/' + files[1],
                width: 450,
                height: 320
            } );
            $( '#playerArea' ).show();
        } );

        $( '.videoImage img' ).click( function() {
            $( '#playerArea' ).hide();
            $( '#main_image' ).attr( 'src', '/uploads/resources/images/main/' + $( this ).attr( 'rel' ) ).show();
        } );
    };

    if ( $( '#main_image' ).length > 0 && $( '#main_image' ).attr( 'src' ) ) $( '#main_image' ).show();

    if ( $( '.changeMe' ).length > 0 ) changeLogo();

    // Admin page URL
    if ( $( '#circleOfFlags').length > 0 ) {
        $( '#circleOfFlags img' ).css( 'opacity', 0 ).css( 'display', 'block' );

        $( '#flag_usa' ).animate( { 'top': 0, 'left': 464, 'opacity': 0.6 }, 200 ); // first
        $( '#flag_australia' ).animate( { 'top': 0, 'left': 580, 'opacity': 0.6 }, 200 );
        $( '#flag_belgium' ).animate( { 'top': 0, 'left': 690, 'opacity': 0.6 }, 250 );
        $( '#flag_brazil' ).animate( { 'top': 0, 'left': 800, 'opacity': 0.6 }, 300 ); // top right
        $( '#flag_canada' ).animate( { 'top': 110, 'left': 800, 'opacity': 0.6 }, 350 );
        $( '#flag_germany' ).animate( { 'top': 220, 'left': 800, 'opacity': 0.6 }, 400 );
        $( '#flag_greece' ).animate( { 'top': 330, 'left': 800, 'opacity': 0.6 }, 450 );
        $( '#flag_hongkong' ).animate( { 'top': 440, 'left': 800, 'opacity': 0.6 }, 500 );  // bottom right
        $( '#flag_india' ).animate( { 'top': 440, 'left': 700, 'opacity': 0.6 }, 550 );
        $( '#flag_indonesia' ).animate( { 'top': 440, 'left': 600, 'opacity': 0.6 }, 600 );
        $( '#flag_ireland' ).animate( { 'top': 440, 'left': 500, 'opacity': 0.6 }, 650 );
        $( '#flag_italy' ).animate( { 'top': 440, 'left': 400, 'opacity': 0.6 }, 700 );
        $( '#flag_japan' ).animate( { 'top': 440, 'left': 300, 'opacity': 0.6 }, 750 );
        $( '#flag_malaysia' ).animate( { 'top': 440, 'left': 200, 'opacity': 0.6 }, 800 );
        $( '#flag_netherlands' ).animate( { 'top': 440, 'left': 100, 'opacity': 0.6 }, 850 );
        $( '#flag_newZealand' ).animate( { 'top': 440, 'left': 0, 'opacity': 0.6 }, 900 ); // bottom left
        $( '#flag_philippines' ).animate( { 'top': 330, 'left': 0, 'opacity': 0.6 }, 950 );
        $( '#flag_poland' ).animate( { 'top': 220, 'left': 0, 'opacity': 0.6 }, 1050 );
        $( '#flag_singapore' ).animate( { 'top': 110, 'left': 0, 'opacity': 0.6 }, 1100 );
        $( '#flag_spain' ).animate( { 'top': 0, 'left': 0, 'opacity': 0.6 }, 1150 ); // top left
        $( '#flag_sweden' ).animate( { 'top': 0, 'left': 115, 'opacity': 0.6 }, 1200 );
        $( '#flag_thailand' ).animate( { 'top': 0, 'left': 230, 'opacity': 0.6 }, 1250 );
        $( '#flag_uk' ).animate( { 'top': 0, 'left': 345, 'opacity': 0.6 }, 1300 ); // last

        selectFlag( 'uk' );
        $( '#circleOfFlags img' ).click( function() {
            var id = $( this ).attr( 'id' ).split( '_' );
            selectFlag( id[1] );
        } );
    }

    // Members only section
    if ( $( '#memberIcons' ).length > 0 ) $( '#memberIcons li a' ).hover( function() { $( this ).animate( { opacity: 1 }, 400 ); }, function(){ $( this ).animate( { opacity: 0 }, 200 ); } );

    // User details
    if ( $( '.userDetail' ).length > 0 ) $( '.userDetail' ).prettyPhoto();

    // Search feature
    if ( $( '.criteria' ).length > 0 ) {
        $( '.criteria' ).click( function() {
            if ( $( this ).hasClass( 'dateCriteria' ) ) $('.dateCriteria').removeClass('on');
            if ( $( this ).hasClass( 'on' ) ) $( this ).removeClass( 'on' );
            else $( this ).addClass('on' );
            narrowDownSearch();
        } );
        return false;
    }

    // alerts
    var $alert = $( '#alert' );
    if ( $alert.length ) {
        var alerttimer = window.setTimeout( function () { $alert.trigger( 'click' ); }, 3000 );
        $alert.animate( { height: $alert.css( 'line-height' ) || '50px' }, 200 ).click( function () {
            window.clearTimeout( alerttimer );
            $alert.animate( { height: '0' }, 200 );
        } );
    }

} );

function narrowDownSearch() {
    $( '#searchResults li' ).hide();
    var criteria = $( '.searchCriteria .on' );
    $( '.searchCriteria .on' ).each( function() {
        var vars = $( this ).attr( 'id' ).split( '_' );
        var key = vars[0];
        var value = vars[1];
        if ( key == 'date' ) {
            var d = new Date();
            switch ( value ) {
                case 'today':
                    value = d.getFullYear() + '-' + ( d.getMonth() + 1 ) + '-' + d.getDate();
                    $( '.' + value ).show();
                    break;

                case 'yesterday':
                    d = new Date( d - 86400000 );
                    value = d.getFullYear() + '-' + ( d.getMonth() + 1 ) + '-' + d.getDate();
                    $( '.' + value ).show();
                    break;

                case '7':
                    d = new Date.UTC( d.getFullYear(), d.getMonth(), d.getDate() );
                    var temp = new Date();
                    for ( i = 1; i <= 7; i++ ) {
                        temp = new Date( d - ( 86400000 * i ) );
                        value = temp.getFullYear() + '-' + pad( ( temp.getMonth() + 1 ), 2 ) + '-' + pad( temp.getDate(), 2 );
                        $( '.' + value ).show();
                    }
                    break;

                case '30':
                    d = new Date.UTC( d.getFullYear(), d.getMonth(), d.getDate() );
                    var temp = new Date();
                    for ( i = 1; i <= 30; i++ ) {
                        temp = new Date( d - ( 86400000 * i ) );
                        value = temp.getFullYear() + '-' + pad( ( temp.getMonth() + 1 ), 2 ) + '-' + pad( temp.getDate(), 2 );
                        $( '.' + value ).show();
                    }
                    break;

                default: $( '[class*=' + value + ']' ).show();

            }
        } else $( '.' + value ).show();
    } );

    if ( criteria.length < 1 ) $( '#searchResults li' ).show();

}

function daysInMonth( iMonth, iYear ) { return 32 - new Date( iYear, iMonth, 32 ).getDate(); }

function changeLogo() {
    li = $( '#footer_company_logos li:random' );
    as = $( 'a.hide:random', li );
    $( 'a.changeMe', li ).fadeOut( 800 );
    setTimeout( 'as.fadeIn( 800 )', 800 );
    setTimeout( '$( "a.changeMe", li ).removeClass( "changeMe" ).addClass( "hide" );',1600 );
    setTimeout( 'as.removeClass( "hide" ).addClass( "changeMe" );', 1600 );
    setTimeout( 'changeLogo();', 4000 );
}

function selectFlag( id ) {
    var index = $( '#circleOfFlags *' ).index( $( '#flag_' + id ) );
    var next = index + 1 == 21 ? 0 : index + 1;
    var prev = index - 1 < 0 ? 20 : index - 1;

    if ( $( '#flag_' + id ).css( 'width' ) == '60px' ) {
        $( '#circleOfFlags div' ).hide();
        $( '#circleOfFlags div#info_' + id ).fadeIn( 500 ); // Fade in content area for specific flag
        $( '#flag_' + id ).animate( { 'opacity': 1, 'width': '80px', 'height': '68px' }, 200 ); // Show selected flag
        $( '#circleOfFlags img' ).eq( next ).animate( { 'opacity': 0.7, 'width': '50px', 'height': '42px' }, 200 ); // side flags
        $( '#circleOfFlags img' ).eq( prev ).animate( { 'opacity': 0.7, 'width': '50px', 'height': '42px' }, 200 ); // side flags

        next++;
        prev--;

        if ( next == 21 ) next = 0;
        if ( prev < 0 ) prev = 20;
        $( '#circleOfFlags img' ).eq( next ).animate( { 'opacity': 0.6, 'width': '48px', 'height': '40px' }, 100 ); // Hide
        $( '#circleOfFlags img' ).eq( prev ).animate( { 'opacity': 0.6, 'width': '48px', 'height': '40px' }, 100 ); // Hide
    } else if ( $( '#flag_' + id ).css( 'width' ) != '80px' ) {
        $( '#circleOfFlags div' ).hide();
        $( '#circleOfFlags div#info_' + id ).fadeIn( 500 ); // Fade in content area for specific flag
        $( '#circleOfFlags img' ).animate( { 'opacity': 0.6, 'width': '48px', 'height': '40px' }, 100 ); // Hide
        $( '#flag_' + id ).animate( { 'opacity': 1, 'width': '80px', 'height': '68px' }, 200 ); // Show selected flag
        $( '#circleOfFlags img' ).eq( next ).animate( { 'opacity': 0.7, 'width': '50px', 'height': '42px' }, 200 ); // side flags
        $( '#circleOfFlags img' ).eq( prev ).animate( { 'opacity': 0.7, 'width': '50px', 'height': '42px' }, 200 ); // side flags
    }
}

function slideUp() { $( '.bg, .content', this ).animate( { top: '20px' }, 500 ); }

function slideUpSmall(){ $( '.bg, .content', this ).animate( { top: '0' }, 500 ); }

function slideDownSmall(){ $( '.bg, .content', this ).animate( { top: '62px' }, 300 ); }

function slideDown(){ $( '.bg, .content', this ).animate( { top: '125px' }, 300 ); }

function pad( number, length ) {
    var str = '' + number;
    while ( str.length < length ) str = '0' + str;
    return str;
}

Object.size = function( o ) {
    var s = 0, k;
    for ( k in o ) if ( o.hasOwnProperty( k ) ) s++;
    return s;
}
Object.getKey = function( o, v ) {
    for ( var k in o ) if ( o[ k ] == v ) return k;
    return false;
}
Object.getValue = function( o, k ) {
    if ( typeof o[ k ] != 'undefined' ) return o[ k ];
    return false;
}

