﻿/*
  Main javascript file Van Gogh Museum
  @author: Andre van Toly, http://www.toly.nl
  @version  '$Id: main.js 2316 2011-08-18 14:51:27Z andre $'
*/

/* "pulldow" navigation */
function initNav() {
    $('div#pulldownnavigation').hide().removeClass('hidden'); /* seems weird: prevents showing nav while loading */

    if (isIos()) {
        /* On touch/ios devices: inmediately opens navigation when clicked.
           When clicked again it will open the menu item. */
        $('#headnav a').click(
            function() { 
                if ($('div#pulldownnavigation').hasClass('openme')) {
                    $('div#pulldownnavigation').removeClass('openme').slideDown('fast'); 
                    return false;
                } else {
                    return true;
                }
            }
        );
    }
    
    $('#headnav').mouseenter(
        function() { 
            $('div#pulldownnavigation').addClass('openme');
            setTimeout(function(){
                if ($('div#pulldownnavigation').hasClass('openme')) {
                    $('div#pulldownnavigation').slideDown('fast'); 
                }
            }, 500);
        }
    );
    $('#headnav').mouseleave(
        function(ev) { 
            var divOffset = $('div#pulldownnavigation').offset();
            //console.log('leaving y 1: ' + divOffset.top + " mouse " + ev.pageY);
            $('div#pulldownnavigation').removeClass('openme');
            if (ev.pageY + 10 < divOffset.top) {
                $('div#pulldownnavigation').slideUp('fast');
            }
        }
    );
    $('div#pulldownnavigation').mouseenter(
        function() { 
            $('div#pulldownnavigation').addClass('openme').show();
        }
    );
    $('div#pulldownnavigation').mouseleave(
        function(ev) { 
            var divOffset = $('div#pulldownnavigation').offset();
            $('div#pulldownnavigation').removeClass('openme'); 
            //console.log('y 2: ' + divOffset.top + " mouse " + ev.pageY);
            setTimeout(function() {
                if (ev.pageY > divOffset.top + 10 && ! $('div#pulldownnavigation').hasClass('openme')) {
                    $('div#pulldownnavigation').slideUp('fast'); 
                }
            }, 250);
        }
    );
    
    $('li#sectie_vincent a, ul#groupsectie_vincent').hover(function(){
        $('ul#groupsectie_vincent').find('li').addClass('active');
        $('li#sectie_vincent').find('a').addClass('active');
    },
    function(){
        $('ul#groupsectie_vincent').find('li').removeClass('active');
        $('li#sectie_vincent').find('a').removeClass('active');
    });

    $('li#sectie_museum a, ul#groupsectie_museum').hover(function(){
        $('ul#groupsectie_museum').find('li').addClass('active');
        $('li#sectie_museum').find('a').addClass('active');
    },
    function(){
        $('ul#groupsectie_museum').find('li').removeClass('active');
        $('li#sectie_museum').find('a').removeClass('active');
    });
    
    $('li#sectie_actueel a, ul#groupsectie_actueel').hover(function(){
        $('ul#groupsectie_actueel').find('li').addClass('active');
        $('li#sectie_actueel').find('a').addClass('active');
    },
    function(){
        $('ul#groupsectie_actueel').find('li').removeClass('active');
        $('li#sectie_actueel').find('a').removeClass('active');
    });
    
    $('li#sectie_onderzoek a, ul#groupsectie_onderzoek').hover(function(){
        $('ul#groupsectie_onderzoek').find('li').addClass('active');
        $('li#sectie_onderzoek').find('a').addClass('active');
    },
    function(){
        $('ul#groupsectie_onderzoek').find('li').removeClass('active');
        $('li#sectie_onderzoek').find('a').removeClass('active');
    });

    $('li#sectie_onderwijs a, ul#groupsectie_onderwijs').hover(function(){
        $('ul#groupsectie_onderwijs').find('li').addClass('active');
        $('li#sectie_onderwijs').find('a').addClass('active');
    },
    function(){
        $('ul#groupsectie_onderwijs').find('li').removeClass('active');
        $('li#sectie_onderwijs').find('a').removeClass('active');
    });
    
    $('li#sectie_kinderen a, ul#groupsectie_kinderen').hover(function(){
        $('ul#groupsectie_kinderen').find('li').addClass('active');
        $('li#sectie_kinderen').find('a').addClass('active');
    },
    function(){
        $('ul#groupsectie_kinderen').find('li').removeClass('active');
        $('li#sectie_kinderen').find('a').removeClass('active');
    });
    
    $('li#sectie_online a, ul#groupsectie_online').hover(function(){
        $('ul#groupsectie_online').find('li').addClass('active');
        $('li#sectie_online').find('a').addClass('active');
    },
    function(){
        $('ul#groupsectie_online').find('li').removeClass('active');
        $('li#sectie_online').find('a').removeClass('active');
    });
    
}

