jQuery(document).ready(function() {

	var ie7 = (jQuery.browser.msie && jQuery.browser.version.substring(0, 2) == "7.") ? true : false
	var ie8 = (jQuery.browser.msie && jQuery.browser.version.substring(0, 2) == "8.") ? true : false

	// CSS3 fixes for old browsers
	if(ie7 || ie8){
		jQuery("ul.features li:nth-child(3n)").each(function(){ jQuery(this).css("margin-right","0"); })
		jQuery("ul.polaroids li.polaroid").css({"float":"left","border":"1px solid #cccccc","margin-right":"14px"});
		jQuery("ul.polaroids li.polaroid:nth-child(6n)").css("margin-right","-2px");
		jQuery(".clearfix").after("<div style=\"clear:both;\"> </div>");
		jQuery('body').append("<style type=\"text/css\">#main { border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; }");
	}

	// Feature carousel
	if(jQuery("body.front ul.features").length > 0){
		var feat = jQuery("body.front ul.features");
		feat.css({"position":"relative"});
		var li = feat.find("li");
		var h = li.outerHeight();
		var wide = li.outerWidth()+16;
		li.css({"margin-right":"16px"});
		var o = feat.offset();
		var p = feat.position();
		var w = feat.outerWidth();
		var offset = 0;
		var isAnimating = false;
		var timer;
		jQuery("body.front .view-id-features_list").css({"height":h+"px"});
		if(ie7) jQuery("body.front .view-id-features_list").css({"position":"relative"});
		jQuery('body').append("<style type=\"text/css\"> .featurenav { position:absolute;width:32px;height:113px;z-index:10;cursor:pointer; } .featurenav:hover { background-color: rgba(0,0,0,0.5); }</style>");
		var l = "<div style=\"left:0px;top:"+(p.top-16)+"px;\" class=\"featurenav featurenav-left\"><img src=\"/sites/default/modules/lco_slideshow/images/leftbar_s.png\" alt=\"Previous features\" /></div>";
		var r = "<div style=\"left:"+(w-16)+"px;top:"+(p.top-16)+"px;\" class=\"featurenav featurenav-right\"><img src=\"/sites/default/modules/lco_slideshow/images/rightbar_s.png\" alt=\"More features\" /></div>";
		if(ie7) jQuery("body.front .rowlast").prepend(l).append(r);
		else feat.before(l).after(r);
		
		
		feat.css({"width":(li.length*wide)+"px"});
		jQuery(".featurenav-left").click(function(){ scrollFeature(1) }).mouseover(function(){ clearInterval(timer); shiftFeature(1) } ).mouseout(function(){ shiftFeature(-1) } );
		jQuery(".featurenav-right").click(function(){ scrollFeature(-1) }).mouseover(function(){ clearInterval(timer); shiftFeature(-1) } ).mouseout(function(){ shiftFeature(1) } );
		function shiftFeature(dx){
			var s = (dx > 0) ? "+=" : "-=";
			feat.animate({"left":s+16},200);
			offset = dx*16;
		}
		function scrollFeature(d,delay){
			if(isAnimating) return;
			if(!d) d = -1;
			if(!delay) delay = 1000;
			isAnimating = true;
			var s = (d > 0) ? "+=" : "-=";
			var o = feat.position();
			dx = wide*3;
			if(d > 0){
				if(o.left+dx > 0){
					feat.css({"left":"-"+(dx-offset)+"px"}).append(li.slice(0,-3).detach());
					li = feat.find("li");
				}
			}else{
				if(o.left-dx+li.length*wide <= dx){
					feat.append(li.slice(0,3).detach()).css({"left":(o.left+dx-16)+"px"});
					li = feat.find("li");
				}
			}
			feat.animate({"left":s+(dx)},delay,function(){ isAnimating = false; })
		}
		timer = setInterval(function(){ scrollFeature(-1,2000) },10000);
	}

	// Load an observation block
	if(jQuery("#observations").length > 0){
		ncol = 5; //jQuery("#observations")
		nrow = 4;
		jQuery.ajax({
			url: "http://lcogt.net/observations/recent.json",
			dataType: "jsonp",
			success: function(data){
				obs = data.observation
				if(obs.length == 10)
					for(var i = 0; i < 10 ; i++) obs.push(obs[i])
				o = "";
				for(var i = 0 ; i < ncol*nrow ; i++){
					mr = (i % ncol == ncol-1) ? 0 : 1;
					mb = (i < (ncol*(nrow-1))) ? 1 : 0;
					o += "<a href=\""+obs[i].about+"\"><img src=\""+obs[i].image.thumb+"\" alt=\""+obs[i].image.label+"\" title=\""+data.observation[i].image.label+"\" style=\"margin-right:"+mr+"px;margin-bottom:"+mb+"px;float:left;width:60px;height:60px;\" \/><\/a>";
				}
				jQuery("#observations").html(o)
				if(jQuery("#observations img").length > 0) imageLoadError("#observations img");
			}
		});
	}

	// Side image links - a fix because Drupal doesn't do it properly
	jQuery(".field-type-media .small_thumbnail_sq").each(function(){
		if(jQuery(this).find("a").length==0){
			loc = jQuery(this).find("img").attr("src").replace(/styles\/small_thumbnail_sq\/public\//,'');
			jQuery(this).replaceWith("<a href=\""+loc+"\">"+jQuery(this).html()+"</a>");
		}
	})
	
	function imageLoadError(el,sz){
		jQuery(el).each(function(){
				var missing = (jQuery(this).outerWidth() > 200) ? "/sites/default/themes/lcogt/images/missing_large.png" : "/sites/default/themes/lcogt/images/missing.png";
				// Work	around for error function reporting of file load failure
				this.src = this.src;
				jQuery(this).bind("error",function() {
					this.src = missing;
					this.alt = "Image unavailable";
					this.onerror = "";
					return true;
				})
		});
	}

});


