// Copyright (c) 2007 Kynan Stewart Hughes (http://www.kynan.net.au)
//alert("slideshow");

var global_tags = 'seeingsystem';

if(global_tags)
{
	tags = global_tags;
}
else
{
	tags = 'seeingsystem';
}

var slideshow={
    // Page section IDs
    slide_div_a:'slide-div-a',
    slide_div_img:'slide-div-img',
	slide_div:'slide-div',
    slide_div_height:704,
    slide_div_width:704,
    interval:5000, // The timeout for the slideshow 
    timerID:'', // set this so that is is accessible to all functions
    global_image_number:1, //the number to start at - this gets incrememnted. Only really used with the start/stop
    global_stop_start_flag:'started',
    //total_results_available:null, //how many there are - initially set to null
    //SlideRegion:'HOME',//decide the value in slide array in /TNSW/Destination/RegionHeaderSlide.asp
    //SlideHeaderAttribute:"",

	photofeed:false,

    params:{
    	'api_key':'1cd28ef9270b9dad6d02734d6a8d88a9',
    	'method':'flickr.photos.search',
    				//user_id (Optional) - The NSID of the user who's photo to search. If this parameter isn't passed then everybody's public photos will be searched. A value of "me" will search against the calling user's photos for authenticated calls.
    	'user_id':'77411642@N00',
    				//tags (Optional) - A comma-delimited list of tags. Photos with one or more of the tags listed will be returned.
    	'tags':tags,
    				//tag_mode (Optional) - Either 'any' for an OR combination of tags, or 'all' for an AND combination. Defaults to 'any' if not specified.
    	'tag_mode':'all',
    				//sort (Optional) - The order in which to sort returned photos. Deafults to date-posted-desc. The possible values are: date-posted-asc, date-posted-desc, date-taken-asc, date-taken-desc, interestingness-desc, interestingness-asc, and relevance.
    	'sort':'date-posted-desc',
    				//extras (Optional) - A comma-delimited list of extra information to fetch for each returned record. Currently supported fields are: license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags.
    	'extras':'original_format',
    				//content_type (Optional) - Content Type setting: 1 for photos only. 2 for screenshots only. 3 for 'other' only. 4 for all types.
    	'content_type':'1',
    				//per_page (Optional) - Number of photos to return per page. If this argument is omitted, it defaults to 100. The maximum allowed value is 500.
    	'per_page':'1',
    				//page (Optional) - The page of results to return. If this argument is omitted, it defaults to 1.
    	// THIS IS SET EACH CALL AND INCREMENTED: 'page':,
    	'format':'json',
    },

	//http://api.flickr.com/services/rest/?api_key=1cd28ef9270b9dad6d02734d6a8d88a9&method=flickr.photos.search&user_id=77411642%40N00&tags=seeingsystem&tag_mode=all&sort=date-posted-desc&extras=original_format&content_type=1&per_page=1&page=1&format=php_serialArray
    
    addEvent:function(elm, evType, fn, useCapture)
    {
        if (elm.addEventListener) 
        {
    	    elm.addEventListener(evType, fn, useCapture);
    		return true;
        }
    	else if(elm.attachEvent)
    	{ 
            var r = elm.attachEvent('on' + evType, fn);
            return r;
        }
    	else
    	{
            elm['on' + evType] = fn;
        }

		return true;
    },

    init:function()
    {
        slide_node = document.getElementById(slideshow.slide_div_img);
	if(slide_node)
	{
	    //alert("calling getNextPhoto");
            //slideshow.getNextPhoto(slideshow.global_image_number,slideshow.total_results_available);
            slideshow.getNextPhoto(slideshow.global_image_number);
	}

        slide_node_a = document.getElementById(slideshow.slide_div_a);

	if(slide_node_a)
	{
            slideshow.addEvent(slide_node_a, 'click', slideshow.stopStart, false);
	    //alert("\"slide-div-a\" is present on the page: " + slide_node_a.href);
	}
	else
	{
	    alert("\"slide-div-a\" is not present on the page.");
	}
    },

    stopStart:function()
    {
        if(slideshow.global_stop_start_flag=='started')
        {
            slideshow.global_stop_start_flag = 'stopped';
		    //alert(slideshow.global_stop_start_flag);
            window.clearTimeout(slideshow.timerID);
        }
        else
        {
            slideshow.global_stop_start_flag = 'started';
		    //alert(slideshow.global_stop_start_flag);
            ++slideshow.global_image_number;
            slideshow.getNextPhoto(slideshow.global_image_number);
        }
    },
    
    formatParams:function(params)
    {
			var encoded_params_string = "";

            for (var key in params)
			{
            	encoded_params_string += key + '=' + params[key] + "&";
            }

			return encoded_params_string;
    },

	makeNewImage:function(feed,width,height)
    {
		var results = feed.photos;
		var response_array = new Array;

		response_array['page'] = results.page;
		response_array['pages'] = results.pages;
		response_array['total'] = results.total;
		//alert(response_array['total']);
        for (var key in results.photo[0])
		{
		    response_array[key] = results.photo[0][key];
        }
    
        this_image = new Image;
        //var src = "/assets/"+response_array[1];
        var src = "http://farm" + response_array['farm'] + ".static.flickr.com/" + response_array['server'] + "/" + response_array['id'] + "_" + response_array['secret'] + ".jpg"
        ////var src = "/assets/" + response_array['id'] + "_" + response_array['secret'] + ".jpg"

        this_image_number = response_array['page'];

        this_image.src = src;
        total_results_available = response_array['total'];
        url = "./?h=Ttlla&static=true&page=" + this_image_number;
		// we only want the image to swap on the page after the image we've created has loaded into memory
		//alert(url);
        this_image.onload = slideshow.swapImageNodeSrc(this_image,this_image_number,width,height,total_results_available,src,url);
	},
	// that came from http://icant.co.uk/articles/24ways/getflickr.js

    getNextPhoto:function(this_image_number,total_results_available)
    {
		//alert("slideshow.getNextPhoto");
		/*
        var total_results_available = (total_results_available == null) ? slideshow.total_results_available : total_results_available;
        window.clearTimeout(slideshow.timerID);
        if(slideshow.total_results_available != total_results_available)
        {
            this_image_number = 1;
            slideshow.total_results_available = total_results_available;
        }
		*/
        if(total_results_available == null)
        {
            this_image_number = 1;
        }
		var param_string = slideshow.formatParams(slideshow.params);
        //var url = 'http://seeing-system.localhost/slideshow.php?this_image_number=' + this_image_number;
        var url = 'http://api.flickr.com/services/rest/?' + param_string + "page=" + this_image_number;
        ////var url = '/photofeed_' + this_image_number + '.html';

        // Write the script into the document - this will make a funciton call to slideshow:jsonFlickrFeed()
        new Effect.Fade(slideshow.slide_div_img,{ duration: 2, fps: 12, afterFinish: function() { 
		    var s = document.createElement('script');
		    s.src = url;
		    document.getElementsByTagName('head')[0].appendChild(s);
          }
        })
    },
    
    swapImageNodeSrc:function(this_image,this_image_number,width,height,total_results_available,src,url)
    {
		//alert("swapImageSrc - this_image_number: "+this_image_number);
        var image_node = document.getElementById(slideshow.slide_div_img);
        var link_node = document.getElementById(slideshow.slide_div_a);
    
        if(image_node)
        {
            image_node.onload = new Function("slideshow.fadePhotoIn("+this_image_number+","+total_results_available+",'"+src+"');");
            image_node.src = this_image.src;
            image_node.width = width;
            image_node.height = height;

			// Get a figures for the margins
			//vertical_margin = (slideshow.slide_div_height - height)/2;
            //vertical_margin =  vertical_margin +"px";
			//horizontal_margin = (slideshow.slide_div_width - width)/2;
			//horizontal_margin = horizontal_margin + "px";
			vertical_border = (slideshow.slide_div_height - height)/2;
            vertical_border =  vertical_border +"px solid black";
			horizontal_border = (slideshow.slide_div_width - width)/2;
			horizontal_border = horizontal_border + "px solid black";

            image_node.style.borderTop =  vertical_border;
            image_node.style.borderBottom = vertical_border;
            image_node.style.borderLeft = horizontal_border;
            image_node.style.borderRight = horizontal_border;
            //image_node.style.marginTop =  vertical_margin;
            //image_node.style.marginBottom = vertical_margin;
            //image_node.style.marginLeft = horizontal_margin;
            //image_node.style.marginRight = horizontal_margin;
        }
        else
        {
           // //alert("The element \"img-slide-div\" doesn't exist.");
        }

        // NOTE: This doesn't appear to work. Strangely, it sets the href 
		// ut the link takes you back to the start
		if(link_node)
		{
            link_node.href = url;
		    //alert(link_node);
		}
    },
    
    fadePhotoIn:function(this_image_number,total_results_available,src)
    {
		//alert("fadePhotoIn - this_image_number: "+this_image_number);
        new Effect.Appear(slideshow.slide_div_img, {duration: 2,fps: 12,queue:'end',afterFinish: function() { document.getElementById(slideshow.slide_div).style.backgroundImage = "url("+src+")"; }});

		if(this_image_number == total_results_available)
		{
        		var next_image_number = 1;
		}
		else
		{
        		var next_image_number = this_image_number + 1;
		}

		//alert("fadePhotoIn - next_image_number: " + next_image_number + ' of ' + total_results_available);
    
        slideshow.timerID=setTimeout("slideshow.getNextPhoto("+next_image_number+","+total_results_available+")",slideshow.interval);
    }
}

