/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// Collapsable menu navigation added by Ben (who in other respects denies any
// involvement with this site...)
$(document).ready(function() {
    $('.category-header').each(function() {
        var category = $(this).data('category');

        if ($.cookie('category_' + category) !== 'open') {
            $('.category-' + category).hide();
        }

        $(this).click(function() {
            if ($('.category-' + $(this).data('category')).is(':visible')) {
                $(this).removeClass('open').addClass('closed');
                $.cookie('category_' + $(this).data('category'), null);
            } else {    
                $(this).removeClass('closed').addClass('open');
                $.cookie('category_' + $(this).data('category'), 'open');
            }
            $('.category-' + $(this).data('category')).slideToggle();
        });
    });
});


// Original site follows...
//
var timeline = function()
{
  var divid = ".footer #timeline";
  
  $(divid + ' #line').animate({'width' : '100%', 'left' : '0px'}, 800);
  $(divid + ' #dates #date').each(function(){
      var left = $(this).attr('left');
      $(this).delay(500).animate({'left' : left+'px', 'opacity' : '1.0' ,'filter': 'alpha(opacity=100)'},800);
  });
}
var update_cart = function()
{
  var cartDiv = ".cart";
  
 /* $.getJSON('/update_cart', function(data) 
  {
    $('.cart #title').html('Cart Total £'+data.cart_total);
    console.log(data);
  });
 */
}
var popout = function(load)
{
  var overlay = '.overlay';
  var message = '.overlay #message';
  
    if (load == "ext") {

    } else {
        $(overlay + ' #content').hide();
        $(message).hide();
        $(overlay).fadeIn();
        $('.overlay #content').hide();

        //Ajaxify the content
        $.ajax({
            url: "/ajax/"+load,
            context: document.body,
            success: function(mbody){
              $(message).html(mbody);
              $(message).fadeIn();
              console.log(mbody);
          }
        });
    }
 
}

$('div.navigation').css({'width' : '300px', 'float' : 'left'});
$('div.contenta').css('display', 'block');

$(window).load(function(){
  timeline();
  try {
  $('#timeline a').lightBox({
    imageLoading: '/images/lightbox-loading-ico.gif',
	imageBtnClose:'/images/lightbox-btn-close.gif',
	imageBtnPrev: '/images/lightbox-btn-prev.gif',
	imageBtnNext: '/images/lightbox-btn-next.gif'
    });
  } catch(e) {}

  update_cart();
   $('.footer #timeline #dates #date').mouseover(function(){
     var me  = $(this);
     var pos = me.offset();
     var left_pos = pos.left - 90;
     var top_pos  = pos.top  - 240;
     var text = me.attr('text');
     $('#tooltip').remove();
     $('body').append('<div onclick="javscript: $(this).remove();" id="tooltip"style="color:black; position:absolute; left:'+left_pos+'px; top:'+top_pos+'px;">'+text+'</div>');
});
   $('.footer #timeline #dates #date, #tooltip').mouseleave(function(){ $('#tooltip').remove(); });
   
     $('.gall').click(function(){
       var me = $(this);
       var src = me.attr('src');
       $('.overlay #content').html("<img maxwidth='600px' id='overimg' src='"+src+"' />");
       $('.overlay #content').css({'margin-left' : '-250px', 'left' : '50%'});
       
       $('.overlay').fadeIn();
     });   
   $('.overlay').click(function(){
     $(this).fadeOut();
   });
   $('#gallery img').click(function(){
     var me = $(this);
     var src = me.attr('src');
     console.log(me);
     $('.overlay #content').html("<img maxwidth='600px' id='overimg' src='"+src+"' />");
     $('.overlay #content').css({'margin-left' : '-250px', 'left' : '50%'}).fadeIn();
       
     $('.overlay').fadeIn();
     $('.overlay #content').fadeIn();
   });
   
   $('.map img').click(function(){
     var me = $(this);
     var src = me.attr('src');
     console.log(me);
     $('.overlay #content').html("<img width='800px' id='overimg' src='"+src+"' />");
     $('.overlay #content').css({'margin-left' : '-250px', 'left' : '50%'}).fadeIn();
       
     $('.overlay').fadeIn();
     $('.overlay #content').fadeIn();
   });
   
   
   
   $('#right img').click(function(){
     var load = $(this).attr('load');
     if(load == "order")
      window.location = "/order";
     else
      popout(load);
   });
   
   $('.postcodeb').click(function(){
     $('#postcode').submit();
   });
});

