


// Add ios testing
Modernizr.addTest('ios', function () {
    var ua = navigator.userAgent;
    return (/iPad/i.test(ua) || /iPhone OS/i.test(ua));
});
var isIE=navigator.userAgent.match(/MSIE/);
// Jquery hint
(function ($) {

$.fn.hint = function (blurClass) {
    if (!blurClass) blurClass = 'blur';

    return this.each(function () {
        var $input = $(this),
            title = $input.attr('title'),
            $form = $(this.form),
            $win = $(window);

        function remove() {
            if (this.value === title && $input.hasClass(blurClass)) {
                $input.val('').removeClass(blurClass);
            }
        }

        // only apply logic if the element has the attribute
        if (title) { 
            // on blur, set value to title attr if text is blank
            $input.blur(function () {
                if (this.value === '' || this.value === title) {
                    $input.val(title).addClass(blurClass);
                }
            }).focus(remove).blur(); // now change all inputs to title

            // clear the pre-defined text when form is submitted
            $form.submit(remove);
            $win.unload(remove); // handles Firefox's autocomplete
        }
    });
};

})(jQuery);

$('input[title!=""]').hint();


// Make li clickable
$(document).ready(function(){
  $("#primary-nav li").click(function(){
    window.location=$(this).find("a").attr("href");return false;
  });

  // Add hover to anything

  $('*').hover(
   function() {
    $(this).addClass('hover');
   },
   function() {
    $(this).removeClass('hover');
   }
  );



});

function initUI(e) {
  // enable all the UI stuff when the page loads

  renderModels();
  //if (!isIE) initSlider(e);
  initVolume(e);
//  niceLoad(e);
  $(window).resize(resizeHandler);
  initValidation();
  resizeHandler();
  $('#primary-nav > ul > li:last').addClass('bottom');
  $('#text').append(' <div class="clear"><!-- --></div><div style="height:300px"></div>');
  $('#images').css({left:($('#text').outerWidth())+"px"});
  $('#image').css({left:(($('#text').outerWidth())+$('#text2').outerWidth())+"px"});
  $('#mapholder').css({left:(($('#text').outerWidth())+$('#text2').outerWidth())+"px"});
  $('#content').width($('#map').outerWidth()+$('#images').outerWidth()+$('#text').outerWidth()+$('#text2').outerWidth()+$('#image').outerWidth()+10);
  // hack to make IE set the height correctly
  if ($('#text').length>0) $('body').height(Math.max($('#text').height(),$('#text2').height())+100);
  $('#image img').load(function() {
    $('#content').width($('#map').outerWidth()+$('#images').outerWidth()+$('#text').outerWidth()+$('#text2').outerWidth()+$('#image').outerWidth()+10);
  });
  //if (isIE) initSlider(e);
  if ($('#images img').length>0) {
    $('#images img:last').load(function(e) {
      $('#content').width($('#map').outerWidth()+$('#images').outerWidth()+$('#text').outerWidth()+$('#text2').outerWidth()+$('#image').outerWidth()+10);
      initSlider(e);
      resizeHandler();
    });
  } else {
    initSlider(e);
    resizeHandler();
  }
}

function niceLoad(e) {
  // nice loading using pushstate



 function loadNicely(href,title) {

    var faded=false;
    var loaded=false;
    var path=href.replace(window.location.protocol+'//'+window.location.host,'');
    $('li.selected').removeClass('selected');
    $('a[href="'+path+'"]').addClass('selected').parent().addClass('selected');

    function maybeShowContent() {
      if (faded&&loaded) {
        $('#content').fadeIn();
        renderModels();
        initUI();
      }
    }

    $('#content').fadeOut('slow',
                          function(){
                            faded=true;
                            maybeShowContent();
                            $('.scroll-button').fadeOut();
                          });

    $.post(href,{ajax:1},
                       function(e){
                         $('#content').replaceWith(e); 
                         loaded=true;
                         maybeShowContent();
                       },'text');

    var stateObj = { location: href, title: title };
    history.pushState(stateObj, title, href);

  }

  $('#primary-nav a, #secondary-nav a').click(
    function(ev) {
        loadNicely(this.href,this.title);
        return false;
    });

  window.onpopstate = function(event) {
    if (event.state) {
      loadNicely(event.state.location,event.state.title);
    }
  };


}

function updateSlider(pos) {

  // when the content has been moved without the slider, update it
  var offset=($("#text").width()+$("#images").width())-$(window).width()-30+200;
  var value=0;

  if (pos) {
    value=Math.abs(pos/offset)*100;
  } else {
    value=Math.abs($('#content-holder').offset().left/offset)*100;
  }

  $('#slider').slider("value",value);
}

