﻿$(document).ready(function(){
  $.preloadCssImages();
});

//$(document).ready(function() {
//    $(document).pngFix();
//});



//SCRIPT FOR DELIVERY BANNER FADING
var fadetimer;
$(document).ready(function() {
    FadeInDeliveryBanner();
});

function FadeInDeliveryBanner() {
    $('#freedelivery').fadeIn('slow', function() {
        $('#freedelivery').fadeOut('slow');
        $('#freedelivery2').fadeIn('slow', function() {
            window.clearTimeout(fadetimer);
            fadetimer = window.setTimeout('FadeOutDeliveryBanner();', 1000);
        });
    });
}
function FadeOutDeliveryBanner() {
    $('#freedelivery2').fadeOut('slow', function() {
        window.clearTimeout(fadetimer);
        fadetimer = window.setTimeout('FadeInDeliveryBanner();', 500);
    });
}

//SCRIPT FOR HOME PAGE MOVEMENT
var showrunning = 1;
var counter = 0;
var timer;

$(document).ready(function() {
    $('.homethumb').mouseenter(function() {
        var thisid = $(this).attr("id");
        var thisnum = thisid.charAt(thisid.length - 1);
        selectPic(thisnum);
        ShowPause(thisnum);
    });
    for (var i = 1; i<9; i++) {
        preLoadThis('/commonimages/homemain' + i + '.jpg');
    }
    NextImage();
});
function NextImage() {
    if (showrunning == 1) {
        counter++;
        if (counter > 8) {
            counter = 1;
        }
        selectPic(counter);
        timer = window.setTimeout("NextImage();", 5000);
    }
}
function ShowPause(thisnum) {
    showrunning = 0;
    counter = thisnum;
    window.clearTimeout(timer);
    timer = window.setTimeout("ShowStart();", 8000);
}
function ShowStart() {
    showrunning = 1;
    NextImage();
}
function selectPic(idnum) {
    var thumbs = $('.homeadsection').find('.homethumb');
    var thisthumbid = '#homethumb' + idnum
    var thisthumb = $('.homeadsection').find(thisthumbid);
    var link = thisthumb.attr('href');
    thumbs.stop();
    thisthumb.fadeTo('slow', 0.6);
    thumbs.not(thisthumb).fadeTo('slow', 1);
    $('#homemain').stop();
    $('#homemainnew').remove();
    $('.homemainarea').prepend('<img src="commonimages/homemain' + idnum + '.jpg" id="homemainnew" />');
    $('#homemain').fadeOut('slow', function() {
        $('#homemain').remove();
        $('#homemainnew').attr('id', 'homemain');
        $('.homemainarea').attr('href', link);
    });
}
function preLoadThis(str) {
    var cacheImage = new Image();
    cacheImage.src = str;
}





//SCRIPT FOR MOVABLE WINDOWS
var req;
var selObj = null;
var orgCursor = null;   // The original Cursor (mouse) Style so we can restore it
var dragOK = false;     // True if we're allowed to move the element under mouse
var dragXoffset = 0;    // How much we've moved the element on the horozontal
var dragYoffset = 0;    // How much we've moved the element on the verticle

function ajax_request(toppos, leftpos, callfn) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        req = new XMLHttpRequest();
    }
    var url = "AJAX_Servlet.aspx?function=" + callfn + "&top=" + toppos + "&left=" + leftpos + "";
    req.open('post', url, true);
    req.send();
}

function moveHandler(e) {
    if (e == null) { e = window.event }
    if (e.button <= 1 && dragOK) {
        selObj.style.left = e.clientX - dragXoffset + 'px';
        selObj.style.top = e.clientY - dragYoffset + 'px';
        return false;
    }
}

function cleanup(e) {
    document.onmousemove = null;
    document.onmouseup = null;
    selObj.style.cursor = orgCursor;
    dragOK = false;
    if (selObj.className == "sitecontroller") {
        ajax_request(selObj.style.top, selObj.style.left, 'saveSiteControllerLocation')
    } else if (selObj.className == "mysuitebox") {
        ajax_request(selObj.style.top, selObj.style.left, 'saveMySuiteLocation')
    } else if (selObj.className == "advancedsearchwindow") {
        ajax_request(selObj.style.top, selObj.style.left, 'saveAdvancedSearchLocation')
    }
}

function dragHandler(e) {
    var htype = '-moz-grabbing';
    if (e == null) { e = window.event; htype = 'move'; }
    var target = e.target != null ? e.target : e.srcElement;
    selObj = target;
    orgCursor = target.style.cursor;
    if (target.className == "mysuitebox" || target.className == "sitecontroller" || target.className == "advancedsearchwindow") {
        target.style.cursor = htype;
        dragOK = true;
        dragXoffset = e.clientX - parseInt(selObj.style.left);
        dragYoffset = e.clientY - parseInt(selObj.style.top);
        document.onmousemove = moveHandler;
        document.onmouseup = cleanup;
        return false;
    }
}

document.onmousedown = dragHandler;