jQuery.fn.exists = function() { return ($(this).length > 0); }

function scrollAfter(curr, next, opts) {
    var index = opts.currSlide;
    $(opts.prev)[index == 0 ? 'hide' : 'show']();
    $(opts.next)[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

function getParameterByName(location, name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(location);
    if (results == null)
        return "";
    else
        return results[1];
}

function getUrlName(location) {
    var regexS = "[\\#]([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(location);
    if (results == null)
        return "";
    else
        return results[0];
}

// OnLoad
$(function() {

    // :first-child
    $(".updates ul li:first-child, .updates-list li:first-child").addClass("first");
    
    // Menu Current
    $('.menu > li').each(function() {
        var winloc = getParameterByName(window.location.href, "iid");
        var itemloc = getParameterByName($(this).children(0).attr("href"), "iid");
        if (itemloc == "") {
            winloc = getUrlName(window.location.href);
            itemloc = getUrlName($(this).children(0).attr("href"));
        }

        if (winloc == itemloc) {
            $(this).children(0).addClass("current");
        }
            
        // Remove
        $(".menu li:nth-child(16)").hide();
        
        // Show Menu
        $(".menu").show();
    });
    
    // Content Header
    var hname = $(".menu li a.current").attr("name");
    $(".content > h2").addClass("header hblue " + hname);    
    
	// Content Double Place
	if ($(".content .text").exists()) {
		if ($(".gray-box").exists()){
			$(".content .text").addClass("white-box");
			$(".content .text").css("display", "block");			
		} else {
			$(".content .text").css("display", "block");
		}
	}

	// memorialScroll > Cycle
    if ( $("#memorialScroll").exists() ){
		if ( $("#memorialScroll div").length == 1 ){
			$("#memorialScroll div").css("display", "block");
		} else {
		    $('#memorialScroll').cycle({
		        fx: 'fade',
		        timeout: 0,
		        speed: 1500,
		        cleartypeNoBg: true,
		        pager: '#navScroll',
		        pagerEvent: 'click',
			    pagerAnchorBuilder: function(idx, slide) {
			        return '#navScroll li:eq(' + idx + ') a';
			    }
		    });
		} 
	}
	
	// galleryScroll > Cycle
    if ( $("#galleryScroll").exists() ){
		if ( $("#galleryScroll img").length == 1 ){
			$("#galleryScroll img").css("display", "block");
			$(".gallery .nav, .gallery .thumbs ").css("display", "none");	
		} else {
		    $('#galleryScroll').cycle({
		        fx: 'fade',
		        timeout: 0,
		        speed: 1500,
        		next: '#next',
        		prev: '#prev',
				after: scrollAfter,
		        cleartypeNoBg: true,
		        pager: '#thumbScroll',
		        pagerEvent: 'click',
			    pagerAnchorBuilder: function(idx, slide) { 
			        return '#thumbScroll li:eq(' + idx + ') a'; 
			    } 
		    });
		} 
	}
	
});
