/* Authors: Paul & James @ vcom */
/* Site:    750mph */

$(document).ready( function(){
    // Our own functions that are needed on page load and after content is AJAX'd in 
    //  which is filtered via innerShiv, so we need to make JS calls here.
    jQuery_Setup_Page();    
});

function jQuery_Setup_Page()
{
    jQuery_Setup_HomepageMenu()
    jQuery_Setup_MenuAction();
    jQuery_Setup_VideoCycle();
    // jQuery_Setup_ContactVideo();
    jQuery_AJAXCall_Ratecard(); 
}

function jQuery_Setup_Page_iPad()
{
    $('a#videoPlay').click(function(e) { 
        e.preventDefault();
        jQuery_Video_Show_iPad();
        return false;
    });
}

function jQuery_Video_Show_iPad()
{
    if (isiPad())
    {   
        if ($('#playNote'))
            $('#playNote').remove();
       
       if ($('#video_background_image').length == 0)
       {
           //$('#video_background_image').remove();

           // Bring video back on to page       
           $('#video_background_container').css({'width':'100%', 'overflow': 'inherit', '-webkit-transform':'translateX(0px)'});

           // Find video element
           var el_Video = document.getElementsByTagName('video')[0];

           if (el_Video.hasAttribute("controls"))
               el_Video.removeAttribute("controls");

            el_Video.play();
       }
    }
}

function jQuery_Video_Hide_iPad()
{
    if (isiPad())
    {
        if ($('#playNote'))
            $('#playNote').remove();
    } 
}

function jwPlayer_PauseBackgroundVideo()
{
    setTimeout("jwplayer('video_background_player').pause(true);", 5000);
}

window.onorientationchange = function(){

    var orientation = window.orientation;

    // Look at the value of window.orientation:

    if (orientation === 0 || orientation === 180){
        // iPad is in Portrait mode.
        jQuery_Video_Hide_iPad();
    }
    else if (orientation === 90 || orientation === -90){
        // iPad is in Landscape mode. The screen is turned to the left / right.
        if ($('#ajax_Container') && $('#ajax_Container').children().length == 0) 
        {
             window.location.reload();
        }
        jQuery_Video_Show_iPad();
    }
}


function jQuery_Setup_HomepageMenu()
{
    // Fade hovers on menu links were only added to improve the sophistication of the site.
    // Performance is an issue. Kill and reinstate basic css hovers if you think...

    // Hover fade for front page
    // N.B. Needs to be different from above because homepage is set with opacity
    // fonts with opacity rendered more poorly in small font size
    
    if(!$('html').hasClass("ie7")){
        if ($('#ajax_Container').children().length == 0) 
        {
            $("#Mainmenu").delay(1500).fadeIn('normal');
        }
        $('#Mainmenu ul li a').hover(function() {
                $(this).stop().animate({"opacity": '1'}, 'fast');
            },function(){
                $(this).animate({"opacity": '0.5'}, 'fast');
        });
    } else {
        if ($('#ajax_Container').children().length == 0) 
        {
            $('#Mainmenu').show();
        }
    }
}

function jQuery_Setup_MenuAction()
{
    // Hover fade on inner menu
    var menuColourNormal = 'rgb(125, 125, 125)';
    var menuColourHover = 'rgb(255, 255, 255)';

    $("ul#innernav li a").unbind('hover');
    $("ul#innernav li a").hover(function() {
        $(this).stop().animate({"color": menuColourHover}, 'fast');
    },function(){
        $(this).stop().animate({"color": menuColourNormal}, 'fast');
    });

    // Hides each section and brings in requested
    $("ul.menu-main li a").unbind('click');
    $('ul.menu-main li a').click(function(e) {
        
        var el_HREF = this;
        // Setup the history for the AJAX pages
        var History = window.History;

        $(window).bind('statechange',function(){
            var State = History.getState();
        });
        
        if (isiPad())  { 
            //return true;
        }
        
        if ($(this).hasClass('ajax'))
        {     
            if ($('#ajax_Container').length == 0
            || ($(el_HREF).attr("href") == "/showreel" && $('html').hasClass("ie7")))
            {
                return true;  
            } else {
                    
                e.preventDefault();
                // If menu is on the page
                if ($('#Mainmenu').children().length > 0)
                {
                    if (isiPad()) 
                    {
                        $('#Mainmenu').hide();
                        jQuery_PageSwipe($(el_HREF).attr("href"));    
                    } else {
                        $('#Mainmenu').fadeOut('slow', function() {
                            jQuery_PageSwipe($(el_HREF).attr("href"));
                        });
                    }
                } else {                    
                    jQuery_PageSwipe($(el_HREF).attr("href"));
                }
            }
            return false;
        } else if ($(this).hasClass('home')) {
            
            if ($('#ajax_Container').children().length > 0)
            {   
                e.preventDefault();
                
                $('#ajax_Container .container').fadeOut('slow' , function() {
                    $('#ajax_Container').delay(500).animate({'margin-left':('-'+$(window).width())}, "normal", function() {
                        // Reset the content
                        $('#ajax_Container').empty();

                        $("#Mainmenu").delay(500).fadeIn(1250);

                        if (isiPad()) 
                        {
                            jQuery_Video_Show_iPad();
                        } else { 
                            jwplayer("video_background_player").play(true);
                        }
                    });    
                });
                return false;
            } else {
                return true;
            }
        } else {
            //return true;
        }
    });
}

