var secondary_status = 'closed';

$(function () {

  $('#menu .wrap').hide();
  $('#content_secondary .wrap').css("opacity", "0");
  $('#content_tertiary .wrap').css("opacity", "0");
  
  
  
  $('#superbgimage, #navigation, #nextslide, #prevslide').click(function () {
    $('#content_secondary .wrap').fadeOut(50);
    $('#menu .wrap').fadeOut(50);
    $('#address').addClass('white');
    $("#menu").slideUp(800, function () {
      // Animation complete.
      $('#menu_toggle').slideDown();
    });
	
    if (secondary_status == 'open') {

      $("#content_secondary").slideUp(1000, function () {
        // Animation complete.
        $('#secondary_toggle').slideDown();
      });
    }
    if (bySubpage == 'open') {
    $('#content_tertiary .wrap').fadeOut(50);
      $("#content_tertiary").slideUp(1200)
    }
    if (bySlideshow == 'open') {
      $("#content_lookbook .inner").fadeOut(100);
    }
    return false;
  });
  
  
  $('#menu_toggle, #secondary_toggle').click(function () {
	
    $('#menu_toggle, #secondary_toggle').slideUp();
    $("#menu").slideDown(800, function () {
      // Animation complete.
      $('#address').removeClass('white');
      $('#menu .wrap').fadeIn(100);
    });
    if (secondary_status == 'open') {
      $("#content_secondary").slideDown(1000, function () {
        // Animation complete.
        $('#content_secondary .wrap').fadeIn(100);
        if (bySlideshow == 'open') {
          $("#content_lookbook .wrap").fadeIn(100);
          $("#content_lookbook .inner").fadeIn(100);
        }
      });
    }
    if (bySubpage == 'open') {
      $("#content_tertiary").slideDown(1200, function () {
        // Animation complete.
        $('#content_tertiary .wrap').fadeIn(100);
      });
    }
    return false;
  });
  
  $('#menu_toggle').one('click', function () {
    $('#intro').fadeOut(1200);
    $("#menu").slideDown(800, function () {
      // Animation complete.
      $('#address').addClass('white');
      $('#menu .wrap').fadeIn(100);
    });
	if(bySection){
    	$("#content_secondary").slideDown(1000, function () {
      		// Animation complete.
      		$("#content_secondary .wrap").animate({ opacity: 1 }, 100);
			secondary_status = 'open';
    	});
	}
    if (bySlideshow == 'open') {
      $("#content_lookbook").slideDown(1200, function () {
        // Animation complete.
        $("#content_lookbook .wrap").animate({ opacity: 1 }, 100);
      });
    }
    if (bySubpage == 'open') {
      $("#content_tertiary").slideDown(1200, function () {
        // Animation complete.
        $('#content_tertiary .wrap').animate({ opacity: 1 }, 100);
      });
    }
    //secondary_status = 'open'
        reinitialiseScrollPane();
  });
  
});

//ON RESIZE FUNCTION
function onResize() {
  if (resizeTimer) clearTimeout(resizeTimer);
  resizeTimer = setTimeout(function () {

    $('#content_secondary .inner').jScrollPaneRemove();
    $('#content_secondary .inner').css({
      'height': (($(window).height()) - 266) + 'px'
    });
    if (bySlideshow == 'open') {
      $('#content_lookbook .inner').css({
        'height': (($(window).height()) - 291) + 'px'
      });
    }
    if (bySubpage == 'open') {
      $('#content_tertiary .inner').jScrollPaneRemove();
      $('#content_tertiary .inner').css({
        'height': (($(window).height()) - 266) + 'px'
      });
    }
    Jscroll();
  }, 50);
}
var resizeTimer = null;
$(window).resize(onResize);

