// UK Flipbook
$(function() {
  var beforeFlashPanel,
      afterFlashPanel,
      createOrShowFlash,
      hideAllIncidentals,
      offscreenAllFlash,
      anchorindex,
      hash,
      runonce = false,    //hide the initial page curl until after the spring effect
      flashHash = {};

  /**
  * Hide all the widgets that might bounce around or flicker when the transitions happen
  * 
  * @name hideAllIncidentals
  * @function
  *
  */

  hideAllIncidentals = function() {
    $('#panel-1 .detail').hide();
    $('#panel-2 .detail').hide();
    $('#panel-3 .detail').hide();
    $('#panel-4 .detail').hide();
    $('.homebox-container').removeClass('show-people');
    $('#btn-meet').hide();
    $('#btn-tour').hide();
    $('#btn-match').hide();
    $('.nav').hide();
    $('#meetoursuccesscouples').hide();
  };

  /**
  * Create the supplied flash, or create the Download Prompt
  * 
  * @name createOrShowFlash
  * @function
  *
  */

  createOrShowFlash = function(panel,file,container,wd,ht,flashvars) {
    var c = $("#"+container),
        p = $("#"+panel+" .flashcontainer"),
        newContainer,
        att,
        par,
        dlPrompt;

    if (swfobject.hasFlashPlayerVersion("8")) {   
      if (c.length == 0) {
        newContainer = $("<div id="+container+"></div>");
        p.append(newContainer);
      }

      if (container) {
        att = { 
          "data":   file,
          "width":  wd,
          "height": ht
        };
        par = {
          "menu":      "false",
          "wmode":     "transparent",
          "flashvars":  flashvars
        };

        return swfobject.createSWF(att, par, container);
      }
    } else {
      if ($("#"+panel+" .flashcontainer .dl-prompt").length == 0) {
        dlPrompt = $("<p class='dl-prompt'>Please install the latest version of the <a href='http://get.adobe.com/flashplayer/'>Adobe Flash Player</a>.</p>");
        p.append(dlPrompt);
        return null;
      }     
    }
  };
  
  /**
  * Move all flash elements offscreen and display: none them
  * 
  * @name offscreenAllFlash
  * @function
  *
  */

  offscreenAllFlash = function() {
    $(".flashcontainer").each(function(i, elm) {
      var $elm = $(elm);

      if (!$elm.hasClass("offscreen")) {
        $elm.toggleClass("offscreen")
      }
    });
  };
  
  /**
  * Before the transition, show the incidental elements (see hideAllIncidentals), preparing them for the 
  * swipe effect
  * 
  * @name beforeFlashPanel
  * @function
  *
  */

  beforeFlashPanel = function(currSlideElement, nextSlideElement, options, forwardFlag) {
    offscreenAllFlash();
    hideAllIncidentals();
    
    switch (nextSlideElement.id) {
      case "panel-1":
        if (runonce) {
          $('#btn-meet').show();
        }
        $('#panel-1 .detail').show();
        $('.homebox-container').addClass('show-people');
      break;
      case "panel-2":
        $('.nav').show();
        $('#btn-tour').show();
        $('#meetoursuccesscouples').show();
        $("#panel-2 .detail").show();
      break;
      case "panel-3":
        $('.nav').show();
        $('#btn-match').show();
        $("#panel-3 .detail").show();
      break;
      case "panel-4":
        $('.nav').show();
        $("#panel-4 .detail").show();
      break;
    }
  };

  /**
  * After each transition, move in the flash, creating it if it doesn't exist
  * This step happens because if the flash assets were onscreened before the transition, it would tear or
  * flicker. So this step is for cleanliness.
  * 
  * @name afterFlashPanel
  * @function
  *
  */

  afterFlashPanel = function(currSlideElement, nextSlideElement, options, forwardFlag) {
    switch (nextSlideElement.id) {
      case "panel-1":
      break;
      case "panel-2":
        if (!flashHash[nextSlideElement.id]) {
          flashHash[nextSlideElement.id] = createOrShowFlash("panel-2", "http://static.eharmony.com/assets/uk/flipbook/UK-FlashCouples-Blue-610x390.swf", "swf-comemeetsome", "610", "390");
        }
        $("#"+nextSlideElement.id+" .flashcontainer").removeClass("offscreen");
      break;
      case "panel-3":
        if (!flashHash[nextSlideElement.id]) {
          flashHash[nextSlideElement.id] = createOrShowFlash("panel-3", "http://static.eharmony.com/assets/uk/flipbook/ehTour-UK-960x550.swf", "swf-tour", "590", "338");
        }
        $("#"+nextSlideElement.id+" .flashcontainer").removeClass("offscreen");
      break;
      case "panel-4":
        if (!flashHash[nextSlideElement.id]) {
          flashHash[nextSlideElement.id] = createOrShowFlash("panel-4", "http://static.eharmony.com/assets/uk/flipbook/Dimensions-UK-960x550.swf", "swf-dimensions", "590", "338");
        }
        $("#"+nextSlideElement.id+" .flashcontainer").removeClass("offscreen");
      break;
    }
  };

  $('#overview .nav1').click(function() {
    //initTab(1);
    $('#slideshow-overview').cycle(0);
    return false;
  }); 

  $('#overview .nav2, .nav5').click(function() {  
    //initTab(2);
    $('#slideshow-overview').cycle(1);
    return false;
  }); 

  $('#overview .nav3').click(function() {
    //initTab(3);
    $('#slideshow-overview').cycle(2);
    return false;
  });

  $('#overview .nav4').click(function() {
    //initTab(4);
    $('#slideshow-overview').cycle(3);
    return false;
  });

  // Parse Querystring to get Tab
  $.extend({
    getUrlVars: function(){
      var vars = [],
          hash,
          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];
    }
  });

  // Set Starting Slide Based On URL Hash
  anchorindex = 0;

  hash = $.getUrlVar('tab');
  if (hash) {
    anchorindex = hash - 1;
    hashindex = parseInt(hash);
    $('#slideshow-overview').cycle(hashindex);
  }

  //Intiate slideshow
  if ($('#slideshow-overview').length) {
    $('#slideshow-overview').cycle({
      fx: 'wipe',//'scrollHorz',
      cssBefore: {clip: 'rect(0px 701px 90px 600px)','background-color':'transparent'},
      timeout: 0,
      autostop: 0,
      speed: 500,
      startingSlide: anchorindex,
      before: beforeFlashPanel,
      after: afterFlashPanel
    });
  };

  setTimeout(function() {
    $(".curl img").animate({ width:600, height:600 }, 250);
    $(".curl img").animate({ width:450, height:450 }, 250);  
    $(".curl img").animate({ width:600, height:600 }, 350, function() {
      // Animation complete
      $(".next").zIndex(100).show();
      $(".curl img").zIndex(10).hide();
      $('#btn-meet').show();
      runonce = true;
    });  
  }, 1500);
}); //end onReady

