﻿//Global Javascript File
var _requiredFlashPlayerVersion = "9";
var _countDownTargetDate;
var _countDownServerDate;
var _countDownIntervalID;

$(document).ready(function() {
    initYouTubeVideo();
    enableCountDownJS();
});

// initialises the YouTube video overall
function initYouTubeVideo() {
    $("a.youtubevideo").colorbox({ iframe: true, innerWidth: 620, innerHeight: 420, loop: false });
}

// inits the image viewviewer on the homepage
function initImageViewer(objectIDToHandle, filmStripPosition, pointerSize, panelWidth, frameWidth, frameHeight) {
    $('#imagePanel,#mediaGallery').removeClass('noJs');

    $(objectIDToHandle).galleryView({
        panel_width: panelWidth,
        panel_height: 300,
        frame_width: frameWidth,
        frame_height: frameHeight,
        filmstrip_position: filmStripPosition,
        transition_speed: 800,
        transition_interval: 4000,
        overlay_opacity: 0.55,
        frame_opacity: 1.0,
        show_captions: false,
        fade_panels: true,
        pointer_size: pointerSize,
        overlay_color: 'white',
        caption_text_color: 'white',
        pause_on_hover: true,
        frame_gap: 5
    });
}

function enableCountDownJS() {
    var targetYear = $("div.countDown div.targetDate span.year").text();
    var targetMonth = parseInt($("div.countDown div.targetDate span.month").text()) -1;
    var targetDay = $("div.countDown div.targetDate span.day").text();
    var targetHour = $("div.countDown div.targetDate span.hour").text();
    var targetMinute = $("div.countDown div.targetDate span.minute").text();

    var serverYear = $("div.countDown div.serverTime span.year").text();
    var serverMonth = parseInt($("div.countDown div.serverTime span.month").text())-1;
    var serverDay = $("div.countDown div.serverTime span.day").text();
    var serverHour = $("div.countDown div.serverTime span.hour").text();
    var serverMinute = $("div.countDown div.serverTime span.minute").text();
    var serverSecond = $("div.countDown div.serverTime span.seconds").text();

    if (targetYear != null && serverYear != null) {
        _countDownTargetDate = new Date(targetYear, targetMonth, targetDay, targetHour, targetMinute, 0);
        _countDownServerDate = new Date(serverYear, serverMonth, serverDay, serverHour, serverMinute, serverSecond);

        if (_countDownTargetDate > _countDownServerDate)
            _countDownIntervalID = setInterval(function() { updateCountDownTimer(); }, 1000);
    }
}

//Update the count down timer every second
function updateCountDownTimer() {
    if (_countDownTargetDate != null && _countDownServerDate != null) {

        _countDownServerDate = new Date(_countDownServerDate.getTime() + 1000);
    
        var timeSpanMillis = GetMillisDifferentBetweenDates(_countDownServerDate, _countDownTargetDate);

        //If time has expired, there is no more need to update the count down panel.
        if (timeSpanMillis <= 0) {
            clearInterval(_countDownIntervalID);
                
            //If count down timer is finished while user is on the page, reload it so that the page link can be seen!
            window.location.reload();
        }

        try {
            $("div.timeContainer .timeDays").text(DaysIn(timeSpanMillis));
            $("div.timeContainer .timeHours").text(HoursIn(timeSpanMillis));
            $("div.timeContainer .timeMinutes").text(MinutesIn(timeSpanMillis));
            $("div.timeContainer .timeSeconds").text(SecondsIn(timeSpanMillis));

            //setting the nouns plural / singular form
            $("div.timeContainer .daysNoun").text(DaysIn(timeSpanMillis) > 1 ? "DAYS" : "DAY");
            $("div.timeContainer .hoursNoun").text(HoursIn(timeSpanMillis) > 1 ? "HOURS" : "HOUR");
            $("div.timeContainer .minutesNoun").text(MinutesIn(timeSpanMillis) > 1 ? "MINUTES" : "MINUTE");
            $("div.timeContainer .secondsNoun").text(SecondsIn(timeSpanMillis) > 1 ? "SECONDS" : "SECOND");
        } catch (e) { }
    }
}

// removes the 'noJs' classe from any element with the specified id
function noJsFix(id) {

    var element = $('#' + id);

    if (element != null && $(element).hasClass('noJs')) {
        $(element).removeClass('noJs');
    }
}

// script to bind the click function for the faq items
function initFaqScript() {

    $('p.faqQuestion').click(function() {

        // find this questions answer div
        var question = $(this);
        var answer = $(this).next('p.faqAnswer');


        // animation to open the faq item
        if ($(this).hasClass('closed')) {
            $(answer).slideDown('fast', function() {
                $(question).removeClass('closed');
                $(question).addClass('open');
            });
        }

        // animation to close the faq item
        if ($(this).hasClass('open')) {
            $(answer).slideUp('fast', function() {
                $(question).removeClass('open');
                $(question).addClass('closed');
            });
        }
    });
}

