﻿var textclass = ".main, .subnav, .accesibility, .foot";

$(document).ready(function() {

    //set fontsize from cookie
    checkTextSizeCookie();
    setUpResizers();


});

/* http://www.w3schools.com/JS/js_cookies.asp */
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ";path=/" +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function checkTextSizeCookie() {
    size = getCookie('IPMG_text_size');
    if (size != null && size != "") {
        $(textclass).each(function() {
            $(this)[0].style.fontSize = size;
        });
    }
}
var noFlash = false;
function showintro() {
    $(".flashintro").show();

    //setup our flash intro
    $('.flashintro').flash(
        { src: 'flash/IPMediaIntrov2.swf',
            width: 964,
            height: 540,
            loop: false,
            quality: 'high'
        },
        null,
        null,
        function() { //callback if no flash - show animations sooner
            noFlash = true;
            $(".flashintro").hide();
            $(".main h1").fadeIn(1000);
            setTimeout(function() { rotateHomeImgs(4000, 2500); }, 1500);
        }
     );
     
    //ensuring we have flash, run delayed animations
    if (!noFlash) {
        setTimeout(function() { $(".flashintro").fadeOut("fast"); }, 3500);
        setTimeout(function() { $(".main h1").fadeIn(2000); }, 3500);
        setTimeout(function() { rotateHomeImgs(4000, 2500); }, 5000);
    }
}

function setUpResizers() {
    $("#textSmaller").click(function() {
        if ($(textclass)[0].style.fontSize) {
            var currentSize = $(textclass)[0].style.fontSize;
            currentSize = currentSize.match(/([0-9]+)/);
            currentSize = (Number(currentSize[0]) - 10) + "%"
        }
        else {
            var currentSize = "90%"
        }
        $(textclass).each(function() {
            $(this)[0].style.fontSize = currentSize;
        });
        setCookie('IPMG_text_size', currentSize, 9999)
        return false;
    });
    $("#textLarger").click(function() {
        if ($(textclass)[0].style.fontSize) {
            var currentSize = $(textclass)[0].style.fontSize;
            currentSize = currentSize.match(/([0-9]+)/);
            currentSize = (Number(currentSize[0]) + 10) + "%"
        }
        else {
            var currentSize = "110%"
        }
        $(textclass).each(function() {
            $(this)[0].style.fontSize = currentSize;
        });
        setCookie('IPMG_text_size', currentSize, 9999)
        return false;
    });
}
var img = 0;

function rotateHomeImgs(delayBetweenRotations, fadeDuration) {

    if (img == 0) {
        $("#iamrow").fadeIn(fadeDuration);
        $("#wtrrow").fadeOut(fadeDuration, function() {
            setTimeout(function() {
                rotateHomeImgs(delayBetweenRotations, fadeDuration);
            }, delayBetweenRotations);
        });
        img = 1;
    } else {
        $("#iamrow").fadeOut(fadeDuration);
        $("#wtrrow").fadeIn(fadeDuration, function() {
            setTimeout(function() {
                rotateHomeImgs(delayBetweenRotations, fadeDuration);
            }, delayBetweenRotations);
        });
        img = 0;
    }
}