/*! --- onload --- */
var youtube_link = '';

$(document).ready(function()
{
	set_external_links();
	$('.corner').corner('12px');
	$('.corner_top').corner('12px top');
	
	set_quick_nav();
	//$('#q').corner('5px'); - this throws up an error with IE - can't apply to inputs
	show_hide_text('#q');
	
	$('.photos img:nth-child(4n)').css('margin', '8px 0 0 0');
	$('a.lightbox').lightBox({doAfter: show_youtube });
	$('a.lightbox').click(function(){
		hide_youtube();
	});
});

function hide_youtube() {
	$('iframe').css('display','none');
}

function show_youtube() {
	$('iframe').css('display','block');
}

/*! --- functions --- */

function set_quick_nav() {
	var list = $('.quick_nav ul.top_level>li');
	list.corner('5px');
	list.find('ul>li:last').corner('5px bottom');
	
	list.find('ul').hide();
	
	if($.browser.msie) {
			list.uncorner().css('border','solid 1px #ababab'); // corners do not work in IE
	}
	
	// hide all the menu's
	$('body').click(function() {
		// take off any corner styles and add normal corners
		list.corner('5px');
		// hide the lists
		list.find('ul').hide();
		
		if($.browser.msie) {
				list.uncorner().css('border','solid 1px #ababab'); // corners do not work in IE
		}
	});
	
	// click on menu
	list.click(function(e){
		e.stopPropagation();                       // stop the 'body' click actions (above)
		
		// find out if this menu is already visible
		if($(this).find('ul').is(':visible')) {
			// menu is visible already
			list.find('ul').hide();                  // hide all the menu's
			list.corner('5px');                      // make sure the corners are re-set
			
			if($.browser.msie) {
				list.uncorner().css('border','solid 1px #ababab'); // corners do not work in IE
			}
			
		} else {
			// menu needs to be shown
			list.find('ul').hide();                  // hide all the menu's
			list.corner('5px');                      // make sure the corners are re-set
			$(this).uncorner().corner('5px top');    // add square corners to bottom (remove all corners - and only add to top)
			$(this).find('ul').show();               // show child menu
			
			if($.browser.msie) {
				list.uncorner().css('border','solid 1px #ababab'); // corners do not work in IE
				list.find('ul>li:last').corner('5px bottom').css({'border':'solid 1px #ababab','border-top':'none'});
			}
		}
	});
	
	
	/* IE fix 
	if($.browser.msie) {
		list.uncorner().css('border','solid 1px #ababab');
		list.hover(function(){
			$(this).uncorner().css('border','solid 1px #ababab');
		},function(){
			$(this).uncorner().css('border','solid 1px #ababab');
		});
		list.find('ul>li:last').corner('5px bottom').css({'border':'solid 1px #ababab','border-top':'none'});
	}*/
}

// open external links in a new window
function set_external_links() {
	var server = 	window.location.protocol + '//' +  window.location.host;
	
	// (get absolute links) (not on this server)
	$('a[href^=http]').not('a[href^="'+server+'"]').click(function(ev){
		ev.preventDefault();
		// open a new window
    window.open(this.href);
    return false;
	});
}

// hide text in input on click (useful for search boxes)
function show_hide_text(el) {
	$(el).each(function() {
		document['def_'+$(this).attr('name')] = $(this).attr('value');
		
		$(this).focus(function() {
			if($(this).attr('value') == document['def_'+$(this).attr('name')]) { $(this).attr('value',''); }
		});
		
		$(this).blur(function() {
			if($(this).attr('value') == '') { $(this).attr('value',document['def_'+$(this).attr('name')]); }
		});
		
	});
}

function homepage_tabs() {
	

	/* cycle all images */	
	$('.tabs').before('<div class="tabs_nav">').cycle({
		fx:				'fade',
		speed:		1000,
		timeout:	8000,
		pause:		1,
		after:  onAfterTab,
		pager:		'.tabs_nav'
	});
	
	// this needs to be added afterwards - so that it doesn't mess up IE
	$('.tabs').corner('15px tr bl br');
	
	if($.browser.msie) {
		$('.tabs').css('border','solid 1px #ababab');
	}
	
	// make an array of all tab titles (from slide heading)
	var tab_array = [];
	$('.tabs .slide').each(function(){
		tab_array.push($(this).find('.tab_title').html());
	});
	
	// rewrite the tabs from numbers to the headings	
	for(var i=0; i<tab_array.length; i++)
	{
		$('.tabs_nav a:nth-child('+(i+1)+')').html(tab_array[i]);
	}
	
	// wrap each a tag with an item div, and add a div for each corner
	$('.tabs_nav a').wrap('<div class="item">');
	// set the first slide be highlighted
	$('.tabs_nav .item a.activeSlide').parent().css({'background-image':'url(/images/tab_active.jpg)'});
	
	$('.tabs_nav .item').corner('10px top');
	$('.tabs_nav .item a.activeSlide').css({'color':'#ffffff'});
	
	$('.tabs .slide').each(function(){
		var bg_img = $(this).find('.background_image img').attr('src');
		if(bg_img != null) $(this).find('.background_image').css('background','transparent url('+bg_img+') no-repeat 0 0').find('img').css('display', 'none');
	});
	
	$('.tabs .slide .background_image').corner('15px right');
}

function onAfterTab() {
	$('.tabs_nav .item').css({'background-image':'url(/images/tab_normal.jpg)'});
	$('.tabs_nav .item a.activeSlide').parent().css({'background-image':'url(/images/tab_active.jpg)'});
	$('.tabs_nav .item a').css({'color':'#000000'});
	$('.tabs_nav .item a.activeSlide').css({'color':'#ffffff'});
}

function show_hide(el) {
	$(el).toggle();
}

function getScreen( url, size )
{
  if(url === null){ return ""; }

  size = (size === null) ? "big" : size;
  var vid;
  var results;

  results = url.match("[\\?&]v=([^&#]*)");

  vid = ( results === null ) ? url : results[1];

  if(size == "small"){
    return "http://img.youtube.com/vi/"+vid+"/2.jpg";
  }else {
    return "http://img.youtube.com/vi/"+vid+"/0.jpg";
  }
}