//Sets the height of all list items to be the same (the maximum height of all of them)
function setListItemsHeight(listElementClassOrID) {
    var list = $(listElementClassOrID);

    if (list != null) {
        var listItems = list.find("li");
        var maxHeight = 0;

        listItems.each(function() {
            var itemHeight = $(this).outerHeight(false); 

            if (itemHeight > maxHeight)
                maxHeight = itemHeight;
        });

        listItems.height(maxHeight);
    }
}

//Sets the height of judgeItem to all the same
function setJudgeItemsHeight() {
        
		var judgeItems = $("div.judgeItem");
        var maxHeight = 250;

        judgeItems.each(function() {
            var itemHeight = $(this).outerHeight(true); 

            if (itemHeight > maxHeight)
                maxHeight = itemHeight;
        });

        judgeItems.height(maxHeight);
}



function initMediaGallery(pageContainerID, overlayContainerID) {
    initPageMediaGallery(pageContainerID);
    initMediaGalleryOverlay(overlayContainerID);

    //When clicking on image on the page, forward event to open appropriate overlay
    $(pageContainerID + " ul li").click(function() {
        var objectChildren = $(this).children("object");

        //If flash, do not forward
        if (objectChildren != null && objectChildren.length > 0) return;

        var imgIndex = $(pageContainerID + " ul li").index($(this));

        $(overlayContainerID + " ul li").eq(imgIndex).find("a").click();
    });
}

function initPageMediaGallery(containerID) {
    //Needs to add the data-url to the object so that the anythingslider can find the videos and wait for videos playing.
    $(containerID + " li > object").each(function() {
        var flashSource = $(this).children("param[name='movie']").attr('value');

        $(this).attr("data-url", flashSource);
    });

    $(containerID + " ul").anythingSlider({
        resizeContents: false,
        expand:true,
        infiniteSlides: false,
        resumeOnVideoEnd: true,
        resumeOnVisible: false,
        hashTags : false,
        addWmodeToObject: 'transparent'
    });

    //Navigation Hover for Sliders
    $(containerID + ' .anythingSlider .arrow').hide();

    $(containerID + ' .anythingSlider').mouseenter(function() {
        $('.arrow').show();
    });
    $(containerID + ' .anythingSlider').mouseleave(function() {
        $('.arrow').hide();
    });
}



function initMediaGalleryOverlay(overlayContainerID) {

    $(overlayContainerID + " ul li img").each(function() {
        var imageSource = $(this).attr('src');
        var containerClass = $(this).parent().parent().parent().parent().parent().attr('class');
        $(this).wrap(
			$('<a/>').attr({
			    'href': imageSource,
			    'rel': 'prettyPhoto['+ containerClass +']'
			})
		);
    });

    $(overlayContainerID + " ul li > object").each(function() {
        var flashSource = $(this).children("param[name='movie']").attr('value');
        var width = $(this).attr("width");
        var height = $(this).attr("height");
        var containerClass = $(this).parent().parent().parent().parent().parent().attr('class');

        var tempEl = $('<a/>').attr({
            'href': flashSource,
            'rel': 'prettyPhoto['+ containerClass +']'
        });

        var parentLi = $(this).parent("li");

        parentLi.append(tempEl);
    });

    $(overlayContainerID + " ul li a").prettyPhoto({
        markup: '<div class="pp_pic_holder"> \
                    <div class="ppt">&nbsp;</div> \
                    <div class="pp_top"> \
                    <div class="pp_left"></div> \
                    <div class="pp_middle"></div> \
                    <div class="pp_right"></div> \
                    </div> \
                    <div class="pp_content_container"> \
                    <div class="pp_left"> \
                    <div class="pp_right"> \
                    <div class="pp_content"> \
                    <div class="pp_loaderIcon"><img src="_colourtrophy/i/global/loading.gif" width="20" /><p>LOADING</p></div> \
                    <div class="pp_fade"> \
                    <a href="#" class="pp_expand" title="Expand the image">Expand</a> \
                    <div id="pp_full_res"></div> \
                    </div> \
                    <div class="pp_hoverContainer"> \
                    <a class="pp_next" href="#">next</a> \
                    <a class="pp_previous" href="#">previous</a> \
                    </div> \
                    <div class="pp_details"> \
                    <a class="pp_close" href="#">Close</a> \
                    </div> \
                    </div> \
                    </div> \
                    </div> \
                    </div> \
                </div> \
                <div class="pp_overlay"></div>',
        gallery_markup: '<div class="pp_gallery"> \
                            <div> \
                            <ul class="popupNav"> \
                            {gallery} \
                            </ul> \
                            </div> \
                        </div>',
		changepicturecallback: function(){ 
		$('.pp_hoverContainer a').show();
		$('.js .pp_hoverContainer').css('display','block');
		}
    });
}

//Hover on list item for drop down menu
sfHover = function() {
	var sfEls = document.getElementById("topMenu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() { 
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() { 
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
