Array.prototype.indexOf=function(obj){     
    var result=-1;     
    for (var i=0;i< this.length;i++){     
        if (this[i]==obj){     
            result=i;     
            break;     
        }     
    }     
    return result;     
} 

Array.prototype.contains=function(obj){     
    return (this.indexOf(obj)>=0);     
} 

Array.prototype.append=function(obj){     
    this[this.length]=obj;       
} 

function RedirectPage(pageUrl, querystring){
    var windowLocation = pageUrl + '?';
    var i = 0;
    if ( querystring.length ){
        for ( i = 0; i < querystring.length; i ++ ){
            windowLocation += querystring[i];            
            if ( i != querystring.length - 1 ){
                windowLocation += '&';
            }
        }
    } 
    else{
        windowLocation += querystring == null ? "" : querystring;
    }
    window.location = windowLocation;
}

/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* @example value = $.getURLParam("paramName");
*/ 
jQuery.extend({
    getURLParam: function(strParamName){
        var strReturn = "";
        var strHref = window.location.href;
        var bFound=false;

        var cmpstring = strParamName + "=";
        var cmplen = cmpstring.length;

        if ( strHref.indexOf("?") > -1 ){
            var strQueryString = strHref.substr(strHref.indexOf("?")+1);
            var aQueryString = strQueryString.split("&");
            for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
                if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
                    var aParam = aQueryString[iParam].split("=");
                    strReturn = aParam[1];
                    bFound=true;
                    break;
                }
            }
        }
        if (bFound==false) 
            return null;
        return strReturn;
    }
});

(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);



(function($){
	$.fn.JGalleryViewer = function(options){
		return this.each(function(i){
			var el = this;
			el.jqthis = $(this);
			el.jqchildren = el.jqthis.find('img');
			el.opts = $.extend({}, JGalleryViewer.defaults, options);
			el.index = i;
			el.totalChildren = el.jqchildren.size();
			el.jqjqviewii = JGalleryViewer.swapOut(el);
			
			el.container = $('<div class="gvIIContainer">').appendTo(el.jqjqviewii);

			el.mainImgContainer = $('<div class="gvIIImgContainer">').appendTo(el.container);
			
			el.image = $('<img style="display:none;"/>').appendTo(el.mainImgContainer)
			.mouseover(
			    function(){
					el.altTextBox.fadeTo("fast",el.opts.titleOpacity);
			    }
		    )
			.mouseout(
			    function(){
				    el.altTextBox.fadeTo("slow",0);
			    }
		    );
		    
			el.loader = $('<div class="gvIILoader"/>').appendTo(el.mainImgContainer);
			el.altTextBox = $('<div class="gvIIAltText"/>').appendTo(el.mainImgContainer);
			el.holder = $('<div class="gvIIHolder"/>').appendTo(el.container);
			
			el.jqthis.after(el.jqjqviewii).remove();
			
			el.imgCw = el.mainImgContainer.width();
			el.imgCh = el.mainImgContainer.height();

			el.jqchildren.each(function(j){
				var jqimage = $(this);
				var tmpimage = this;
				
				tmpimage.index = j;

				var jqdiv = $('<div id="gvIIID'+j+'" class="gvIIItem">')
				.appendTo(el.holder)
				.append('<div class="gvIILoaderMini">');// end : $div
				
				if(el.opts.getUrlBy == 0){
					tmpimage.altImg = jqimage.parent().attr('href');
				}
				else if(el.opts.getUrlBy == 1){
					tmpimage.altImg = el.opts.fullSizePath + tmpimage.src.split('/').pop();
				}
				else if(el.opts.getUrlBy == 2){
					tmpimage.altImg = tmpimage.src.replace(el.opts.prefix,'');
				};
				
			    this.altTxt = jqimage.attr('alt');
			    this.relTxt = jqimage.parent().attr('rel');
				
			    var image = new Image();
			    image.onload = function(){
				    image.onload = null;
				    jqdiv.empty().append(jqimage);
					
				    var margins = JGalleryViewer.center({"w":jqdiv.width(),"h":jqdiv.height()},{"w":image.width,"h":image.height});

				    jqimage.css({marginLeft:margins.l,marginTop:margins.t});
				    var largeImage = new Image();
				    largeImage.onload = function(){
					    largeImage.onload = null;

					    $('<div class="gvIIFlash">').appendTo(jqdiv).css({opacity:".01"})
					    .mouseover(
						    function(){
							    var $f = $(this);
							    $f.css({opacity:".75"}).stop().animate({opacity:".01"},500);
						    }
					    )
					    .click(function(){
							jqimage.trigger('click');
					    }).trigger('mouseover');

					    jqimage.click(function(){
						    JGalleryViewer.view(this,el);		   
					    })
					    .css({marginLeft:margins.l,marginTop:margins.t});

					    if( tmpimage.index  == 0 ){
						    jqimage.trigger('click');
						    jqimage.siblings().trigger('mouseover');
					    };
				    };  // end : largeImage.onload 
				    largeImage.src = tmpimage.altImg;
			    };// end : image.onload 
			    image.src = tmpimage.src;
			});
		});
	};

	JGalleryViewer = {
		//pDem parent deminsions
		//iDem img deminsions
		resize : function(pDem,iDem){
			if (iDem.w > pDem.w) {
				iDem.h = iDem.h * (pDem.w / iDem.w); 
				iDem.w = pDem.w; 
				if (iDem.h > pDem.w) { 
					iDem.w = iDem.w * (pDem.h / iDem.h); 
					iDem.h = pDem.h; 
				};
			} else if (iDem.h > pDem.h) { 
				iDem.w = iDem.w * (pDem.h / iDem.h); 
				iDem.h = pDem.h; 
				if (iDem.w > pDem.w) { 
					iDem.h = iDem.h * (pDem.w /iDem.w); 
					iDem.w = pDem.w;
				};
			};
			
			return iDem;
		},
		center : function(pDem,iDem){
			return { "l":(pDem.w-iDem.w)*.5, "t": (pDem.h-iDem.h)*.5 };
		},
		swapOut : function(el){
			return $('<div id="jqgvii'+el.index+'">');
		},
		view : function(img, el){
			if(typeof img.altImg == 'undefined') 
			    return false;
			var url = img.altImg;
			if(/picasa/.test(url)){
				url = /\?/.test(img.altImg) ? '&imgmax=800' : '?imgmax=800';
			};
			
			el.loader.show();		

			image = new Image();
			image.onload = function(){
				image.onload = null;
				dem = {};
				dem.w = $wOrg = image.width;
				dem.h = $hOrg = image.height;
				dem = JGalleryViewer.resize({"w":el.imgCw,"h":el.imgCh},{"w":dem.w,"h":dem.h});

				var margins = JGalleryViewer.center({"w":el.imgCw,"h":el.imgCh},{"w":dem.w,"h":dem.h});
	            
	            el.altTextBox.fadeTo("slow",el.opts.titleOpacity);
	            
				el.image.css({width:dem.w,height:dem.h/*, marginLeft:margins.l,marginTop:margins.t*/});
				
				el.loader.fadeOut('fast');
				
				el.image.fadeOut('fast',function(){
					el.image.attr('src',url).fadeIn();
                    el.altTextBox.html('<div class="alttexttitle">' + img.relTxt + '</div><div>' + img.altTxt + '</div>');
				});
				
				el.altTextBox.fadeTo("slow",0);
			};
			image.alt = img.altTxt;
			image.title = img.relTxt;
			image.src = url;
		},
		defaults : {
			getUrlBy : 0, // 0 == from parent A tag | 1 == the full size resides in another folder
			fullSizePath : null,
			prefix: 'thumbnail.',
			titleOpacity : .60
		}
	};
})(jQuery);