function isIos() {
    return navigator.userAgent.match(/iPhone|iPod|iPad/i) != null;
}


/* carousel with jquery.jcarousel */
function initCarousel() {
    jQuery('#homecarousel').jcarousel({ 
        auto: 5, 
        scroll: 1,
        animation: 900,
        wrap: 'both', /* "first", "last", "both" or "circular" */
        initCallback: initCarouselCallback,
        itemVisibleInCallback: {
          onBeforeAnimation: function(car, el, i){
            var mod = i % car.size();
            /* add extra property to keep track of where we are */
            if (car.count == undefined) {
                car.count = 0;
            } else {
                car.count++;
            }
            $('#homecarouselcontrols ol').find('li').each(function(index){
                $(this).find('a').removeClass('selected');
                index = index + 1;
                if (index == mod) {
                    $(this).find('a').addClass('selected');
                } else if ((mod + car.size()) == index) {
                    $(this).find('a').addClass('selected');
                }
            });
          }
        }
    });
}

/* carousel controls */
function initCarouselCallback(car) {
    $('#homecarouselcontrols a').click(function(ev){
        var link = $(this).attr('href');
        var index = parseInt(link.substring(link.indexOf('_') + 1));
        var goindex = index;
        car.scroll($.jcarousel.intval(goindex));
        return false;
    });
}

/* jqeury.lightbox.js for images */
function initLightBox() {
    var settings = jQuery.extend({
        overlayOpacity: 0.9,
        containerResizeSpeed: 250,
        imageLoading:   'style/css/images/loading.gif',
        imageBtnPrev:   'style/css/images/lightbox-prev.png',
        imageBtnNext:   'style/css/images/lightbox-next.png',
        imageBtnClose:  'style/css/images/lightbox-close.png',
        imageBlank:     'style/css/images/lightbox-blank.png'
    }, settings);
    $('a.lightbox').lightBox(settings);
}

function initLanguageSwitch() {
    $("select[id='language_choices']").change(function() {
        var newUrl = $(this).val();
        document.location = newUrl;
    });
}


/* Called by a Flash movie : Vincent van Gogh in vogelvlucht flash popup */
function open_pop_up(url) {
   window.open('vogelvlucht/' + url,'vogelvlucht','width=660, height=520, resizable=1, scollbars=0');
}

/* Opens pop-up to a Flash zoom tool */
function initZoom() {
    $('a.zoom').click(function(){
        window.open(this.href, 'Zoom', 'location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=820,height=550');
        return false;
    });
}

function initFaq() {
    $('ul.faqs').find('div.answer').hide();
    $('ul.faqs').find('a.faquestion').click(function(ev){
        ev.preventDefault();
        $(this).next('div.answer').slideToggle('fast');
    });
}

function initTabs(  ) {
	$('#collectie_tabs').tabs();
}

