var loc = window.location.href
var culinary = {
	loc: window.location.href,
	purl: (loc.indexOf('#') > 0) ? loc.substring(0,loc.indexOf('#')) : loc,
	printParams: 'toolbar=0,status=0,height=600,width=500,scrollbars=yes,resizable=yes',
	imgWBounds: 600,
	tempW: 0,
	tempH: 0,
	photoDims: {
		albums: {},
		tours: {
			height: 240,
			width: 480
		}
	},
	startZIndex: 100,
	currentImgId: 'img-1'
};
var purl = (loc.indexOf('#') > 0) ? loc.substring(0,loc.indexOf('#')) : loc;
var printParams = 'toolbar=0,status=0,height=600,width=500,scrollbars=yes,resizable=yes';
var imgWBounds = 600;
var tempW = tempH = 0;

if (!console) { var console = {}; console.log = function(msg) {}; }

sfHover = function() {
	var sfEls =  $('menu').getElements('li');
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover = function() {
			this.className += ' sfhover';
			//alert(this.className);
		}
		sfEls[i].onmouseout = function() {
			this.className = this.className.replace(new RegExp(' sfhover\\b', ''));
		}
	}
}

window.addEvent('domready',function() {

});

window.addEvent('load',function() {
	hh = [];
	$$('.landing_tout').each(function(a){
		var h = a.getCoordinates().height;
		hh.push(h)
	});
	hh.sort(function(a,b) {
		if (a > b) return 1
		if (a < b) return -1
		return 0;
	});
	newH = hh[(hh.length - 1)] + 'px';
	$$('.landing_tout').each(function(a){
		a.setStyles({ height: newH })
	});
	if ($('bot_social')) {
		$('bot_social').addEvent('click',function(e) {
			$('bot_social_links').toggleClass('social_links_off')
		})
		$$('.share').each(function(a){
			a.addEvent('click',function(e) {
				e = new Event(e).stop();
				if (a.getAttribute('href')) {
					var id = a.getAttribute('href').split('#').pop();
					shareLink(id);
				}
			})
		});
	}
	if ($('bot_bookmark')) {
		$$('#bot_bookmark a').addEvent('click', function(e) { bookmark(title, purl) });
	}
	
	if ($('bot_print')) {
		$$('#bot_print a').addEvent('click', function(e) {
			var url = purl + 'print/';
			console.log(url);
			console.log('printParams: ', printParams);
			pop(url,'print',printParams);
		});
	}	
	/*
	if($('swap_img')) {
		var per = w = h = 0;
		w = $('swap_img').getSize().x;
		h = $('swap_img').getSize().y;
		if (w > culinary.imgWBounds) {
			per = culinary.imgWBounds/w;
			var ratio = w/h;
			var newW = per * w;
			var newH = per * h;
			console.log('per: %a, w: %b, h: %c, ratio: %d', per, newW, newH, ratio)
			$('swap_img').setStyles({
				height: newH,
				width: newW
			});
			culinary.photoDims.albums = {
				height: newH,
				width: newW
			};
		}
		$('swap_img').setStyles({
//			visibility: 'visible'
		});

	}
*/	
	if ($$('.thumb')) {
		$$('.thumb').each(function(a) {
			if (a.getProperty('href') && !a.getProperty('href').indexOf('#')) { 
				a.addEvent('click', function(e) {
//					e = new Event(e).stop();
					e.stop();
					var imgId = a.getProperty('href').split('#').pop();
					var $img = $(imgId);
					var $cur = $(culinary.currentImgId);
					
					console.log(imgId)
					
					
					$cur.fade('out');
					$img.setProperty('opacity',0).removeClass('hide').fade('in');
					
					
					culinary.currentImgId = imgId;
					console.log(culinary.currentImgId)
/*
					var img = this.getChildren('img').getProperty('src').toString();
					var w = culinary.photoDims.albums.width || culinary.photoDims.tours.width;
					var h = culinary.photoDims.albums.height || culinary.photoDims.tours.height;
					url = img.replace('55/55',  w + '/' + h);
					var imgXHR = new XHR({
							method: 'get',
							headers: function(a) {
								console.log('headers: ', a)
							},
							onSuccess: function(data) {
								$('swap_img').empty();
								setTimeout(function() { $('swap_img').setProperty('src', url); }, 100);
							},
							onFailure: function() {
								console.log('fail')
							}
						}).send(url);
*/			

				});
				
			}
		});
	}
	
});

function pop(url,name,params){
	window.open(url,name,params);
}

function shareLink(id) {
	console.log('share it: ', id)
	console.log('url: ', purl);
	console.log('title: ', title);
	switch (id) {
		case 'delicious':
			pop('http://del.icio.us/post?url=' + culinary.purl + '&title=' + title + '&bodytext=' + desc, 'del.icio.us', 'toolbar=0,status=0,height=400,width=700,scrollbars=yes,resizable=no');
			break;
		case 'reddit':
			pop('http://reddit.com/submit?url=' + culinary.purl + '&title=' + title, 'reddit', 'toolbar=0,status=0,height=436,width=646,scrollbars=yes,resizable=yes');
			break;
		case 'digg':
			pop('http://digg.com/submit?phase=2&url=' + culinary.purl, 'digg', 'toolbar=0,status=0,height=605,width=1024,scrollbars=yes,resizable=yes');
			break;
		case 'facebook':
			pop('http://www.facebook.com/sharer.php?u=' + culinary.purl + '&t=' + title, 'facebook', 'toolbar=0,status=0,height=436,width=646,scrollbars=yes,resizable=yes');
			break;
	}
}

function bookmark(title, url) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