function Slide(title, description, image){
    this.title = title;
    this.description = description;
    this.image = image;
    return true;
}

function Album(title, description, image){
    this.title = title;
    this.description = description;
    this.image = image;
    this.slides = new Array();
    return true;
}

function PhotoGallery(){
    this.index = 0;
    this.albums = new Array();
    return true;
}

var pg = new PhotoGallery();

function loadGallery(){

    for(var a in pg.albums[pg.index].slides){
        if(pg.albums[pg.index].slides[a].image != undefined){
            var li = $('<li></li>');
            var anchor = $('<a></a>');
            var img = $('<img/>');

            img.attr('src', pg.albums[pg.index].slides[a].image);
            img.attr('alt', pg.albums[pg.index].slides[a].description);
            img.attr('border', '0');

            anchor.attr('href', pg.albums[pg.index].slides[a].image);
            anchor.attr('title', pg.albums[pg.index].slides[a].description);
            anchor.attr('rel', pg.albums[pg.index].slides[a].title);
            
            anchor.append(img);
            li.append(anchor);
            li.appendTo('#galleryUL');
        }
    }
    
    $('#galleryUL').JGalleryViewer();
}

function loadTheGallery(xmlFile){
   $.ajax({
        url: xmlFile,
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        error: function(request, text, exception){
            alert('Error loading XML document: ' + text);
        },
        success: function(xml){
            $(xml).find('album').each(function(i){
                var album_title = $(this).attr('title');
                var album_description = $(this).attr('description');
                var album_image = $(this).attr('image');
                
                var new_album = new Album(album_title, album_description, album_image);

                $(this).find('slide').each(function(j){
                    var slide_title = $(this).attr('title');
                    var slide_description = $(this).attr('description');  
                    var slide_image = $(this).attr('image');  
                    
                    var new_slide = new Slide(slide_title, slide_description, slide_image);
                    new_album.slides.append(new_slide);
                });

                pg.albums.append(new_album);
            });
            
            loadGallery();
        }
    });
}