var default_content = "";
$.extend({
  getUrlVars: function () {
    var vars = [],
        hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function (name) {
    return $.getUrlVars()[name];
  }
});

function Jscroll() {
  $('#content_secondary .inner').jScrollPane({
    scrollbarWidth: 7,
    showArrows: true,
    dragMaxHeight: 30,
    dragMinHeight: 30
  });
  if (bySubpage == 'open') {
    $('#content_tertiary .inner').jScrollPane({
      scrollbarWidth: 7,
      showArrows: true,
      dragMaxHeight: 30,
      dragMinHeight: 30
    });
  }
}


//////////////SLIDESHOW
// function callback on clicking image, show next slide
function superbgimage_click(img) {
	  $('#slideshow').nextSlide();
}

function resetSlideShow(){
   $('#slideshow').stopSlideShow();
   $('#slideshow').empty().hide();
   $('#superbgimage').empty();
}

function initSlideShow(){
   var nb = $('#slideshow a').length;
   if(nb > 1)anim = 1;
   else anim = 0;
   $.fn.superbgimage.options = {
	 	transition: 1,
	 	transitionout: 1,
		randomtransition: 0, // 0-none, 1-use random transition (0-7)
		slideshow: anim, // 0-none, 1-autostart slideshow
		slide_interval: 6000, // interval for the slideshow
		randomimage: 0, // 0-none, 1-random image
		speed: 300, // animation speed
		onClick: superbgimage_click,
		reload:1,
		preload: 1
	};
	
	$('#slideshow').stopSlideShow();
	// initialize SuperBGImage
	$('#slideshow').superbgimage();

   	
// prev slide
	$('a#prevslide').click(function() {
		return $('#slideshow').prevSlide();
	});

	// next slide
	$('a#nextslide').click(function() {
		return $('#slideshow').nextSlide();
	});

}

function reinitialiseScrollPane() {
 	$('#content_secondary .inner').jScrollPaneRemove();
    $('#content_secondary .inner').css({
      'height': (($(window).height()) - 266) + 'px'
    });
    if (bySlideshow == 'open') {
      $('#content_lookbook .inner').css({
        'height': (($(window).height()) - 291) + 'px'
      });
    }
    if (bySubpage == 'open') {
      $('#content_tertiary .inner').jScrollPaneRemove();
      $('#content_tertiary .inner').css({
        'height': (($(window).height()) - 266) + 'px'
      });
    }
    Jscroll();
 }
 

function redirect(link) {
  secondary_status = 'open';
  $('#content_secondary .wrap').animate({opacity: 0}, 1);
  $('#content_lookbook .wrap').hide();
  $('#content_lookbook').slideUp(1000);
  $("#content_secondary").slideUp(800);
  $("#content_tertiary").slideUp(1000);
  $("#content_secondary").slideUp(800, function () {
    checkURL(link.href);
    $("#content_secondary").slideDown(800, function () {
      // Animation complete.
      $('#content_secondary .wrap').animate({ opacity: 1 }, 100);
      reinitialiseScrollPane();
    });
    if (bySlideshow == 'open') {
      $("#content_lookbook").slideDown(1000, function () {
        // Animation complete.
        $('#content_lookbook .wrap').fadeIn(100);
      });
    }
  });
}

$(document).ready(function () {
  checkURL();
  
  $('input[type=text]').live('focus', function(){
     $(this).data('inputName', $(this).attr('value'));
     
	   $(this).attr('value', '');;
	 
  });
  $('input[type=text]').live('focusout', function(){
    
    if($(this).attr('value')=='')
    $(this).attr('value', $(this).data('inputName'));
  });
  $('#main li a').click(function (e) {
    redirect(this);
  });
  $('a.address').click(function (e) {
    redirect(this);
  });
  $('#secondary li a').click(function (e) {
    redirect(this);
  });
  $('#secondary_menu li a').live('click', function (e) {
    redirect(this);
  });
  $('a.tertiary_link').live('click', function (e) {
    $('#content_tertiary .wrap').hide();
    $("#content_tertiary").slideUp(800, function () {
      checkURL(this.href);
      $('#content_tertiary .wrap').animate({ opacity: 1 }, 100);
      $("#content_tertiary").slideDown(800, function () {
        // Animation complete.
        $('#content_tertiary .wrap').fadeIn(100);
          reinitialiseScrollPane();
      });
    });
  });
  
  $('ul#text_news li a').live('click', function (e) {
    var list = $(this).siblings('ul');
    if (list.is(":hidden")) {
      list.show('fast', reinitialiseScrollPane);
      
    }
    else{
      list.hide('fast', reinitialiseScrollPane);
      
    }
    return false;
  });
  //filling in the default content
  default_content = $('#content_secondary .inner').html();
  setInterval("checkURL()", 500);
  setTimeout("initSlideShow()", 2000);
});
var lasturl = "";

function checkURL(hash) {
  if (!hash) hash = window.location.hash;
  if (hash == "")
    hash = '#section=home&page=index';
    
  if (hash != lasturl) {
    
    lasturl = hash;
    
    // FIX - if we've used the history buttons to return to the homepage,
    // fill the pageContent with the default_content
    loadPage(hash);
  }
}
// Init variables
var lastsection = "";
var bySection = "";
var byPage = "";
var bySlideshow = "";
var bySubpage = "";

function loadPage(url) {

  lastsection = bySection;
  // Getting URL var by its name
  bySection = $.getUrlVar('section');
  byPage = $.getUrlVar('page');
  
  $('.active').each(function () {
    $(this).removeClass("active");
  });
  $('#main li a[href^="#section=' + bySection + '"]').addClass('active');
  $('#secondary li a').each(function () {
    $(this).unbind('mouseenter mouseleave');
    $(this).hover(function () {
      $(this).find('img').attr('src', $(this).find('img').attr('src').replace('.png', '_it.png'));
    }, function () {
      $(this).find('img').attr('src', $(this).find('img').attr('src').replace('_it.png', '.png'));
    });
    $(this).find('img').attr('src', $(this).find('img').attr('src').replace('_it.png', '.png'));
  });
  $('#secondary li a[href^="#section=' + bySection + '"]').each(function () {
    $(this).unbind('mouseenter mouseleave');
    $(this).find('img').attr('src', $(this).find('img').attr('src').replace('.png', '_it.png'));
  });
  if(!bySection) return;
  // Default page
  if (!bySection) bySection = 'house';
  if (!byPage) byPage = 'index';
  //	$.getJSON()
  var jsonUrl = "json.php";
  $.getJSON(
  jsonUrl, {
    section: bySection,
    page: byPage
  }, function (json) {
    document.title = 'Graanmarkt13 - ' + json.title;
    $('#secondary_menu').empty();
    $('#content_secondary .inner').html(json.text);
    
    // If secondary_menu is needed
    if (json.links != null) {
    
      $.each(json.links, function () {
        // Use the values this.name and this.id
        $('#secondary_menu').append('<li><a href=' + '#section=' + bySection + '&page=' + this.id + '>' + this.name + '</a></li>');
      });
      $('#secondary_menu li a[href^="#section=' + bySection + '&page=' + byPage + '"]').addClass('active');
    }
    
    // If we need to load a new set of background images
    if (bySection != lastsection) {
    
      resetSlideShow();
      

      
      for (var i = 0; i < json.supersize.length; i++)
      $('#slideshow').append('<a href="' + json.supersize[i].url + '">'+ i +'</a>');
      
      initSlideShow();    
	
  }
     
  // If we need a secondary slideshow
  if (json.sub_slideshow != null) {
    bySlideshow = 'open';
    $('#content_lookbook inner, #caption').empty();
    $('#content_lookbook .inner').hide();
    //$('#content_secondary .inner').append('<h2>'+ json.title +'</h2><div id="caption"></div>');
    $('#content_secondary .inner').append('<div id="caption"></div>');
    i = 0;
    $.each(json.sub_slideshow, function () {
      // Use the values this.name and this.id
      $('#content_lookbook .inner').append('<img id="subSlide' + i + '" src="' + this.url + '" alt="lookbook"/>');
      $('#content_lookbook .inner #subSlide' + i).data('desc', this.caption);
      i++;
    });
    if (i == 1) {
      $('#caption').html(json.sub_slideshow[0].caption).fadeIn(300);
    }
    else if (i > 1) $('#content_secondary .inner').prepend('<div id="navigation2"><a href="#" id="prevslide2"></a><a href="#" id="nextslide2"></a></div><br class="clear" /><div id="caption"></div>');
    $('#content_lookbook .inner').fadeIn('slow');
   
    $('#content_lookbook .inner').cycle({
      fx: 'custom',
      speed: 300,
      timeout: 0,
      containerResize: 0,
      sync: 0,
      prev: '#prevslide2, #content_lookbook .inner img',
      next: '#nextslide2',
      before: function () {
        var desc = $(this).data('desc');
        $('#caption').fadeOut(300, function () {
          $(this).html(desc).fadeIn(300);
        });
        $('#content_lookbook').css({
          width: $(this).outerWidth()
        });
      },
      cssBefore: {
        left: 0,
        top: 0,
        width: 'auto',
        height: '100%',
        opacity: 0,
        zIndex: 1
      },
      animOut: {
        opacity: 0
      },
      animIn: {
        left: 0,
        top: 0,
        width: 'auto',
        height: '100%',
        opacity: 1
      },
      cssAfter: {
        zIndex: 0
      }
    });
  }
  //if no secondary slideshow, we empty the divs 
  else {
    $('#content_lookbook .inner, #caption').empty();
    bySlideshow = 'closed';
  }
  // If we need a tertiary menu
  if (json.links_tertiary != null) {
    $('#content_secondary .inner').html('<ul id="tertiary_menu"></ul>')
    $.each(json.links_tertiary, function () {
      // Use the values this.name and this.id
      $('#tertiary_menu').append('<li><a class="tertiary_link" href=' + '#section=' + bySection + '&page=' + this.id + '>' + this.name + '</a></li>');
    });
    $('#secondary_menu li a[href^="#section=' + bySection + '&page=' + byPage + '"]').addClass('active');
  }
  // If we need a tertiary content
  if (json.tertiary_text != null) {
    bySubpage = 'open';
    $('#content_tertiary .inner').html(json.tertiary_text);
  }
  else {
    $('#content_tertiary .inner').empty();
    bySubpage = 'closed';
  }
});
//alert('ok');
   reinitialiseScrollPane();

}
