
function  ccSlides(slides, albums, pages, rotateInterval, fadeSpeed) {
    // 4/9/2009
    // by Jason Denizac @ Synoptek
    //
    // slides, albums, and pages refers to the id of the eponymous containing element
    // in this case, #ccSlides, #ccAlbums, #ccPages

    var self = this;
    
    var Slides = slides;
    var Albums = albums;
    var Pages = pages;

    var rInterval = typeof (rotateInterval) != 'undefined' ? rotateInterval : 5000;
    var Speed = fadeSpeed;

    var SlideOn = true;
	var SlideOnCookie = SlideOn;
    var CurPage = 1; // 1-based index
    var CurAlbum = 1; // 1-based index
    var CurAlbumPages = 1; // 1-based index
    var ccPulse;
	
	var d = false;
	self.log = function Log(msg){
		if(d){
			if(!(typeof(console)=='undefined')){
				console.log(msg);
			}else{
				jQuery(".cc2col").prepend("<br>"+msg);
			}
		}
	}

    self.Start = function Start() {
        self.log("its off, turn it on");
        SlideOn = true;
        jQuery(Slides).stop(1);
        if (typeof (ccPulse) != 'undefined') clearInterval(ccPulse);
        ccPulse = setInterval(self.Next, rInterval);
        
        jQuery("#ccStartStop").text("Slide Show ON").attr("title", "Click to stop");
    }
    self.Stop = function Stop() {
        self.log("its on, turn it off");
        SlideOn = false;

        if (typeof (ccPulse) != 'undefined') clearInterval(ccPulse);
        
        
        if (CurAlbumPages > 1) jQuery("#ccStartStop").text("Slide Show OFF").attr("title", "Click to resume");
    }

    self.Restart = function Restart() {
        self.Stop();
        self.Start();
    }
	
	jQuery.getHash = function(url){
	var ind = url.indexOf('#');
	var out;
	if(ind != -1)	out = url.substr(url.indexOf('#')+1);
	else out = url;
	
	return out;
	}


    self.setupAlbums = function setupAlbums() {

    SlideOn = (jQuery.cookie("ccSlideOn") == "false") ? false : true;
	SlideOnCookie = SlideOn;


    if (jQuery(Slides + " li:visible").length > 1) {
        jQuery(Slides + " li:not(:first)").hide();
        self.log("hiding all elements other than the first");
    }
    
    
        if (SlideOn) {
            jQuery("#ccAlbums").append('<li class="lmCurPage lmFirst"><a href="#" id="ccStartStop" title="Click to stop">Slide Show ON</a></li>');
        } else {
            jQuery("#ccAlbums").append('<li class="lmCurPage lmFirst"><a href="#" id="ccStartStop" title="Click to resume">Slide Show OFF</a></li>');
        }

        jQuery("#ccStockSlides h5").children().appendTo("#ccAlbums").wrap("<li></li>");
        
        jQuery("#ccAlbums a[rel=ccAlbum]").click(function() {
            var al = jQuery.getHash(jQuery(this).attr("href"));
            self.switchAlbum(al);
            return false;
        });

        jQuery("#ccStartStop").click(function() { try { toggleSlideShow(this); } catch (e) { console.log(e); } return false; });

    } //setupAlbums()


    self.switchAlbum = function switchAlbum(album) {
        // parameter album should the rel attribute of the OL containing the album
        self.log("swichalbum " + album + " called");

        CurAlbum = album;
        jQuery(Albums + " li").removeClass("lmCurPage");

        jQuery(Albums + " a[href=#"+album+"]").parent().addClass("lmCurPage");
        var al = jQuery("#ccStockSlides li[rel=" + album + "]")
        self.log("Album: "+al);
        var pageout = ""
        for (i = 1; i <= al.children("ol").children("li").length; i++) {
            pageout += "<li><a href='#" + i + "'>" + i + "</a></li>";
        }

        CurAlbumPages = i - 1;
		self.log("curalbumpages: "+CurAlbumPages);

        jQuery("#ccPages").empty().append(pageout).children().eq(0).addClass("lmCurPage");
        jQuery("#ccPages a").click(function() {
            var pg = jQuery.getHash(jQuery(this).attr("href"));
            self.log("link page: "+pg);
            try {
                self.switchPage(pg);
                if (self.SlideOn) self.Restart();
                self.log("link switched ok");
            }
            catch (e) { self.log("link switch error: "+e); }
            return false;
        });

        jQuery("#ccSlides").empty().hide().append(al.children("ol").children().clone()).show();
		CurPage=1;
		if(parseInt(CurAlbumPages) >= 2){
		                if(SlideOnCookie) self.Restart();
                self.log("2 or more slides, set rotator");
		}else if(parseInt(CurAlbumPages) == 1){
		                self.log("just 1 slide, display static");
                self.Stop();
		}
    }                 // function switchalbum()

    function toggleSlideShow(switchid) {
        var sw = jQuery(switchid);
        if (sw.text().match("ON")) {
            self.Stop();
	    jQuery.cookie("ccSlideOn","false");
        } else {
            self.Start();
	    jQuery.cookie("ccSlideOn",null)
        }
    } //function toggleSlideShow()


    self.Next = function Next() {
        self.switchPage("next");
    }

    self.Prev = function Prev() {
        self.switchPage("prev");
    }

    //args: "next", "prev", or integer
    self.switchPage = function switchPage(page) {
	if(SlideOn) self.Restart();
        if (!isNaN(page)) {

            self.log("switchPage " + page + " called:");
            if (jQuery("#ccSlides li").length >= page) {          //check for valid page

                self.log(page+" / "+jQuery("#ccSlides li").length);
                var jqpage = jQuery("#ccSlides li:nth-child(" + page + ")");
                if (page == CurPage) {
                    self.log("selected page is already active");
					//if(SlideOn) self.Restart();
					
                } else {
                    CurPage = page;
                    jQuery("#ccPages li").removeClass("lmCurPage");
                    jQuery("#ccPages li:nth-child(" + page + ")").addClass("lmCurPage");
                    jQuery("#ccSlides li").hide();
                    jQuery("#ccSlides li:nth-child(" + page + ")").fadeIn("slow");
                }
            }
        } else if (page == "next") {
            if (CurPage == CurAlbumPages) {
                switchPage(1);
            } else {
                switchPage(parseInt(CurPage) + 1);
            }
        } else if (page == "prev") {
            if (CurPage == 1) switchPage(CurAlbumPages);
            else switchPage(parseInt(CurPage) - 1);

        }
    }                 //function switchPage
}

jQuery.noConflict();

jQuery(document).ready(function() {
	
	if(jQuery("#ccAdmin").length==0){
    ccS = new ccSlides("#ccSlides","#ccAlbums","#ccPages",5000,"slow"); // note: change to var ccS for scope on production / before golive
    ccS.setupAlbums();
    ccS.switchAlbum("album1");
	jQuery("#ccSlides").click(ccS.Next);
	}
	
    // remove link background images since we're re-doing the hover interaction below
    // (doing it this way retains the CSS default hover states for non-javascript-enabled browsers)
    // we also want to only remove the image on non-selected nav items, so this is a bit more complicated
    jQuery(".ccGlobalNav li:not(.lmCurPage) a").mouseover(function() {
        jQuery(this).parent().addClass("lmCurPage").children().stop(1).fadeTo(0, 1);
    }).mouseout(function() {
        jQuery(this).fadeTo(300, 0, function() { jQuery(this).parent().removeClass("lmCurPage").children().fadeTo(0, 1); });
    });
});

/* jQuery cookie plugin*/
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
/* EOF */