﻿// starting the script on page load
$(document).ready(function(){
  imagePreview();
});


jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("z-index","70");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
jQuery.fn.fillScreen = function () {
  this.css("top", "0px");
  this.css("left", "0px");
  this.css("width", $(window).width()+'px')
  this.css("height", $(document).height()+'px');
  return this;
}


/* For new car navigation */

var activeQuestion
function showQuestion(_activeQuestion) {
  activeQuestion = _activeQuestion;
  activeQuestion.center();
  activeQuestion.fadeIn();
  $('#qbg').fillScreen().fadeTo("slow",0.7);;
}
function closeQuestion() {
  activeQuestion.fadeOut();
  $('#qbg').fadeOut();
  activeQuestion = null
}

function gotoWebsite(url, carid) {
  $('#manuLink').attr("href", url);
  $('#bookTestDrive').attr("href", "/book_testdrive.aspx?c=" + carid)
  $('#manuBookTestDrive').attr("href", "/book_testdrive.aspx?c=" + carid)
  
  showQuestion($('#websitequestion'))
  $(window).scroll(function () { if (activeQuestion) {activeQuestion.center();} });
  $(window).resize(function () { if (activeQuestion) {activeQuestion.center();$('#qbg').fillScreen();} });
}

function openWebsite() {
  closeQuestion();
  setTimeout("checkFocus()", 500 );
}
function checkFocus() {
  if (document.hasFocus()) {
    showQuestion($('#makebooking'))    
  }else{
    setTimeout("checkFocus()", 500 );
  }
}


function imagePreview() {
  if($("a.preview").length==0) return

    $("body").append("<p id='preview'><img src='' alt='Image preview' id='previmg' /></p>");
    xOffset = 10;
    yOffset = 30;   		
  /* END CONFIG */
  $("a.preview").hover(function(e){
    this.t = this.title;
    this.title = "";	
    //var c = (this.t != "") ? "<br/>" + this.t : "";
    $('#previmg').attr("src", this.rel);
    
    $("#preview")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px")
      .fadeIn("fast");						
    },
  function(){
    this.title = this.t;	
    $("#preview").hide()
    });	
  $("a.preview").mousemove(function(e){
    $("#preview")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px");
  });			
};


