﻿/*!
 * Optiek van de Vecht scripts
 * Copyright (c) 2009-2010 B. Sumter
 * Version: 1.0 (01-JUN-2010)
 */


// Variables
var lastAnchor = "";
var lastTemplate = "";
var templates = new Array();
var isAnimating = false;
var menuItems = new Array();
var isDemoMode = false;

$(document).ready(function() {
    // Get the templates
    $.get('content/1.html', function(data) {
        // Set the templates
        templates[1] = data;

        $.get('content/2.html', function(data) {
            // Set the templates
            templates[2] = data;

            $.get('content/3.html', function(data) {
                // Set the templates
                templates[3] = data;

                // Initializes the hover menu
                initializeHoverMenu();

                // Initializes the timers
                initializeTimers();
            });
        });
    });
});

function initializeHoverMenu() {
    $('#menu li').hover(
        function() {
            $("ul", this).slideDown('fast');
        },
	    function() {
	        $("ul", this).slideUp('fast');
	    });

    $('#menu li ul li a').click(function() {
        $('#menu li ul').slideUp('fast');
    });

    $('#menu a').each(function() {
        menuItems.push($(this).attr('href'));
    });

    $('#menu li:first').addClass('first');
    $('#menu li:last').addClass('last');
    $('#menu li:first a').addClass('first');
    $('#menu li:last a').addClass('last');

    // Initalize the light box
    $('#slideshow img').lightBox();
}

function initializeTimers() {
    setInterval('slideSwitch()', 5000);
    setInterval('checkAnchor()', 200);
    setInterval('pageSwitch()', 10000);
}

function initializeCycle() {
    $('#main').cycle({
        fx: 'scrollUp',
        timeout: 0,
        easing: 'easeInOutBack',
        before: onBefore,
        after: onAfter
    });
}

function onBefore() {
    isAnimating = true;
    $(this).parent().find('.activeSlide').removeClass('activeSlide');
}

function onAfter() {
    isAnimating = false;
    $(this).addClass('activeSlide');
}

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ($active.length == 0) $active = $('#slideshow IMG:last');
    var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first');

    $active.addClass('last-active');
    $next.css({ opacity: 0.0 })
                .addClass('active')
                .animate({ opacity: 1.0 }, 1000, function() {
                    $active.removeClass('active last-active');
                });
}

function pageSwitch() {
    if (isDemoMode) {
        document.location = 'index.html' + menuItems[Math.floor(Math.random() * menuItems.length)] + '&demo';
    }
}

function checkAnchor() {

    if (isAnimating) {
        return;
    }

    var url = document.location.toString();
    // look at the URL to see if it contains an anchor
    if (url.match('#')) {

        // if the demo tag is 
        var queryString = url.split('#')[1];
        isDemoMode = queryString.indexOf('&demo') != -1;

        // if so, get it from the URL
        var urlAnchor = queryString.split('&')[0].toLowerCase();
        if (lastAnchor != urlAnchor) {
            // Set the last anchor
            lastAnchor = urlAnchor;

            // Load the data (content) file
            $.get(mapPath(urlAnchor), function(data) {
                var templateType = $(data).find('.template').html();
                var template = $(templates[parseInt(templateType)]);

                // Replace the tags from the template with the tags from the content file
                $(data).find('.content').each(function(index, value) {
                    var contentId = value.id;
                    var contentHtml = value.innerHTML;

                    $(template).find('#' + contentId).html(contentHtml);
                });

                // Remove every non active slide
                if ($('.templatecontainer').length > 1) {
                    $('.templatecontainer:not(.activeSlide)').remove();
                }

                // Add the current page
                $('#main').append($(template));

                // Reinitialize the cycle
                initializeCycle();

                // Activate the next
                $('#main').cycle('next');
            });

            return;
        }
    }
    else {
        document.location = "#home";
    }
}

function setImage(url) {
    $('#bigimage img').attr('src', url);
}

function setActionImage(url) {
    $('#bigactionimage img').attr('src', url);
}

function popup(url) {
    if (window.focus) {
        window.focus();
    }

    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    var newWindow = window.open(url, 'myWin7', 'width=550,height=500,screenX=275,screenY=50,left=300,top=50,scrollbars=no');

    if (is_chrome) {
        newWindow.parent.blur();
    }
    newWindow.focus();

    return false;
}

function mapPath(object) {
    return "content/" + object + ".html"
}
