/*
 * jQuery CareWorks Media Gallery Plugin v 1.0.0
 * http://www.caretech.com
 *
 * Requires:	Nivo Plugin http://nivo.dev7studios.com
 *
 * April 2011
 */

(function ($) {
    $.fn.CWMediaGallery = function (options) {

        //Defaults are below
        var settings = $.extend({}, $.fn.CWMediaGallery.defaults, options);

        return this.each(function () {
            var cwgallery = $(this);
            var mgid = cwgallery.attr("mgid");
            trace("Gallery ID: " + mgid);

            if (mgid == undefined || mgid <= 0)		//get out if we don't have an id
            {
                trace("Gallery ID not Found");
                return this;
            }

            //we have a gallery id - load the xml and attempt to retrieve the data
            //download XML file
            $.ajax({
                type: "GET",
                url: "/upload/MediaGallery/Xml/MediaGallery" + mgid + ".xml", 	//url: "/Upload/MediaGallery/Xml/MediaGallery" + mgid + ".xml",
                dataType: "XML",				//use 'text' since XML is not always valid when saved
                success: function (xml) {
                    trace("XML Downloaded");
                    //Currently just use support for a single image

                    $(xml).find("album").each(function () {
                        var imgpath = $(this).attr("lgPath");
                        trace("Loading images from: " + imgpath);

                        //loop through each image and form html to add to slider
                        //$("#sliderMG-" + mgid).html("");		//maybe remove this
                        $(this).find("img").each(function (index) {
                            //combine these into an object
                            var imgsrc = $(this).attr("src");
                            var imglink = $(this).attr("link");
                            var imgtarget = $(this).attr("target") || "_self";
                            var imgpause = $(this).attr("pause");
                            var imgtitle = $(this).attr("title");
							
							if(index == 0)
							{
								var learn_more = $('.h_banner_text .learn_more');
								
								learn_more.attr('href', function (){return imglink;});
							}

                            trace("Loading Image: " + imgsrc);

                            if (imglink != "") {
                                cwgallery.append("<a href=\"" + imglink + "\" target=\"" + imgtarget + "\"><img title=\"\" src=\"" + imgpath + imgsrc + "\" /></a>");
                            } else {
                                cwgallery.append("<img src=\"" + imgpath + imgsrc + "\" title=\"\" />");
                            }
                        });
                    });

                    cwgallery.nivoSlider(settings);

                    $(".ppnav").toggle(
					function () {
					    $(this).html(settings.playText);
					    cwgallery.data('nivoslider').stop();
					},
					function () {
					    $(this).html(settings.pauseText);
					    cwgallery.data('nivoslider').start();
					}
				);

                }
            });
        });
    };

    // For debugging
    var trace = function (msg) {
        if (this.console && typeof console.log != "undefined")
            console.log(msg);
    }
    //Default settings for nivo slider - can be overriden when loading media gallery
       $.fn.CWMediaGallery.defaults = {
       	effect: 'fade',
       	//effect: 'slideLeft',
        slices: 15,
        boxCols: 8,
        boxRows: 4,
        animSpeed: 500,
        pauseTime: 5000,
        startSlide: 0,
        directionNav: false,
        directionNavHide: true,
        controlNav: true,
        controlNavThumbs: false,
        controlNavThumbsFromRel: false,
        controlNavThumbsSearch: '.jpg',
        controlNavThumbsReplace: '_thumb.jpg',
        keyboardNav: false,
        pauseOnHover: true,
        manualAdvance: false,
        captionOpacity: 0.8,
        prevText: 'Prev',
        nextText: 'Next',
        playPauseNav: true,
        playText: 'PLAY',
        pauseText: 'PAUSE',
        beforeChange: function () { },
        afterChange: function () { 
			var learn_more = $('.h_banner_text .learn_more');
			
			learn_more.attr('href', function (){
			return $($('.nivo-imageLink').get($('.nivoSlider').data('nivo:vars').currentSlide)).attr('href')});
		},
        slideshowEnd: function () { },
        lastSlide: function () { },
        afterLoad: function () { }
    };

})(jQuery);
