/*
 * Flickrush 1.0 - jQuery flickr plugin
 *
 * Copyright (c) 2010 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.flickrush.js 2010-02-17 $ 
 *
 */
function load_overlay(o_id){
    //alert("dasdasd");
    //$("#"+overlay_id).click(function(){
        $("#"+o_id).overlay({load:true});
    //});

}
function do_pretty_photo(img_path, title, description){

    $.fn.prettyPhoto();
    $.prettyPhoto.open(img_path, title, description);
}

function ppInit(){
            $("a[rel^='prettyPhoto']").prettyPhoto({
                        animation_speed: 'fast', /* fast/slow/normal */
			slideshow: 5000, /* false OR interval time in ms */
			autoplay_slideshow: false, /* true/false */
			opacity: 0.80, /* Value between 0 and 1 */
			show_title: true, /* true/false */
			allow_resize: true, /* Resize the photos bigger than viewport. true/false */
			default_width: 640,//500,
			default_height: 480,//344,
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			horizontal_padding: 20, /* The padding on each side of the picture */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode: 'opaque', /* Set the flash wmode attribute */
			autoplay: true, /* Automatically start videos: True/False */
			modal: false, /* If set to true, only the close button will close the window */
			deeplinking: true, /* Allow prettyPhoto to update the url to enable deeplinking. */
			overlay_gallery: true, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
			keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
			callback: function(){}, /* Called when prettyPhoto is closed */
			ie6_fallback: true});
        }




(function($){
	$.fn.flickrush=function(options){ 
		var defaults={
			limit:3,
			random:true,
			id:'44499772@N06'
		};
		var options=$.extend(defaults,options);
		return_me = this.each(function(options){
			var act=$(this);
                        //api_call_url =" http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=c5a7b7af0d78460374ab2e91edd5e368&user_id=68614001%40N08&extras=description%2C+date_upload%2C+date_taken%2C+owner_name%2C+last_update%2C+tags%2C+o_dims%2C+views%2C+media%2C+path_alias%2C+url_sq%2C+url_t%2C+url_s%2C+url_m%2C+url_z%2C+url_l%2C+url_o&per_page=16&page=0&format=json";
                        api_call_url ="http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=f7586ee55d4f8c55c46a5cf6d542433c&extras=description%2C+date_upload%2C+date_taken%2C+owner_name%2C+last_update%2C+tags%2C+o_dims%2C+views%2C+media%2C+path_alias%2C+url_sq%2C+url_t%2C+url_s%2C+url_m%2C+url_z%2C+url_l%2C+url_o&per_page="+defaults.limit+"&page="+defaults.page+"&format=json&nojsoncallback=0";
			//$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?format=json&id="+defaults.id+"&perpage=2&page=1&jsoncallback=?",
                        
                        jsonFlickrApi = function(data_flickr){
                                //console.log(data_flickr);
                                $(defaults.info_element).text(" Page "+data_flickr.photos.page+" of "+data_flickr.photos.pages+". ("+data_flickr.photos.total+")");
				var num = 0; //get a random integer
				var imageArray = new Array(); //store used images in here

                                        $(act).html("");
			   		$.each(data_flickr.photos.photo, function(i,item){
                                            /*if (i<defaults.page*defaults.limit) i = defaults.page*defaults.limit;
                                            if (i>data_flickr.items.length) i=data_flickr.items.length - defaults.limit;
                                            if (i<0) i=0;*/
                                            //console.log(item);
			   			if(defaults.random == true) {
							if( ( i == randomiser ) && (!imageArray.in_array(randomiser) ) ) {
								var newImage = $("<img/>").attr({
									src: item.url_sq,
									alt: item.tags,
									width: item.height_sq, //defaults.width
                                                                        height: item.width_sq,
                                                                        id: item.pathalias+"_"+item.id
								});
								$(act).append(newImage);
                                                                $("#"+item.title).wrap("<a href='" + item.url_o/*item.link*/ + "' rel='prettyPhoto' class='prettyPhoto'></a>");
			   				}
			   			} else {
							if( ( i <= defaults.limit-1 ) && (!imageArray.in_array(defaults.limit-1) ) ) {
                                                                var overlay_id = "overlay_"+i; //unique name for overlay container to be generated
								var newImage = $("<img/>").attr({
                                                                        "class":"overlay_me",
									src: item.url_sq,//item.media.m,
									alt: item.title,//item.tags,
                                                                        width: item.height_sq, //defaults.width
                                                                        height: item.width_sq,
                                                                        id: overlay_id,//item.title,
                                                                        rel: "#"+overlay_id

								});
								$(act).append(newImage);
                                                                //$("#"+item.title).wrap("<a href='javascript:void(0);' onclick='do_pretty_photo(\""+item.media.m.replace("_m","")+"\",\""+item.title+"\",\"Description\" );' rel='prettyPhoto' class='prettyPhoto'></a>");
                                                                $("#"+overlay_id/*item.title*/).wrap("<a href='" + item.url_l/*item.link*/ + "' rel='prettyPhoto[flickr]' class='prettyPhoto'></a>");


                                                                //***************************************************************************************
                                                                /*var newOverlay = $("<div/>").attr({
									"class": "simple_overlay",
                                                                        id: overlay_id

								});
								$("#overlays").append(newOverlay);
                                                                overlay_contents = "<img src='"+item.media.m.replace("_m","")+"' />";
                                                                newOverlay.html(overlay_contents) //fill_overlay
                                                                */
                                                                //***************************************************************************************

			   				}
			   			}
			       	});



			       	if(defaults.random == true) {
						if ( imageArray.in_array(randomiser) ) {
							defaults.limit++;
						}
						//strore our image number
						imageArray.push(randomiser);
					} else {
						imageArray.push(defaults.limit-1);
					}

                                $(act).append('<div style="clear:both;"></div>');
                                ppInit(); //pretty photo initiated here
                                if (typeof defaults.calback_func == "function") defaults.calback_func(); else alert("Callabck is not a function.");

		  	}

                        $.get(api_call_url,{user_id:defaults.id},jsonFlickrApi,"jsonp");
		 	
			//function to check in an array
		  	Array.prototype.in_array = function(p_val) {
				for(var i = 0, l = this.length; i < l; i++) {
					if(this[i] == p_val) {
						return true;
					}
				}
				return false;
			}			
		});                
                return return_me;
                
	}
        
})(jQuery);
