//SITE4 CUSTOM Messaging system - based on JQUERY 2010 - 2011
//Written by: Kenneth Millard

//$html = header('Content-Type: text/html; charset=utf-8');
//$html = header('Content-Type: text/html; charset=iso-8859-1');

//SETTING UP WINDOWS POPUP   
//0 means disabled; 1 means enabled;   
var site4CustomWindowStatus = 0;

//remember active divs..
var parent_div_id = '';
var nTriggerCount = 0;

$.ajaxSetup({
    cache: false
})


//CAPTURE A TAG TO MESSAGE WINDOW
$(document).ready(function() {
    $('a[rel*=site4window], a[class*=site4window],a[link*=site4window],a[id*=site4window],a[target*=site4window]').live('click', function() {
        SITE4window_ctrl(this);
        return false;
    });

});

function loadSite4WaitMessage() {

  $("body").append("<div id='SITE4_wait' style='position:absolute;display:none;'><img src='/version30/messaging/loading.gif'></div>");

  //centering
  $('#SITE4_wait').css({ 
      "position": "absolute",
      "z-index": "9998",
      "width": "250px",
      "height": "200px",
      "padding-top": "50px",
      "text-align": "center",
      "border": "0px solid #cecece"      
  });

  //center the window
  var nTop = ($(window).height() - $('#SITE4_wait').height()) / 2 + $(window).scrollTop();
  var nLeft = ($(window).width() - $('#SITE4_wait').width()) / 2 + $(window).scrollLeft();
  $('#SITE4_wait').css("top", nTop + "px");
  $('#SITE4_wait').css("left", nLeft + "px");

  $('#SITE4_wait').fadeIn(500);
  
}

function removeSite4WaitMessage() {
  $('#SITE4_wait').topZIndex();
  $('#SITE4_wait').fadeOut(500);
  $('#SITE4_wait').remove();
}

//centering popup
function centerWindowMessage() {

    //centering
    $('#SITE4_overlay').css({
        "position": "absolute",
        "z-index": "9998"
    });

    //center the window
    var nTop = ($(window).height() - $('#SITE4_overlay').height()) / 2 + $(window).scrollTop();
    var nLeft = ($(window).width() - $('#SITE4_overlay').width()) / 2 + $(window).scrollLeft();


    //Exceeds window width 
    if ($('#SITE4_overlay').width() > $(window).width()) {
        $('#SITE4_overlay').css({
            "width": $(window).width() * 0.90,
            "overflow": "auto",
            "overflow-y": "hidden"
        });
        nLeft = 100;
    }

    //Exceeds window height
    if ($('#SITE4_overlay').height() > $(window).height()) {
        nTop = 50;
    }
    
    $('#SITE4_overlay').css("top", nTop + "px");
    $('#SITE4_overlay').css("left", nLeft + "px");

    //place the close button: SITE4_overlay_close
    $('#SITE4_overlay_close').css({
        "position": "absolute",
        "z-index": "9999",
        "color": "red",
        "padding": "0px",
        "font-size": "14px",
        "font-family": "verdana",
        "font-weight": "bold",
        "cursor": "hand"
    });

    //place the close button
    $('#SITE4_overlay_close').css("top", nTop - 11 + "px");
    $('#SITE4_overlay_close').css("left", nLeft + $('#SITE4_overlay').width() - 12 + "px");

}



//loading windows with jQuery 
function loadWindowMessage(){
  //loads popup only if it is disabled
  if(site4CustomWindowStatus==0){

     $('#SITE4_overlay').css({
         "z-index": "999"
     });


     $('#SITE4_overlay').fadeIn(500);
   
     $('#SITE4_overlay_close').fadeIn(500);
     $('#SITE4_overlay_close').topZIndex();

     $('#SITE4_background').css({
         "opacity": "0.7",
         "z-index": "998",
         "height": $(document).height(),
         "width": $(document).width()
     });

     $('#SITE4_background').fadeIn(500);

     site4CustomWindowStatus = 1;
 
}
}

function SITE4Window_populate_div() {

    //Wrapper
    $(parent_div_id).wrap("<div id='SITE4_placeholder' />");
    $(parent_div_id).appendTo('#SITE4_overlay');
    $(parent_div_id).show();

}

function SITE4Window_populate_image(){

    //bug onload in img tag loads twice in IE
    nTriggerCount++;
    if (nTriggerCount == 2) { return false; }

    var nTargetHeight = $($('#SITE4_overlay_image_id')).height();
    var nTargetWidth = $($('#SITE4_overlay_image_id')).width();

    SITE4Window_add(nTargetHeight, nTargetWidth);
    $('#SITE4_overlay_image').appendTo('#SITE4_overlay');
    $('#SITE4_overlay_image').show();
    $('#SITE4_overlay_image').click(SITE4Window_remove);

    centerWindowMessage();
    loadWindowMessage();

}

