jQuery(document).ready(function($) {
	
	if(!handheld) {

		var socialbox = $('div#socialbox'),
			promobox = $('a#promo'),
			bodybox = $('body'),
			windowbox= $(window);
	
		windowbox.scroll( function() {
			var scrollTopPos = windowbox.scrollTop();
			if (scrollTopPos > 280) {
				socialbox.css("top",(scrollTopPos+10)+"px");
				promobox.css("top",(scrollTopPos+10)+"px");
			} else {
				socialbox.css("top","280px");
				promobox.css("top","290px");
			}
		});
	
		resizeWindow();

		var resizing = false;
		windowbox.resize(function(){
			if(resizing !== false) clearTimeout(resizing);
			resizing = setTimeout(resizeWindow, 200);
		});
		function resizeWindow() {
			if(windowbox.width()>1165) {
				bodybox.attr("id","full");
			} else {
				bodybox.attr("id","w1000");
			}
		}
	}
	
// WIDGETS

	// Setup Photo & Video Share Buttons
	$(".embed-btn").each(function(){
		var id = this.id.replace("embed-btn-",""),
			type = this.rel,
			widget = $("#widget-"+id),
			embedBtn = $(this),
			codebox = $("#codebox-"+id);
		
		// widget mouse events
		widget.mouseenter( function(){ 
			widgetOv(id,type); 
		});
		widget.mouseleave( function(){
			widgetOut(id,type);
		});
		
		$("#embed-btn-"+id).click( function() {
			share(id,this.rel);
		});
		$("#close-btn-"+id).click( function() {
			shareClose(id);
		});
		if(type=='video') {
			$("#embed-"+id).click( function() {
				share(id,type);
			});
		}
		
		// mouse enter
		function widgetOv(id,type) {
			embedBtn.fadeIn("fast");
			if(type=='video') {
			} else {
				$("#gal-"+id+"-controls").fadeIn("fast");
				$("#gal-"+id+"-caption").fadeIn("fast");			
			}
		};
		// mouse leave		
		function widgetOut(id,type) {
			embedBtn.fadeOut("fast");
			codebox.fadeOut("fast");
			if(type=='video') {
			} else {
				$("#gal-"+id+"-controls").fadeOut("fast");				
				$("#gal-"+id+"-caption").fadeOut("fast");
			}
		};
		// share toggle
		function share(id,type){
			codebox.fadeIn("fast");
			embedBtn.fadeOut("fast");
			get_embed(id,type);
			return false;
		};
		// share - close click
		function shareClose(id) {
			codebox.fadeOut("fast");
			embedBtn.fadeIn("fast");
			return false;
		}
	});
	
// SCROLLERS

	$(".scrollme").each(function(){
		scrollme = $(this);
		scrollme.tinyscrollbar({size: (scrollme.height()-15)});
	});
	
// WIDGETS - VIDEO PLAYERS
	
	var vimeos = document.querySelectorAll('iframe.e-vimeo'), player;	
	for (var i = 0, length = vimeos.length; i < length; i++) {
		player = vimeos[i];
		$f(player).addEvent('ready', ready);
	}
	function addEvent(element, eventName, callback) {
		if (element.addEventListener) element.addEventListener(eventName, callback, false);
		else element.attachEvent(eventName, callback);
	}
	function ready(player_id) {
		var video = document.getElementById(player_id).parentNode;
			id = video.id.replace("widget-","");
		var froogaloop = $f(player_id);
		
		var playing = false,
			screenshot = $("#screenshot-"+id),
			playBtn = $("#play-"+id),
			embedBtn= $("#embed-btn-"+id),
			codebox = $("#codebox-"+id);
			
		function vimeoPlay() {
			playing = true;
			
			froogaloop.api('play');
			screenshot.fadeOut("fast");
			playBtn.fadeOut("fast");
			embedBtn.fadeOut("fast");
			codebox.fadeOut("fast");
			
			// events
			froogaloop.addEvent('finish', vimeoFinish);	
			froogaloop.removeEvent('ready');
		}
		function vimeoFinish() {
			playing = false;
			
			screenshot.fadeIn("fast");
			playBtn.fadeIn("fast");
			embedBtn.fadeIn("fast");
			codebox.fadeIn("fast");
			get_embed(id,'video');
			
			// events
			froogaloop.removeEvent('finish');
			froogaloop.addEvent('ready', ready);
		}

		addEvent(document.querySelector("#play-"+id), 'click', vimeoPlay, false);
		addEvent(document.querySelector("#screenshot-"+id), 'click', vimeoPlay, false);

		playBtn.fadeIn("fast");
		embedBtn.fadeIn("fast");
	}
});

// Get Embed Codes
function get_embed(id,type) {
	var size = document.getElementById("size-"+id);
	var embedcode = document.getElementById("embedcode-"+id);
	var p = document.getElementById("p-"+id).value;
	if(size.value == 'SIZE') {
		if(type=='video') size.value = '640';
		else size.value = '500';
		embedcode.select();
	} else { 
		var width = parseInt(size.value);
		if(width<360) {
			width = 360;
			size.value = '360';
		}
		if(type=='video') {
			var height = parseInt(width/640*360);
			with(embedcode) {
				value = '<iframe src="http://www.sundaybikes.com/videos/player.php?p='+p+'&w='+width+'&h='+height+'" width="'+width+'" height="'+height+'" frameborder="0"></iframe>';
				select();
			}
		} else {
			var height = parseInt(width/500*333);
			with(embedcode) {
				value = '<iframe src="http://www.sundaybikes.com/widgets/photo-gallery.php?p='+p+'&w='+width+'&h='+height+'" width="'+width+'" height="'+height+'" frameborder="0"></iframe>';
				select();
			}
		}
	}
}
