﻿$(document).ready(function(){
  $.preloadCssImages();
});


//SCRIPT FOR HOME PAGE MOVEMENT
var showrunning = 1;
var counter = 0;
var timer;

$(document).ready(function() {
    $('.homemainarea').mouseenter(function() {
        var thisid = $(this).attr("id");
        var thisnum = thisid.charAt(thisid.length - 1);
        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 prevnum = 8
    if (idnum > 1) {
        prevnum = idnum - 1;
    }
    var oldimage = $('.homeadsection').find('#homemain' + prevnum);
    var newimage = $('.homeadsection').find('#homemain' + idnum);

    newimage.fadeIn('slow', function() {
        oldimage.fadeOut();
    });
}    
    
function preLoadThis(str) {
    var cacheImage = new Image();
    cacheImage.src = str;
}


//SCRIPT FOR FEATURE BACKGROUND HOVER IMAGE
$(document).ready(function() {
    var $el, leftPos, newWidth;
    var $highlight = $('.menubarhighlight');
    $highlight.width($('.slidingmenu').width() - 10)
    .css('left', $('.slidingmenu').position().left + 5)
    .data('origLeft', $highlight.position().left)
    .data('origWidth', $highlight.outerWidth());

    $('.slidingmenu').hover(function() {
        $el = $(this);
        leftPos = $el.position().left + 5;
        newWidth = $el.width() - 10;
        $highlight.stop().animate({
            width: newWidth,
            left: leftPos
        }, 500, 'swing');
    }, function() {
        $highlight.stop().animate({
            left: $highlight.data('origLeft'),
            width: $highlight.data('origWidth')
    }, 500, 'swing');
    });
});





//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;
