

// Moveable library 1.1 October 2010

//utility functions - common library

var slideTimer

function findPos(obj) {
	//returns x,y coordinates of an element
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft, curtop];
}

function closeInit(element) {
	//timer for mouseout on panels
	clearTimeout(closeTimer)
	closeTimer = window.setTimeout(function() { closePanel(element) }, 400)
}

function closePanel(element) {
    if ($(element).hasClass('dropMenu')) {
	    $(element).siblings().find('a').removeClass('active')
	}
	$(element).fadeOut('fast', function() { $(element).parent().find('.menuGradient').remove() });
}

function mousePanel(src, panel, initFunction) {
	$(src).mouseover(function() { window[initFunction](src) })
	$(src).mouseout(function() { closeInit(panel) })
	$(panel).mouseover(function() { clearTimeout(closeTimer) })
	$(panel).mouseout(function() { closeInit(panel) })
}

function openDialog(element) {
	//this is the general dialog handler.
	//pass the element name and this will copy
	//the contents of the element to the dialog box
	if (!$('#overlay').length) {
		$(document.body).append('<div id="overlay" onclick="closeDialog()"></div>')
	}
	if (!$('#dialog').length) {
		$(document.body).append('<div id="dialog"></div>')
	}
	
	$('#overlay').css('height', $(document.body).height() + 'px')
	$('#overlay').show()
	$('#dialog').html($(element).html())
	dialogSource = element
	centerMe('#dialog')
	$('#dialog').show();

}

function closeDialog() {
    $('.handle').css('top','0px')
	$('#overlay').hide()
	$('#dialog').hide()
	$('#dialog').html('')
}

function centerMe(element) {
	//pass element name to be centered on screen
	var pWidth = $(window).width();
	var pTop = $(window).scrollTop()
	var eWidth = $(element).width()
	var height = $(element).height()
	$(element).css('top', pTop + 100+'px')
	$(element).css('left', parseInt((pWidth / 2) - (eWidth / 2)) + 'px')
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + escape(value) +
	((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}

function flipSlide() {
    $('#homeImg img').hide()
    $('#caption').hide()
    $('#homeImg img').attr('src', homeSlides.slides[homeSlidePos].img)
    //$('#homeImg img').attr('src', homeSlides.slides[homeSlidePos].img.replace("/Assets", "Assets"))
    $('#captionText').html(homeSlides.slides[homeSlidePos].txt + '&nbsp;<a class="more" onclick="slideDialog()" href="javascript://">MORE</a>')
    $('#homeImg img').fadeIn('slow')
    window.setTimeout(function() { $('#caption').fadeIn('slow') }, 500)
}

function slidePrev() { 
    clearTimeout(slideTimer)
    if (homeSlidePos == 0) {
        homeSlidePos = homeSlides.slides.length-1
    }
    else {
        homeSlidePos--
    }
    $('#slidePos').html(homeSlidePos+1)
    $('#homeImg').css('background-image', 'url(' + $('#homeImg img').attr('src') + ')')
    flipSlide()
    slideTimer = window.setTimeout(function() { runSlideshow() }, slideDelay)
}

function slideNext() {
    clearTimeout(slideTimer)
    homeSlidePos++
    if (homeSlidePos>homeSlides.slides.length-1) {
        homeSlidePos = 0
    }
    $('#slidePos').html(homeSlidePos + 1)
    $('#homeImg').css('background-image', 'url(' + $('#homeImg img').attr('src') + ')')
    flipSlide() 
    slideTimer = window.setTimeout(function() {runSlideshow()}, slideDelay)
}

function fixPath() {
    $('img').each(function() {
        $(this).attr('src', $(this).attr('src').replace("/Assets", "Assets"))
    })
}


function hideLightbox() {
	$('#overlay').hide()
	$('#lightbox').hide()
	$('#lightbox').html('')
}

function lightbox(contents) {
	//pass it html
	//example: lightbox($('#divName').html())
	$('#overlay').css('height', $('#root').height() + 40 + 'px')
	$('#overlay').show()

	//$('#lightbox').css('top', $("body").scrollTop()+50+ 'px')
	$('#lightbox').css('left', '-4400px')
	$('#lightbox').html(contents)
	var newLeft = ($(window).width() / 2) - ($('#lightbox').width() / 2)
	var newTop = ($(window).height() / 2) - ($('#lightbox').height() / 2)
	$('#lightbox').css('left', newLeft + 'px')
	$('#lightbox').css('top', newTop + 'px')
	$('#lightbox').show()
}

function initMenu() {
	$('.nav ul li a').mouseover(function() {
	var target = $(this).parents('li').find('ul')
		if (target.length) {
			target.slideDown('fast')
			target.addClass('open')			
			target.mouseout(function() {
				window.setTimeout(function() { closeMenu(target) }, 200)
			})
		}
	})
	$('.nav ul li a').mouseout(function() {
		var target = $(this).parents('li').find('ul')
		target.removeClass('open')
		window.setTimeout(function() { closeMenu(target) }, 200)
	})

	$('.nav ul li ul').mouseout(function() {
		$(this).removeClass('open')
	})
	$('.nav ul li ul').mouseover(function() {
		$(this).addClass('open')
	})
}

function closeMenu(element) {
	if ($(element).hasClass('open')) {
		return
	}
	$(element).slideUp('fast')
}


