/* Copyright (c) 2009 Iksi */

(function($) {

var openclose = false;

$(document).ready(function() {
	
 jQuery.easing.def = "easeOutQuad";	

 $("#debug").html("loaded");
 
 setToggle($(".toggle"),"close"); // set initial toggle behaviour
 setToggle($(".workvideo > .toggle"),"open"); // set initial toggle behaviour

 function setToggle(div,type) { // set toggle behaviour

  if (type == "open") {

   div.addClass('button3');
   div.removeClass('button');
   div.removeClass('button2');

   div.unbind('mouseenter');
   div.unbind('mouseleave');

   div.bind('mouseenter', function() {
    $(this).addClass('button4');
   }).bind('mouseleave', function() {
    $(this).removeClass('button4');
   });

  } else if (type == "close") {

   div.addClass('button');
   div.removeClass('button3');
   div.removeClass('button4');

   div.unbind('mouseenter');
   div.unbind('mouseleave');

   div.bind('mouseenter', function() {
    $(this).addClass('button2');
   }).bind('mouseleave', function() {
    $(this).removeClass('button2');
   });

  }
 }

 // toggle
 $(".toggle").bind("click", function(){

  $("#debug").html("toggle");
  if ($(this).siblings(".long-body").css("display") == "none") {
   setToggle($(this),"open");
   // hellip
   $(this).siblings().find(".hellip").css("display","none");
   // check for special exceptions
   if ($(this).parent().attr("id") == "header") {
      $("#header-invisible2").slideDown("fast");
   }
   if ($(this).parent().prev().attr("class") == "item-image") {
    $(this).parent().prev().find("div").show("blind", { direction: "vertical" }, "fast"); 
   }
   //

   $(this).siblings(".long-body").show("blind", { direction: "vertical" }, "fast", function() {
    $("#debug").html("done");
    // if ($(this).parent().prev().prev().attr("class") == "item-slideshow") {
    // 	 $(this).parent().prev().prev().fadeIn("fast");
    //     }
    // $("#debug").html($(this).parent().prev().prev().attr("class"));
   });   
  } else if ($(this).siblings(".long-body").css("display") == "block") {
   setToggle($(this),"close");	
   // hellip
   $(this).siblings().find(".hellip").css("display","inline");
   // check for special exceptions
   if ($(this).parent().attr("id") == "header") { // header > right
    $("#header-invisible2").slideUp("fast");
   }
   if ($(this).parent().prev().attr("class") == "item-image") { // work > left
    $(this).parent().prev().find("div").hide("blind", { direction: "vertical" }, "fast"); 
   }
   //

   $(this).siblings(".long-body").hide("blind", { direction: "vertical" }, "fast", 	 function() {
    $("#debug").html("done");
	 //     if ($(this).parent().prev().prev().attr("class") == "item-slideshow") {
	 // $(this).parent().prev().prev().fadeOut("fast");
	 //     }

   });	
  }
 });

 // video buttons
 setVideoToggle($(".openvideo"),"close"); // set initial toggle behaviour

 function setVideoToggle(div,type) { // set toggle behaviour

  if (type == "open") {

   div.addClass('button7');
   div.removeClass('button5');
   div.removeClass('button6');

   div.unbind('mouseenter');
   div.unbind('mouseleave');

   div.bind('mouseenter', function() {
    $(this).addClass('button8');
   }).bind('mouseleave', function() {
    $(this).removeClass('button8');
   });

  } else if (type == "close") {

   div.addClass('button5');
   div.removeClass('button7');
   div.removeClass('button8');

   div.unbind('mouseenter');
   div.unbind('mouseleave');

   div.bind('mouseenter', function() {
    $(this).addClass('button6');
   }).bind('mouseleave', function() {
    $(this).removeClass('button6');
   });

  }
 }

 // $(".openvideo").bind('mouseenter', function() {
 //  $(this).addClass('button6');
 // }).bind('mouseleave', function() {
 //  $(this).removeClass('button6');
 // });

 $(".closebutton").bind('mouseenter', function() {
  $(this).addClass('button4');
 }).bind('mouseleave', function() {
  $(this).removeClass('button4');
 });

 $(".openvideo").bind("click", function(){

  $("#debug").html("open video");
  if ($(this).parent().prev().parent().prev().css("display") == "none") {
   setVideoToggle($(this),"open");
    $(this).parent().prev().parent().prev().show("blind", { direction: "vertical" }, "fast", function() {
    $("#debug").html("done");
   });
  } else if ($(this).parent().prev().parent().prev().css("display") == "block") {
   setVideoToggle($(this),"close");
    $(this).parent().prev().parent().prev().hide("blind", { direction: "vertical" }, "fast", function() {	
    $("#debug").html("done");	
   });
  }
 });

 $(".closebutton").bind("click", function(){
  $("#debug").html("close video");
  if ($(this).parent().css("display") == "block" || $(this).parent().css("display") == "inline") {

    // hm exceptions...
    if ($(this).parent().attr("id") == "showreel-hidden") {
     // showreel
     $("#nowplaying").css("display","none");		
    }	
    if ($(this).parent().attr("id") == "featured-info") {
     // featured info
     $("#info-hidden").css("display","none");	 
     $("#info").css("display","inline");
    }
    if ($(this).parent().attr("id") == "featured-hidden") {
     $(this).parent().css("display","none");
     $(this).parent().next().css("display","block");
    } else {
     setVideoToggle($(this).parent().next().children().next().find(".openvideo"),"close");
     $(this).parent().hide("blind", { direction: "vertical" }, "fast", function() {
      $("#debug").html("done");
     });
    }
  }
 });

 /* more exceptions */

 // showreel
 $(".openshowreel").bind("click", function() {
  $("#debug").html("open showreel");
  if ($("#showreel-hidden").css("display") == "none") {
   $("#nowplaying").css("display","block");
   $("#showreel-hidden").show("blind", { direction: "vertical" }, "fast", function() {
     $("#debug").html("done");
    });
  }
 });

 // featured (info)
 $("#info").bind("click", function() {
  $("#debug").html("open info");
  $("#featured-info").show("blind", { direction: "vertical" }, "fast", function() {
    $("#debug").html("done");
   });
 });


});

})(jQuery);