function SITE4Window_populate_iframe() {

    //bug onload in img tag loads twice in IE
    nTriggerCount++;
    if (nTriggerCount == 2) { return false; }

    var nTargetHeight = $($('#SITE4_overlay_iframe')).height();
    var nTargetWidth = $($('#SITE4_overlay_iframe')).width();

    SITE4Window_add(nTargetHeight, nTargetWidth);
    $('#SITE4_overlay_iframe').appendTo('#SITE4_overlay');
    $('#SITE4_overlay_iframe').show();

    centerWindowMessage();
    loadWindowMessage();

    $('#SITE4_overlay_iframe_id').css("width", $('#SITE4_overlay').width() + "px");
    $('#SITE4_overlay_iframe_id').css("height", $('#SITE4_overlay').height() + "px");

}

//SPECIFIC function for the custom site4 windows
function SITE4Window_remove() {
 
  $("#SITE4_overlay").fadeOut(1000);
  $("#SITE4_overlay").unbind("click");
  $("#SITE4_background").fadeOut(1000);
  $("#SITE4_overlay_close").fadeOut(1000);
 
  SITE4Window_depopulate();

  $('#SITE4_overlay_close').trigger("unload").unbind().remove();
  $('#SITE4_overlay').trigger("unload").unbind().remove();
  $('#SITE4_background').trigger("unload").unbind().remove();
  
  site4CustomWindowStatus = 0;
  document.onkeydown = "";
  document.onkeyup = "";
  return false;
}

function SITE4Window_add(height,width) {

  if(document.getElementById("SITE4_overlay") != null){
    SITE4Window_remove();
  }
  $("body").append("<div id='SITE4_background' style='position:absolute;height:100%;width:100%;background-color:#000000;display:none;top:0;left:0;'></div>");
  $("body").append("<div id='SITE4_overlay_close' style='position:absolute;display:none;'><img src='/version30/messaging/closebox.png' style='border:none;'></div>");
 
  if(height==0||width==0){
      $("body").append("<div id='SITE4_overlay' style='background-color:#ffffff;display:none;border:2px solid #cecece;'></div>"); 
  }
  else {
      $("body").append("<div id='SITE4_overlay' style='height:" + height + "px;width:" + width + "px;background-color:#ffffff;display:none;border:2px solid #cecece;'></div>");
  }
  
  $('#SITE4_background').click(SITE4Window_remove);
  $('#SITE4_overlay_close').click(SITE4Window_remove);
 
}

function SITE4Window_close() {
 SITE4Window_remove();
}

function SITE4Window_depopulate() {

   $(parent_div_id).hide();
   $(parent_div_id).appendTo('#SITE4_placeholder'); 
   $(parent_div_id).unwrap("<div id='SITE4_placeholder' />");
 
   //if image div - remove the div for containing the image
   $('#SITE4_overlay_image_id').remove();
   $('#SITE4_overlay_image').remove();

   //if iframe div - remove the div for containing the image
   $('#SITE4_overlay_iframe_id').remove();
   $('#SITE4_overlay_iframe').remove();

}

//load the href from SITE4 deluxe menu system
function SITE4window_deluxe_menu(url) {
    var atag = document.createElement("a");
    $(atag).attr("href", url);
    SITE4window_ctrl(atag);
}

//load the href from SITE4 deluxe menu system
function SITE4window_open(url) {
    var atag = document.createElement("a");
    $(atag).attr("href", url);
    SITE4window_ctrl(atag);
}



function checkIsImage(obj) {
  var imageTypes = ['.png', '.jpg', '.jpeg', '.gif'];
  var ext = obj.href.substring(obj.href.length - 4).toLowerCase();
  if (new RegExp('^(' + imageTypes.join('|') + ')$').test(ext)) {
      return true; 
  }
  else {
     return false;
  }
}

function checkIsDocument(obj) {
    var imageTypes = ['.pdf', '.wri', '.doc'];
    var ext = obj.href.substring(obj.href.length - 4).toLowerCase();
    if (new RegExp('^(' + imageTypes.join('|') + ')$').test(ext)) {
        return true;
    }
    else {
        return false;
    }
}

function isExternalHRef(obj) {
    var hTypes = ['http://'];
    var ext = obj.href.substring(0,7).toLowerCase(); 
    if (new RegExp('^(' + hTypes.join('|') + ')$').test(ext)) {
        if (CheckIfExternalUrl(obj)){
            return true;
        } else {
            return false;
        }
    }
    else {
        return false;
    }
  return false;
}

