Ext.namespace("dlr.cmp.flickr");
dlr.cmp.flickr.flickrPhotoWindow = function(cfg){
	// ------------------------------------------------
	// private properties
	// ------------------------------------------------
	var that = this;
	var isLoaded = false;
	var zoomId = "zoom-" + Math.random().toString().replace(".","");
	var multiplier = 0.5;
	var windowCfg = {
		layout: "border",
		width: 200,
		height: 200,
		items:[{ // ct
			xtype: "panel",
			region: "center",
			layout: "anchor",
			frame: false,
			border: false,
			anchor: "100% 0",
			items:[{ // photo panel
					xtype: "panel",
					frame: false,
					border: false,
					autoScroll: true,
					anchor: "100%",
					title: "&nbsp;"
				}]
		},{
			xtype: "panel",
			region: "west",
			layout: "anchor",
			collapsed: false,
			collapsible: true,
			split: true,
			frame: false,
			border: false,
			hidden: true,
			items:[{ // data panel
					xtype: "propertygrid",
					anchor: "100% 0",
					title: "Details",
					frame: false,
					border: false,
					listeners: {
						"beforeedit": function(evt){
							//evt.cancel = true;
						}
					} // end listeners
			}],
			listeners: {
				"hide" : function(comp){
					//alert("I hide!");
				}
			} // end listeners
		}],
		buttons:[{
			text: "Minimize Details",
			tooltip: "Minimize Details",
			hidden: true,
			handler: (function(){
				var width = Ext.get(zoomId).getWidth() + 10;
				var cmps = getCmps();
				cmps.detailsCt.hide();
				that.setWidth(width);
				that.center();
				cmps.minBtn.hide();
				cmps.maxBtn.show();
				that.doLayout();
			})
		},{
			text: "Maximize Details",
			tooltip: "Maximize Details",
			handler: (function(){
				var width = Ext.get(zoomId).getWidth() + 10;
				var cmps = getCmps();
				cmps.detailsCt.show();
				that.setWidth((width * multiplier) + (width + 50));
				that.center();
				cmps.minBtn.show();
				cmps.maxBtn.hide();
				that.doLayout();
			})
		},{
			text: "Close",
			handler: (function(){
				that.hide();
			})
		}]
	};
	var ldMsk = null;
	// ------------------------------------------------
	// private functions
	// ------------------------------------------------
	function getCmps(){
		return {
			winCt: that.items.get(0),
			photoPanel: that.items.get(0).items.get(0),
			detailsCt: that.items.get(1),
			detailsPanel: that.items.get(1).items.get(0),
			minBtn: that.buttons[0],
			maxBtn: that.buttons[1]
		};
	}; // end getCmps
	function setPhotoCfg(size){
		var photoSize;
		for (var sizeIdx = 3; sizeIdx >= 0; sizeIdx--){
			if (size[sizeIdx]){
				photoSize = size[sizeIdx];
				break;
			}
		}
		return {
			id: zoomId, tag: "img", src: photoSize.source, width: photoSize.width, height: photoSize.height, cls: "zoomPhoto"
		};
	}; // end setPhoto
	function getInfo(data){
		var info = {
			"Original format": data.originalformat,
			"Owners name": data.owner.realname,
			"Owners location": data.owner.location,
			"Title": data.title._content,
			"Description": data.description._content,
			"Date taken": data.dates.taken,
			"Tags": "",
			"License": getLicense(data.license).name,
			"License Info": getLicense(data.license).url

		};
		var tags = [];
		Ext.each(data.tags.tag, (function(itm, idx, len){
			tags.push(itm.raw);
		}));
		info["Tags"] = tags.join(",");
		return info;
	}; // end getInfo
	function getLicense(id){
		var license = {};
		switch(parseInt(id, 10)){
			case 0 :
				license.name = "All Rights Reserved.";
				license.url = "http://en.wikipedia.org/wiki/All_rights_reserved";
				break;
			case 1 :
				license.name = "Attribution-NonCommercial-ShareAlike License";
				license.url = "http://creativecommons.org/licenses/by-nc-sa/2.0/";
				break;
			case 2 :
				license.name = "Attribution-NonCommercial License";
				license.url = "http://creativecommons.org/licenses/by-nc/2.0/";
				break;
			case 3 :
				license.name = "Attribution-NonCommercial-NoDerivs License";
				license.url = "http://creativecommons.org/licenses/by-nc-nd/2.0/";
				break;
			case 4 :
				license.name = "Attribution License";
				license.url = "http://creativecommons.org/licenses/by/2.0/";
				break;
			case 5 :
				license.name = "Attribution-ShareAlike License";
				license.url = "http://creativecommons.org/licenses/by-sa/2.0/";
				break;
			case 6 :
				license.name = "Attribution-NoDerivs License";
				license.url = "http://creativecommons.org/licenses/by-nd/2.0/";
				break;
			default :
				license.name = "No known copyright restrictions";
				license.url = "http://flickr.com/commons/usage/";
				break;
		}
		return license;
	}; // end getLicense
	function getExif(data){
		var exif = {};
		Ext.each(data, (function(itm, idx, len){
			exif[itm.label] = itm.raw._content;
		}));
		return exif;
	}; // end getExif
	// ------------------------------------------------
	// public properties
	// ------------------------------------------------
	// ------------------------------------------------
	// public methods
	// ------------------------------------------------
	this.loadPhoto = function(record){
		var params = {method: "getPhoto", data: Ext.encode(record.data)};
		that.show();
		if (isLoaded) { return; }
		ldMsk = ldMsk || new Ext.LoadMask(getCmps().winCt.body, {msg: "Getting Photo..."});
		ldMsk.show();
		Ext.Ajax.request({
			url: 'flickrHelper.cfc',
			params: params,
			success: (function(response, options){
				var response = Ext.decode(response.responseText);
				var sizes = response.sizes.sizes.size; // array
				var info = getInfo(response.info.photo); // object
				var exif = response.exif.photo ? getExif(response.exif.photo.exif) : []; // array
				var photoCfg = setPhotoCfg(sizes);
				var width = parseInt(photoCfg.width, 10);
				var height = parseInt(photoCfg.height, 10) + 90;
				var photoPanel = getCmps().photoPanel;
				var detailsCt = getCmps().detailsCt;
				var detailsPanel = getCmps().detailsPanel;
				Ext.DomHelper.append(photoPanel.body, photoCfg);
				that.setTitle(info["Title"]);
				that.setSize({width: width, height: height});
				photoPanel.setSize({width: width, height: height});
				detailsCt.setWidth((width * multiplier) + 50);
				detailsPanel.setSource(Ext.apply(info, exif));
				photoPanel.setTitle("<a href='" +info["License Info"]+ "' target='_blank'>" +info["License"]+ "<a/>");
				that.doLayout();
				that.center();
				ldMsk.hide();
				isLoaded = true;
			}),
			failure: (function(){
				alert("fail");
				ldMsk.hide();
			})
		});
	}; // end loadPhoto
	// ------------------------------------------------
	// constructor code
	// ------------------------------------------------
	Ext.apply(this, cfg);
	// apply private cfgs
	Ext.apply(this, windowCfg);
	dlr.cmp.flickr.flickrPhotoWindow.superclass.constructor.call(this, cfg);
	// events
};
Ext.extend(dlr.cmp.flickr.flickrPhotoWindow, Ext.Window);
Ext.reg("flickrPhotoWindow", dlr.cmp.flickr.flickrPhotoWindow);