//var FLICKR_URL = '/pages/flickr';
var FLICKR_URL = '/javascripts/data/flickr.json';
var IMG_ARRAY = new Array();
var CURRENT_IMG = 0;

var FlickrImage = function (url,thumbURL,title,date,imageURL) {
	this.url = url;
	this.thumbURL = thumbURL;
	this.imageURL = imageURL;
	this.title = title;
	this.date = date;
}

var setImgDesc = function (index) {
	var img = IMG_ARRAY[index];
	$("#imgDesc").html(img.title);
}

var loadFlippers = function() {
    $(".flipperLeft").bind('click',function(e) {
       if (CURRENT_IMG > 0)  {
           CURRENT_IMG--;
           $(".imgThumb img").attr('src',IMG_ARRAY[CURRENT_IMG].thumbURL);
           $(".imgThumb img").attr('alt',IMG_ARRAY[CURRENT_IMG].title);
           setImgDesc(CURRENT_IMG);
       }
    });
    $(".flipperRight").bind('click',function(e) {
       if (CURRENT_IMG < (IMG_ARRAY.length - 1))  {
           CURRENT_IMG++;
           $(".imgThumb img").attr('src',IMG_ARRAY[CURRENT_IMG].thumbURL);
           $(".imgThumb img").attr('alt',IMG_ARRAY[CURRENT_IMG].title);
           setImgDesc(CURRENT_IMG);
       }
    });
}

var initializeImgs = function(thumbSuffix,imageSuffix,hasLightbox,hasFlippers) {
	var maxThumbs = 28;
        if (thumbSuffix == undefined) {
            thumbSuffix = "_s";
        }
        if (imageSuffix == undefined) {
            imageSuffix = "";
        }
        if (hasLightbox == undefined) {
            hasLightbox = true;
        }
	
	var generateThumbs = function (lightbox,flippers) {
                var generateCount = IMG_ARRAY.length;
                if (flippers) {
                    generateCount = 1;
                }
                for (var i=0; i<generateCount; i++) {
			var img = IMG_ARRAY[i];
                        var imgHTML = '';
                        imgHTML += '<div class="imgThumb">';
                        if (lightbox) {
                            imgHTML += '<a href="' + img.imageURL + '" rel="lightbox" title="' + img.title + '">';
                        }
                        imgHTML += '<img src="' + img.thumbURL + '" alt="' + img.title + '" />';
                        if (lightbox) {
                            imgHTML += '</a>';
                        }
                        imgHTML += '</div>';
			$("#img").append(imgHTML);
                        //$("#img").append('<div class="imgThumb"><a href="' + img.imageURL + '" rel="lightbox" title="'+ img.title +'"><img src="' + img.thumbURL + '" alt="' + img.title + '" /></a></div>');
			if (i == 0) {
                            setImgDesc(i);
			}
		}
		if (lightbox) {
                    $('#img a').lightBox({fixedNavigation:true});
                }
                if (flippers) {
                    loadFlippers();
                }
		
	}
	
	responseText = $.ajax({
                                type: "POST",
                                url: FLICKR_URL,
                                datatype: "json",
                                async: false
                         }).responseText;
	
	//alert(responseText);
	responseText = responseText.replace("jsonFlickrFeed",""); //the jsonFlickrFeed function definition must be removed from the flickr json
	data = eval(eval('(' + responseText + ')')); //double evaluate to convert to object
	//alert(data);
	for (var i=0; i < data.items.length && i < maxThumbs; i++) {
		
		var url = data.items[i].link;
		var thumbURL = data.items[i].media.m.replace("_m.jpg", thumbSuffix + ".jpg");
		var imageURL = data.items[i].media.m.replace("_m.jpg", imageSuffix + ".jpg");
		var title = data.items[i].title;
		var date = new Date(data.items[i].date_taken);
		
		IMG_ARRAY[i] = new FlickrImage(url,thumbURL,title,date,imageURL);
		
	}
	
	generateThumbs(hasLightbox,hasFlippers);
	
}