function jQuery_PageSwipe(str_URL)
{
    if ($('#ajax_Container .container').is(':visible'))
    {
        $('#ajax_Container .container').hide();
        $('<div></div>', {id: 'ajax_Loader'}).appendTo('#ajax_Container').show();
    }
    
    $.ajax({
        type    : "GET",
        cache   : false,
        dataType: "html",
        url     : str_URL,
        success: function(responseHTML) {
            if ($('#ajax_Container').children().length > 0)
            {             
                $('#ajax_Container').html(innerShiv(responseHTML));
                if ($('#ajax_Loader'))
                {
                    $('#ajax_Loader').fadeOut('fast', function() { 
                        $(this).remove();
                        $('#ajax_Container .container').fadeIn('slow');    
                        
                    });
                } else {
                    $('#ajax_Container .container').fadeIn('slow');    
                }
            } else {
                $('#ajax_Container').html(innerShiv(responseHTML));
                jQuery_PageShow(500);
            }
            // Do these here since the innerShiv breaks any JS calls from the views
            jQuery_Setup_Page();
            
            if (!isiPad())
            {
                jwplayer("video_background_player").pause(true);
            }

        }
    });
   
}

function jQuery_Setup_PageHasContent(int_Delay)
{
    if ($('#Mainmenu'))
    {
        $('#Mainmenu').fadeOut('slow', function() {
            jQuery_PageShow(int_Delay);
        });
    } else {
        jQuery_PageShow(int_Delay);
    }
}

function jQuery_PageShow(int_Delay)
{
    $('#ajax_Container .container').hide();
   
    $('#ajax_Container')
        .stop(true, true)
        .delay(int_Delay)
        .css({'display':'block', 'margin-left': $(window).width()})
        .animate({'margin-left': '0px'}, 'normal', function() {
            $('#ajax_Container .container').fadeIn('slow'); 
    });
    
}

// Callback request handler
function jQuery_AJAXCall_Ratecard()
{
    str_FormName 	= "form#form_Ratecard";
    str_ResultContainer = "#ajax_Container";
    jQuery_AJAXCall(str_FormName, str_ResultContainer);
}

function jQuery_AJAXCall(str_FormName, str_ResultContainer)
{
    $(str_FormName).bind("submit", function() {
        var str_Action = $(this).attr("action");
        $.ajax({
            type    : "POST",
            cache   : false,
            url     : str_Action,
            data    : $(this).serializeArray(),
            success : function(response, status) {
                $(str_ResultContainer).hide(); 
                $(str_ResultContainer).html(innerShiv(response));
                $(str_ResultContainer).show();
                jQuery_Setup_Page();
            }
        });
        return false;
    });    
}

function jQuery_Setup_VideoCycle()
{
    if ($('.videolist') && $('.videolist').length > 0) 
    {
        // The hash is used to keep check on which slide was being viewed
        var index = 0, hash = window.location.hash;
        if (hash) {
            index = /\d+/.exec(hash)[0];
            index = (parseInt(index) || 1) - 1; // slides are zero-based
        }
     
        $('#cyclePrev').hide();
        jQuery_Setup_VideoHover(index);
     

        $('.videolist').cycle({
            timeout : 0, 
            fx      : 'scrollHorz',
            speed   : 'slow',
            nowrap  : 1,
            prevNextEvent: 'click',
            prev    : '#cyclePrev',
            next    : '#cycleNext',
            startingSlide: index,
            after   : VideoCycle_onAfter
        });
    }
}
 