function initSlider(e) {
  // if the slider is needed (ie total width is greater than viewport)
 if ($('#content').width() > $(window).width()+10) {
    $( "#slider" ).slider({
      slide: function(event, ui) { 
               var ratio=ui.value/100;
               var offset=($("#text").width()+$("#images").width())-$(window).width()-30+200; // content area minus the window width plus a 30px margin
               var value=-1*Math.abs(ratio*offset);
               $('#content-holder').css({left:value+"px"});
               $('#images').css({left:($('#text').outerWidth()+value)+"px"});
               $('#image').css({left:($('#text').outerWidth()+$('#text2').outerWidth()+value)+"px"});
               $('#mapholder').css({left:($('#text').outerWidth()+$('#text2').outerWidth()+value)+"px"});
      }
    });
    
    $( "#slider" ).bind( "slidestop", function(event, ui) {
         $('a.ui-slider-handle').animate({'padding-bottom':'0px'},100); 
    });

    $('a.ui-slider-handle').hover(
        function(){$(this).animate({'padding-bottom':'3px'},100);},
        function(){
          if (!$(this).hasClass('ui-state-active')) {
            $(this).animate({'padding-bottom':'0px'},100);
          }
        }
    );


 } else {
   $('#slider_container').hide();
 }

}


function initVolume(e) {
  // setup the volume button


  if ($('#volume').length>0) {
    $('#volume a').click(function(e) {
      $(this).toggleClass('off');
      if ($(this).hasClass('off')) {
        jwplayer('audio').setVolume(0);
      } else {
        jwplayer('audio').setVolume(100);
      }
    });
  }
}

function initValidation() {

  $('#enquiries-form').validate({
 	submitHandler: function(form) {
   		form.submit();
 	},
      messages: {
        "Name": "This field is required",
        "Comments" : "This field is required",
        "Email": {
          required: "This field is required",
          email: "Your email address must be valid"
        }
      }
    });

  $('#register').validate({
      messages: {
        "cm-name": "This field is required",
        "cm-f-njuuhl" : "This field is required",
        "cm-ajydit-ajydit": {
          required: "This field is required",
          email: "Your email address must be valid"
        }
      }
    });

  $('#gift-form').validate({
      messages: {
        "name": "This field is required",
        "order-details" : "This field is required",
        "email": {
          required: "This field is required",
          email: "Your email address must be valid"
        }
      }
    });
  $('#register_age').chosen();
  $('#register_country').chosen();
  $('#payment-method').chosen();
}

function resizeHandler() {
  // resize models
  var target;
  if ($('#slider:hidden').length>0) {
    target = $(window).height()-($('header').height()-10); 
  } else {
    target = $(window).height()-($('header').height()+43); 
  }
  
  // if less than 800px high, slice the array
  if ($(window).height()<800 && typeof collection!="undefined") {
    $('.image-info').each( function() { $(this).find('ul:gt(3)').hide(); });
  } else {
    $('.image-info').each( function() { $(this).find('ul:gt(3)').show(); });
  }
  
  $('.image-holder img').height(target);
  $('#image img').height(target);
  $('#map').width($(window).width()-$('#text').width()-90);
  $('#video').width($(window).width());
  $('#videoOverlay').width($(window).width());
  $('#map, #videoOverlay').height($(window).height()-$('header').height()+70);
  $('a.scroll-button').css('top',(Math.round(($(window).height()-($('header').height()+43))/2)-34)+'px');
  $('a.scroll-button-home').css('top',(Math.round(($(window).height()-($('header').height()))/2)-34)+'px');

  $('#image-large img').css('left',($(window).width()-$('#image-large img').width())/2+'px');
  $('#images').width($('.image-holder').length*$('.image-holder img:first').width()+100);
  $('#content').width($('#map').outerWidth()+$('#images').outerWidth()+$('#text').outerWidth()+$('#text2').outerWidth()+$('#image').outerWidth());
}

function iosFakeFixed() {
  $('header').css('bottom',-window.pageYOffset+'px');
  $('#slider_container').css('bottom',183-window.pageYOffset+'px');
  //$('#image-large-bar').css('bottom',-window.pageYOffset+'px');
}

function youtubeTrack(e) {
  alert(e);
}

$(document).ready(function() {
  initUI();

  if ($('html').hasClass('ios')) {
    iosFakeFixed();
    $(document).scroll(function(e) {
      iosFakeFixed();
    });
  }

//$('#view-video').click(function(e) {
  //  $('#video-modal').lightbox_me({
    //    centered: true,
	//	overlayCSS: {background: 'black', opacity: .8}
      //  });
    //e.preventDefault();
//});

//$('.close').click(function() {
  //  $('#video-modal').fadeOut(400);
    //var player=$f($('.vid:visible')[0]);
    //player.api('pause');
//});

$('.video-link').click(function(e) {
    var ytcode=$(this).find('.youtube').html();
    var yt='<iframe class="vid" src="http://player.vimeo.com/video/'+ytcode+'?api=1&amp;title=0&amp;byline=0&amp;portrait=0&amp;color=000000" width="800" height="450" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
   

 document.location.hash=this.id;
    $('.video-holder').html(yt);
	
    $('#video-modal').lightbox_me({
          centered: true,
		  overlayCSS: {background: 'black', opacity: .8} 
        });
		
    e.preventDefault();
    // setTimeout(startTrackingYT,1500);
     
});

});








