var closeTimer //used for mouseover close events
var loc = document.location + ""
var slideDelay = 8000
var slideTimer
var homeSlidePos = 0
var jsNothing = "javascript://"
var dialogCache
var dialogSource

$(document).ready(function() { init() })

//main event initilization
function init() {
	if ($('li.question').length) {
		faqPanels()
	}
	
	$('.readMore').click(function() {
		$(this).parent().parent().parent().find('.detail').toggle()
		$(this).parent().parent().parent().find('.shortContent').html("")	
		//$(this).html("")
	})

	$('.section, .sectionBot').hover(function() {
		$(this).toggleClass('hiLite');
	}, function() {
		$(this).toggleClass('hiLite')
	}
    ).css({ cursor: 'pointer' });

	$('td.msgShowEmail').hover(function() {
		$(this).toggleClass('tblOverRow');
	}, function() {
		$(this).toggleClass('tblOverRow')
	}
    ).css({ cursor: 'pointer' });

	$('.dashboardBan ul li h2').hover(function() {
		$(this).toggleClass('liOverItem');
	}, function() {
		$(this).toggleClass('liOverItem')
	}
    ).css({ cursor: 'pointer' });

	$('.dashboardBan ul li h2 a').hover(function() {
		$(this).toggleClass('liOverItem');
	}, function() {
		$(this).toggleClass('liOverItem')
	}
    ).css({ cursor: 'pointer' });

	$('.commContainer table.comMsg .msgItem').click(function() {
		$('.messageBox').show();
		$('#overlay').show();
		$('body').css("overflow", "hidden");
	});

	$('#memID').hover(function() {
		$(this).find("div.checkValid").toggleClass('validNo');
	}, function() {
		$(this).find("div.checkValid").toggleClass('validNo')
	}
    ).css({ cursor: 'pointer' });

	$("img.closeBtn2").click(function() {
		$('.messageBox').hide();
		$('#overlay').hide();
		$('body').css("overflow", "show");
	});

	$('.profileBox table tr').hover(function() {
		$(this).toggleClass('tblOverRow');
	}, function() {
		$(this).toggleClass('tblOverRow')
	}
    ).css({ cursor: 'default' });
    
}



//utility functions - common library
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

	$('#overlay').css('height', $(document.body).height() + 'px')
	$('#overlay').show()
//	$('#dialog').html($(element).html())
//	dialogCache = $(element).html()
//	dialogSource = element
	$(element).html("")
	    

	centerMe('#dialog')
	$('#dialog .close').attr('href', 'javascript:closeDialog()')
	$('#dialog .print').attr('href','javascript:;//')
	$('#dialog .print').click(function() { printDialog() })

	$('#dialog a').each(function() {
	    if ($(this).attr('href') == "#" || $(this).attr('href') == "") {
	        $(this).attr('href', jsNothing)
	    }
	})


	$('#dialog').show();
	//checkScroll()
    //clearScroll()

}

function closeDialog() {
	$('#overlay').hide()
	//$(dialogSource).html(dialogCache)
	$('#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', '130px')
	$(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 faqPanels() {
	$('li.question a').click(function() {

		//$('#listFAQ .answer').attr('style', 'display:none;');
		//$(this).next('.answer').slideDown('slow');


		$('#listFAQ .answer:visible').slideUp('slow');
		$(this).next('.answer').slideDown('slow');
	})
}

//testing

function openDialog1(element) {
	//this is the general dialog handler.
	//pass the element name and this will copy
	//the contents of the element to the dialog box

	$('#overlay').css('height', $(document.body).height() + 'px')
	$('#overlay').show()
	$('#dialog').html($(element).html())
	centerMe('#dialog')
	$('#dialog').show();
}

function closeDialog1() {
	$('#overlay').hide()
	$('#dialog').hide()
	$('#dialog').html('')
}

//expressions gallery testing


function rotateImage() {	
	var items = $('ul.expressioncardwrapper li');
	if (items.filter(':visible').length == 1) { // If only one item is visible
		items.slideDown(400); // Return to the start by revealing them all
		$(".nextItemBtn").html("See more Memorial Cards")
		$(".nextItemBtn").attr('href', 'https://gideons.ca/secure/Memorial-Cards/')
	}
	else { 
		items.filter(':visible:first').slideUp(400);
		$(".nextItemBtn").html("See more Occasion Cards")
		$(".nextItemBtn").attr('href','https://gideons.ca/secure/Occasion-Cards/')
	}
	window.setTimeout(function() { rotateImage() }, 5000)
}

window.setTimeout(function() { rotateImage() }, 5000)

function doSearch(searchTerm) {
	document.location = '/Search.aspx?st=' + searchTerm;
	return false;
}


//End testing

// site-specific code


