$(document).ready(function() {
	
	/** registration & login overlays **/
	$("a.login-overlay").colorbox({width:"450px", height: "250px", iframe:true, href:"index.php/profile/login?simple",scrolling:false})
	$("a.register-overlay").colorbox({width:"450px", height: "450px", iframe:true, href:"index.php/register?simple",scrolling:false})

	$("a.markup-info").colorbox({width:"400px", height: "400px", iframe:true})

	// $("a.markup-info").colorbox({width:"300px", iframe:true})
	
	/** dropdown **/
	$("li.topmenu-dropdown").mouseover(function() {
		$(this).find('ul').show();
	})
	$("li.topmenu-dropdown").mouseout(function() {
		$(this).find('ul').hide();
	})
	$("li.topmenu-dropdown").find('ul').hide();
	
	/** spotlight **/
	activateSpotLight('div.spotlight','.spotlightselect');
	
	/** confirmation of blog delete **/
	$('a.delete').click(function() {
		return confirm('Are you sure you want to delete this item?');
	})
	
	$('a.facebook-share').click(function() {
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent($(this).attr('href'))+'&t='+encodeURIComponent($(this).attr('title')),'sharer','toolbar=0,status=0,width=626,height=436');
		return false;
	})
	
	$('a.delicious-share').click(function() {
		window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent($(this).attr('href'))+'&title='+encodeURIComponent($(this).attr('title')),'delicious', 'toolbar=no,width=550,height=550'); 
		return false;
	})
	
	$('.coverage-media .choice li').click(function() {
		
		$('.coverage-media .choice li').removeClass('active')
		$(this).addClass('active')
		var ttype = $(this).attr('type')
		$('.coverage-media-galleries').hide()
		$('.coverage-media-videos').hide()
		$('.coverage-media-'+ttype).show()
		
	})
	$('.coverage-media-videos').hide()
	
	$('.wrap-simple form').attr('target','_parent')
	
	$('.coverage-media a').each(function() {
		
		$(this).append('<div class="tooltip">' + $(this).attr('title') + '</div>')
		$(this).mouseover(function() {
			$(this).find('.tooltip').show()
		})
		$(this).mouseout(function() {
			$(this).find('.tooltip').hide()
		})
	
	})
	
})

 
/** spotlight variables and functions **/
var spCurrent = -1, spTimeoutId = false, spPosition = 0, spSelWidth = 0, spWrapperWidth = 0, spSelMaxItems = 0, spSelPos = 0, spElement = '', spSelElement = '';

function activateSpotLight(elem,selelem) {
	
	spElement = elem, spSelElement = selelem;
	
	spSelWidth = $(spSelElement + ' ul li').eq(1).width() + parseInt($(spSelElement + ' ul li').eq(1).css('marginLeft')) + parseInt($(spSelElement + ' ul li').eq(1).css('marginRight'));
	spSelMaxItems = Math.floor( $(spSelElement).width() / $(spSelElement + ' ul li').eq(0).width() );
	spSelPos = spSelMaxItems % 2 + Math.floor(spSelMaxItems / 2);

	if($(spElement).size() > 1) {
		
		$(spElement).hide();
		switchSpotLight();
		
		$(spSelElement + ' li').click(function() {
			return setActiveSpotlight( $(spSelElement + ' ul li').index( $(this)  ) );
		})
		
	}
}

function switchSpotLight() {
	
	$(spElement).eq(spCurrent).fadeOut(500);
	spCurrent++;
	
	if(spCurrent > ($(spElement).size()-1)) {
		spCurrent = 0;
	}
	
	$(spSelElement + ' ul li').removeClass('active')
	$(spSelElement + ' ul li').eq(spCurrent).addClass('active');
	
	// determine position of selector list
	if( spCurrent < spSelPos ) {
		// move to 0
		$(spSelElement + ' ul').animate({
		    marginLeft: 0,
		  }, 1000);
	} else if (spCurrent > ( $(spElement).size() - Math.floor(spSelMaxItems / 2) ) - (spSelMaxItems % 2) ) {
		// nothing
	} else {
		// move to where-ever
		$(spSelElement + ' ul').animate({
		    marginLeft: -(spSelWidth * spCurrent) + ((spSelPos-(spSelMaxItems % 2)) * spSelWidth),
		  }, 1000);
	}
	
	$('div.spotlight').eq(spCurrent).fadeIn(500);
	spTimeoutId = setTimeout("switchSpotLight()", 5000); // activate self
	
}

function setActiveSpotlight(item) {
	clearTimeout(spTimeoutId);
	$('div.spotlight').eq(spCurrent).fadeOut(500);
	spCurrent = item - 1;
	switchSpotLight();
	return false;
}