/*******************************
Conoco Specific Functions
Author: Jack Lukic - KNI
*******************************/

if (typeof (conoco.navigation) == 'undefined') {
    conoco.navigation = {};
}

// Add mobile links
conoco.addMobileLinks = function() {
    if (conoco.section == 'stationfinder') {
        window.location = conoco.config.mobile.redirect;
    }
    $('#links-find a, #links-plan a, #tout-planner a, #content .info a[href=station-finder/]').attr('href', conoco.config.mobile.redirect);
}

// detects mobile browsers
conoco.isMobile = function() {
    var ua = navigator.userAgent;
    return (ua.search(conoco.config.mobile.regex) > -1)
}

// Used to show depressed button continuity between synchronous loads
conoco.navigation.uiContinuity = function() {
    // 20ms delay to avoid frameloss from domReady garbage collection
    setTimeout(function() {
        $('#navigation li .hover, #navigation li .down').fadeOut(conoco.config.animation.quick);
        $('#navigation li.active .down').fadeOut(conoco.config.animation.slow);
        $('#navigation li.active .active').fadeIn(conoco.config.animation.slow);
    }, 20);
}

// Contains global cufon font definitions used on every page
conoco.initGlobalCufon = function() {
    Cufon.replace('#links-register h3', {
        fontFamily: 'Conduit Extra Light'
    });
    Cufon.replace('#subnavigation h3', {
        fontFamily: 'Conduit Medium',
        textShadow: '1px 1px 2px #E24343'
    });
}


// Returns name of current active section
conoco.findSection = function() {
    return $('body').attr('id');
}

// Precache images
conoco.precache = function(images, callback) {
    jQuery.each(images, function(index, src) {
        var image = new Image();
        if (typeof (callback) == 'function') {
            // bind callback to load error and abort, so it always fires
            jQuery(image)
				.bind('load', callback)
				.bind('error', callback)
				.bind('abort', callback)
			;
        }
        image.src = src;
    });
}

// Cycles winner results from CSV
conoco.cycleWinners = function($text) {
    // default ajax options
    var ajaxSettings = {
        type: 'GET',
        url: 'xml/winners.csv',
        dataType: 'text',
        error: function(error) {
            // error handling goes here	
        },
        success: function(resultText) {
            var resultArray = resultText.split(/\n/);
            var results = [];
            // split on comma, replace weird endline identifiers
            jQuery.each(resultArray, function(i, result) {
                results.push(resultArray[i].replace(/\r/, '').split(','));
            });
            var numWinners = results.length;
            var showWinner = function() {
                var randomSeed = Math.floor(Math.random() * (numWinners));
                var randomWinner = results[randomSeed];
                var text = randomWinner[0] + ' ' + randomWinner[1] + '. from ' + randomWinner[2] + ', ' + randomWinner[3];
                if ($.browser.msie) {
                    $text.html(text);
                    setTimeout(function() {
                        showWinner();
                    }, 2000);
                }
                else {
                    $text
						.html(text)
						.fadeIn(400, function() {
						    $(this).removeAttr('style');
						})
						.delay(2000)
						.fadeOut(400, showWinner)
					;
                }
            };
            showWinner();
        }
    };
    $.ajax(ajaxSettings);
}



/*******************************
FLASH FUNCTIONS
*******************************/

// Used for flash to determine when javascript is ready
conoco.isReady = function() {
    return true;
}

// Used for flash externalInterface integration
conoco.externalInterface = function(id) {
    if (id.charAt(0) == '#') {
        id = id.substr(1, id.length);
    }
    if ($.browser.msie) {
        return window[id];
    }
    else {
        return document[id];
    }
}