function VideoCycle_onAfter(curr, next, opts) 
{
   
    if(!$('html').hasClass("ie7") && !isiPad())
    {
        // Setup the history for the AJAX pages
        var History = window.History;
        History.disableSuid = true;
        var State = History.getState();
        var url = State.url;

        // Get the current window location without a hash (should one be set)
        var cleanURL = url.split("#");

        // Add the hash to indicate the page slide
        url = "/showreel#"+ (opts.currSlide + 1);

        // Add to the history
        History.pushState(null, "Showreel #"+ (opts.currSlide) +" - 750mph", url);
    }
    
    var index = opts.currSlide;
    if (isiPad())
    {
        funcShow = 'show';
        funcHide = 'hide';
    } else {
        funcShow = 'fadeIn';
        funcHide = 'fadeOut';
    }
    
    $('#cyclePrev')[index == 0 ? funcHide : funcShow]();
    $('#cycleNext')[index == opts.slideCount - 1 ? funcHide : funcShow]();   
    
    jQuery_Setup_VideoHover(opts.currSlide + 1);
}

function jQuery_Setup_ContactVideo()
{
    if ($("#video_player_contact") && $("#video_player_contact").length > 0)
    {
        jwplayer("video_player_contact").setup({
            flashplayer: "/scripts/jwplayer/player.swf",
            file: "http://cdn1.chillibean.net/perl/media-1.2.pl?A=710031;K=HlV7IDyhhPCPLgIh2585RBbHh45WtiZvHKbhpy1Y5I1PqFgAnpDn9rglLW8tkwTFmMoojuCKJryyFfIbGLFDTWD61ZHvdM2bhFl5DrEbhvUGsMgRdAVEKabtOcGLkQ9FCPOsNh7ofdiu4f137o3636oqpp04434r26n5n71082n5r4866428",
            provider: "video",
            skin:"/scripts/jwplayer/skins/nacht.zip",
            autostart: true,
            stretching: "fill",
            width: 574,
            height: 292,
            icons: false,
            shownavigation: false,
            controlbar: "none",
            type: "video",
            events : {
                onPlay: function() {
                    jQuery_Show_ContactMapImage();
                },
                onComplete: function() {
                    jQuery_Hide_ContactMapVideo();
                }
            }
        });
        
        setTimeout("$('img#video_contact').removeClass('hidden');", 800);
        
    }
}
    
function timeHandler(object) 
{
    // 10 seconds
    if (object.position >= 0.8) 
    {
        jQuery_Show_ContactMapImage();
        $("div#video_player_contact_wrapper").remove();
    }
}

function jQuery_Show_ContactMapImage()
{
    $("img#video_contact").removeClass("hidden");
}

function jQuery_Hide_ContactMapVideo()
{
    $("div#video_player_contact_wrapper").remove();
}


function jQuery_Setup_VideoHover(currSlide)
{

    /*    $('#videolist-group-'+ currSlide +' a').click(function(e) {
            if ($('img.video-play', this).hasClass('hidden'))
            {
                $('img.video-play', this).removeClass('hidden');
                //e.preventDefault();
                return false;
            } else {
                return true;
            }
        });
        */
    
    
    
    $('#videolist-group-'+ currSlide +', #videolist-group-'+ currSlide +' img.video-preview, #videolist-group-'+ currSlide +' img a').show();
    $('#videolist-group-'+ currSlide +' a img').unbind('hover');
    $('#videolist-group-'+ currSlide +' a img').each(function() { 
        $(this).hover(function() {
            var current = this;
            var content = "#"+$(this).attr('longdesc');
            var el_ParentA = $(this).parent();
            var el_ParentGroup = $(this).parent().parent();
            $('div.video-detail:visible').hide();
            $(content).stop(true, true).fadeIn("fast");
            $(this).fadeTo('fast', 1);
            $('a img.video-preview', el_ParentGroup).each(function(index) {
                if (current != this)
                {
                    $(this).stop(true,true).fadeTo('fast', 0.3);
                }
            });
            
            if (isiPad() && $('img.video-play', el_ParentA).hasClass('hidden'))
            {
                $('img.video-play', el_ParentA).removeClass('hidden');
            }

            
          },
          function () {
            var current = this;
            var content = "#"+$(this).attr('longdesc');
            $(content).stop(true, true).hide();
            var el_ParentGroup = $(this).parent().parent();

            $('a img.video-preview', el_ParentGroup).each(function(index) {
                if (current != this)
                {
                    $(this).stop(true,true).fadeTo('fast', 1);
                }
            });
            if (isiPad())
            {
                $('img.video-play', el_ParentGroup).addClass('hidden');
            }
          }
        );
    });
}


function isiPad()
{
    return (navigator.platform.indexOf("iPad") != -1);
}

function isiPhone()
{
    return (
        //Detect iPhone
        (navigator.platform.indexOf("iPhone") != -1) ||
        //Detect iPod
        (navigator.platform.indexOf("iPod") != -1)
    );
}

﻿if (document.documentElement.attachEvent)
    document.documentElement.attachEvent('onmousedown',function(){
        event.srcElement.hideFocus=true
});
