﻿//page settings
var config = {
    activeClass: 'active',
    mainPhotoSelector: '#img-rollover-module .main-option',
    mainFlashSelector: '#img-rollover-module .main-flash',
    mainPhotoOrigSrc: '',
    mainPhotoText: '',
    mainTextSelector: '#img-rollover-module div.desc',
    updateContentOnEvent: '',
    defaultAnalyticsAccount: 'UA-5498038-2',
    updateContentOn: {
        click: function(event) {
            if ($j(this).is('a')) return; //only execute on LI clicks, not embedded links

            event.preventDefault();
            // if there's a link, follow it
            var link = getLinkForOption(this);
            if (!link) {
                location.href = FFIC.Microsites.AppRoot;
            } else if (link && (!isSamePage(link))) {
                location.href = link;
            } else {
                activateOption(this); //just update content
            }

            $j(this).trigger('after-click');
        },
        mouseover: function(event) {
            activateOption(this);

            $j(this).bind('mouseout', function() {
                selectDefaultOption();
            });
        }
    }
}


// cede control of the mighty $ 
$.noConflict();
var $j = jQuery;

$j(function() {
    //save the orig photo src so we can reset it after rollout
    config.mainPhotoOrigSrc = $j(config.mainPhotoSelector).attr('src');
    config.mainPhotoText = $j(config.mainTextSelector).html();

    //get the event type for swapping content (note: asp.net adds a field prefix, so we use 'id$=' to match the end of the ID)
    config.updateContentOnEvent = $j('input[type=hidden][id$=UpdateContentOn]').val();

    //add a class to indicate what type of list this is (click, mouseover, etc.)
    $j('#img-list').addClass(config.updateContentOnEvent);

    $j('#img-rollover-module #img-list li, #img-rollover-module #img-list li a')
        .bind(
            config.updateContentOnEvent,
            config.updateContentOn[config.updateContentOnEvent]
        );

    selectDefaultOption();
});

function activateOption(option) {
    highlight(option);
    updateMainImageFor(option);
}

function highlight(option) {
    clearHighlight();
    $j(option).addClass(config.activeClass);
    $j(option).addClass('read');
}

function clearHighlight() {
    $j('#img-list li').removeClass(config.activeClass);
}

function updateMainImage(src, text, content) {
    if (src) {
        $j(config.mainFlashSelector).hide().empty();
        $j(config.mainPhotoSelector).attr('src', src);
    } else {
	if (!(content && content.attr('file'))) return;
        loadFlashFile(content.attr('file'));
        var pos = $j(config.mainPhotoSelector).position();
        $j(config.mainFlashSelector)
           	.css({ top: pos.top + 'px', left: pos.left + 'px' })
           	.show();
    }

    $j(config.mainTextSelector)
		.empty()
		.append(text);
}

function updateMainImageFor(option) {
    //each option has its own image and associated text within it
    var img = $j('img', option);
    if (img.length == 0) img = $j('div.roll-flash', option);

    var text = img.prev('div').html();
    updateMainImage(img.attr('src'), text, img);
}

function selectDefaultOption() {
    var defaultItem = $j('#img-list li.default');

    if (defaultItem.length == 0)
        defaultItem = $j('#img-list li:first'); //if no default item, select the 1st

    activateOption(defaultItem);
}

function getLinkForOption(option) {
    //var link = $j('a', option).attr('href'); //not specific enough, picks up links in description text
    var link = $j('#' + option.id + ' > p > a').attr('href');
    if (!link) return;

    // get previously selected options
    var arr = $j.makeArray($j('#img-list li.read'));
    var selectedIDs = $j.map(arr, function(item) {
        return item.id.replace('option-', '');
    }).join(',');

    return link + '&r=' + selectedIDs;
}

//is this provided URL the same page as the one we're one (disregarding querystrings)
function isSamePage(url) {
    var ignorePaths = /home\.aspx|default\.aspx/i
    url = url.replace(ignorePaths, '').split('?')[0].toLowerCase();
    var thisPage = location.pathname.replace(ignorePaths, '').split('?')[0].toLowerCase();
    return url == thisPage;
}



function loadFlashFile(file) {
    var vars;
    var bucket = $j('.main-flash');

    if (file.match(/flv$/)) {

        var so = new SWFObject('modules/ImageRollover/Web/flvplayer/player-licensed.swf', 'mpl', bucket.width(), bucket.height(), '9');
        so.addParam('allowfullscreen', 'true');
        so.addParam('allowscriptaccess', 'always');
        so.addParam('wmode', 'opaque');
        so.addVariable('file', file);
	so.addVariable('bufferlength', '10');
	so.useExpressInstall('modules/ImageRollover/Web/flvplayer/expressinstall.swf');
	
	if (file.match(/JackDevlinPAS_720x405\.flv/i)) {
	        so.addVariable('image', 'http://epas.ffido.com/microsite/modules/imageRollover/web/flvplayer/preview.gif');
	} else if (file.match(/CTSR_WhatAgentsAreSaying_720x405/i)) {
		so.addVariable('image', 'http://trackmyrequests.ffido.com/microsite/files/what-agents-are-saying.gif');
	} else if (file.match(/CTSR_Training_GettingStartedIsEasy/i)) {
		so.addVariable('image', 'http://trackmyrequests.ffido.com/microsite/files/getting-started-is-easy.jpg');
	} else if (file.match(/CTSR_WhatAgentsAreSaying_1280x720/i)) {
		so.addVariable('image', 'http://trackmyrequests.ffido.com/microsite/files/what-agents-are-saying.gif');
		so.addVariable('displayclick','fullscreen');
	}
        
        //add the analytics plugin
        so.addVariable('plugins', 'gapro-1');
        so.addVariable('gapro.accountid', 'UA-5498038-2');
        so.addVariable('gapro.trackstarts', 'true');
        so.addVariable('gapro.trackpercentage', 'true');
        so.addVariable('gapro.tracktime', 'true');
        so.addVariable('gapro.trackcompletes', 'true');
        
        //render the video
        so.write(bucket.attr('id'));
        
        //leaving in for now, in case we need to revert quickly
    } else {
        var so = new SWFObject(file, 'mpl', bucket.width(), bucket.height(), '9');
        so.addParam('loop', 'false');
        so.write(bucket.attr('id'));
    }
}