function CheckIfExternalUrl(obj) {
    hostname = new RegExp(location.host);
    var url = obj.href;
    if (hostname.test(url)) {
        return false;
    }
    else {
        return true;
    }
    return false
}



function SITE4window_ctrl(obj){

    var isDiv = false;
    var isAjax = false;
    var isImage = false;
    var isDocument = false;
    var isExternalUrl = false;

   //alert(obj.href);

    //check if call is empty
    if (obj.href == '') {
        alert("please call SITE4 Window with HREF parameter!");
        return false
    }
   
    //count trigger calls
    nTriggerCount = 0;

    //Show wait..
    loadSite4WaitMessage();
    
    //determine the type of call (div,image,other treated witha ajax)

     //DIV
     var url = window.location.href.split('#')[0];
     parent_div_id = obj.href.replace(url,'');

     //set type
     if (parent_div_id.charAt(0) == '#') {
         isDiv = true;
     }
     //IMAGE (add in div and treat as div)
     else if (checkIsImage(obj)) {
         isImage = true;
     }
     
     //Embed document in iframe
     else if (checkIsDocument(obj)) {
         isDocument = true;
     }
     //Embed document in iframe
     else if (isExternalHRef(obj)) {
         isExternalUrl = true;
     }     
     //AJAX or other..
     else {
         isAjax = true;
     }

     //AJAX
     if (isAjax) {

         //use href and load the page with ajax load in site4
         url = parent_div_id;

         url = url.replace(/func=page/gi, "func=page2");
         url = url.replace(/func=product/gi, "func=product2");
         url = url.replace(/func=products/gi, "func=products2");
         url = url.replace(/func=news/gi, "func=news2");
         url = url.replace(/func=contact/gi, "func=contact2");
         url = url.replace(/func=shopbasket/gi, "func=shopbasket2");
         url = url.replace(/func=productentry/gi, "func=productentry2");
         url = url.replace(/func=shopsearch/gi, "func=shopsearch2");
         url = url.replace(/func=customer_form/gi, "func=customer_form2");

         SITE4Window_add(0, 0);

         $('#SITE4_overlay').load(url, function(response, status, xhr) {
             if (status == "error") {
                 var msg = "SITE4 Window Error: ";
                 $("#error").html(msg + xhr.status + " " + xhr.statusText);
             }
             parent_div_id = "";
             centerWindowMessage();
             loadWindowMessage();
             removeSite4WaitMessage();
         });

     }
     //DIV
     else if (isDiv) {

         //# show div
         var nTargetHeight = $(parent_div_id).height();
         var nTargetWidth = $(parent_div_id).width();
         SITE4Window_add(nTargetHeight, nTargetWidth);
         SITE4Window_populate_div();

         centerWindowMessage();
         loadWindowMessage();
         removeSite4WaitMessage();
     }

     //IMAGE
     else if (isImage) {
         $("body").append("<div id='SITE4_overlay_image' style='display:none;'>" +
          "<img id='SITE4_overlay_image_id' onload='SITE4Window_populate_image();' src='" + obj.href + "'></div>");
         //load is done from the load event in the image tag

         $('#SITE4_overlay_image_id').load(function() {
             removeSite4WaitMessage();
         });

         return false;
     }

     //IMAGE
     else if (isExternalUrl) {
         $('#SITE4_overlay_iframe_id').remove();
         $('#SITE4_overlay_iframe').remove();
         $("body").append("<div id='SITE4_overlay_iframe' style='display:none;width:80%;height:80%'>" +
                         "<iframe id='SITE4_overlay_iframe_id' onload='' style='width:100%;height:100%;' src='" + obj.href + "' frameborder='0' scrolling='auto'></iframe></div>");

         $('#SITE4_overlay_iframe_id').load(function() {
             SITE4Window_populate_iframe();
             removeSite4WaitMessage();
         });

         return false;
     }

     //DOCUMENT
     else if (isDocument) {
         $('#SITE4_overlay_iframe_id').remove();
         $('#SITE4_overlay_iframe').remove();
         $("body").append("<div id='SITE4_overlay_iframe' style='display:none;width:80%;height:90%'>" +
                             "<iframe id='SITE4_overlay_iframe_id' onload='' style='width:100%;height:100%;' src='' frameborder='0' scrolling='auto'></iframe></div>");
         SITE4Window_populate_iframe();
         $('#SITE4_overlay_iframe_id').attr("src", obj.href);

         removeSite4WaitMessage();
         return false;
     }  
      
}