/* check form fields */
function initForm() {
	if ($('form#form-brochure').length) {
	    $('form#form-brochure').submit(function(ev){
	        var self = this;
            var msg = "";
            
            /* all required fields by name */ 
            var arr = [ "firstname", "name", "address", "postcode", "city", "country", "email", "gender" ];
            jQuery.each(arr, function() {
                if ($(self).find('input[name=' + this + ']').is('input:radio')) {  // radio buttons
                    if ($(self).find('input:radio[name=' + this + ']:checked').val() == undefined) {
                        $(self).find('input[name=' + this + ']').parents('div').addClass('error');
                        msg += " " + this;
                    } else {
                        $(self).find('input[name=' + this + ']').parents('div').removeClass('error');
                    }
                } else if ($(self).find('input[name=' + this + ']').val().length == 0) {
                    $(self).find('input[name=' + this + ']').parent('div').addClass('error');
                    msg += " " + this;
                } else {
                    $(self).find('input[name=' + this + ']').parent('div').removeClass('error');
                }
            });
            
            if (msg.length > 0) {
                var lang = $(self).find('input[name=ln]').val();
                if (lang == 'nl') {
                    msg = "Een aantal verplichte velden zijn niet goed of onvolledig ingevuld.";
                } else {
                    msg = "Some required fields were not complete or incorrect.";
                }
                
                if ($(this).find('p.err').length == 0) { 
                    $(this).prepend('<p class="err">' + msg + '</p>');
                } else {
                    $(this).find('p.err').text(msg);
                }
    	        return false;   // hold submit
    	        
            } else {
                if ($(this).find('p.err').length > 0) {
                    $(this).remove('p.err');
                }
    	        return true;    // submit
            }

	    });
	}
}

$(document).ready(function() {
    if ($('div#pulldownnavigation').length) { initNav() };
    if ($('#homecarousel').length) { initCarousel() };
    if ($('#language_choices').length) { initLanguageSwitch(); }
    if ($('a.lightbox').length) { initLightBox(); }
    if ($('ul.faqs').length) { initFaq(); }
    initTabs();
    initZoom();
    initForm();
    $('a.externallink, a.popup').click(function(){
        window.open(this.href);
        return false;
    });
    $('div#box').find('a').each(function(index){
        var href = $(this).attr('href');
        if (href != undefined && href.indexOf('popup.jsp') > -1) {
            $(this).click(function() {
                popup(href,'Window',740,500);
                return false;
            });
        }
    });
    
    $("select[id='select_year']").change(function() {
        $(this).parent('form').submit();
        //document.location = newUrl;
    });
    
    
    $('div.cat').hover(
        function(ev){ $(this).find('a').css('color', '#fff') }, 
        function(ev){ $(this).find('a').css('color', '#000') }
    );
    $('div.cat').click(function(ev){ 
        var link = $(this).find('a').attr('href');
        document.location = link;
    });
    
    
});


/* Below is from older vgm website (before 1/6/2011): 
   the first is a popup window script,
   the second is used for sitestat */
function popup(Link,Name,Width,Height){
  var breedte=Width;
  var hoogte=Height;
  if (breedte==-1) breedte=500;
  if (hoogte==-1) hoogte=500;
  var iMyWidth = (window.screen.width/2) - (breedte/2 + 10);
  var iMyHeight = (window.screen.height/2) - (hoogte/2 + 50);

  var win3 = window.open(Link,Name,"location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=" + breedte + ",height=" + hoogte + ",left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight);
  win3.focus();
}

/*
  name <cis>-- name of the desired variable
  return string containing value <cs> of specified name <cis>
         or null if cookie name not exist
*/
function getValueFromUrl(name)
{
  var url = '' + this.location;
  var idx  = url.indexOf('?');
  if(idx==-1)
	return null;
  var query = "&" + url.substring(idx+1);
  var qs = query.toLowerCase();
  var key = "&" + name.toLowerCase() + "=";
  var begin = qs.indexOf(key);
  if(begin==-1)
	return null;
  else
    begin += key.length;
  var end = qs.indexOf("&", begin);
  if (end == -1)
    end = query.length;
  return unescape(query.substring(begin, end));
}

