///////////////////////////////////footer slider////////////////////////////////////////

function outLinks() {
	yourURL = yourURL.substring(7, yourURL.length - 1);
	var outLink;
	if (document.getElementsByTagName('a')) {
		for ( var i = 0; (outLink = document.getElementsByTagName('a')[i]); i++) {
			if (outLink.href.indexOf(yourURL) == -1) {
				outLink.setAttribute('target', '_blank');
			}
		}
	}
}

function mycarousel_initCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

// /////////////////////////////////tabs & header////////////////////////////////////////

$(document).ready(function() {
	if (jQuery('#mycarousel').jcarousel) {
		jQuery('#mycarousel').jcarousel( {
			auto : 2,
			wrap : 'circular',
			initCallback : mycarousel_initCallback
		});
	}
	outLinks();

	// When page loads...
	// Hide all content
	$(".tableholder").hide();
	// Activate first tab
	$("ul.tabs li:first").addClass("active").show();
	// Show first tab content
	$(".tableholder:first").show();

	// When loaded into iframe, update height of iframe
	frameHeight();

	// On Click Event
	$("ul.tabs li").click(function() {
		// Remove any "active" class
		$("ul.tabs li").removeClass("active");
		// Add "active" class to selected tab
		$(this).addClass("active");
		$(".tableholder").hide(); // Hide all tab content

		// Find the href attribute value to identify the active
		// tab + content
		var activeTab = $(this).find("a").attr("href");
		// Fade in the active ID content
		$(activeTab).fadeIn();
		frameHeight();
		return false;
	});

	// When page loads...
	// Hide all content
	$(".imgholder").hide();

	// Activate first tab
	$("ul.menu li:first").addClass("active").show();

	// Show first tab content
	$(".imgholder:first").show();

	// On Click Event
	$("ul.menu li").click(function() {
		$(".slider_text").show("fast");
		// Remove any "active" class
		$("ul.menu li").removeClass("active");
		// Add "active" class to selected tab
		$(this).addClass("active");
		// Hide all tab content
		$(".imgholder").hide();

		// Find the href attribute value to identify the active
		// tab + content
		var activeTab = $(this).find("a").attr("href");
		$(activeTab).fadeIn(); // Fade in the active ID content

		return false;

	});

	// /////////////////////////////////end of tabs////////////////////////////////////////

	$(".image_thumb ul li:first").addClass('active');
	// Add the active class (highlights the very first list item by
	// default)
	$(".image_thumb ul li").click(function() {
		// Set Variables

		// Get Main Image URL
		var imgTitle = $(this).find('a').attr("href");

		if ($(this).is(".active")) {
			// If the list item is active/selected, then...
			// Don't click through - Prevents repetitive
			// animations on active/selected list-item
			return false;
		
		} else {
			// If not active then... Animate the Description
			// Switch the main image (URL + alt tag)
			$(".main_image img").attr( {
				src : imgTitle,
				alt : imgAlt
			});

		}
		
		// Show active list-item
		// Remove class of 'active' on all list-items
		$(".image_thumb ul li").removeClass('active');
	
		// Add class of 'active' on the selected list
		$(this).addClass('active');
		return false;

	}).hover(function() {
		// Hover effects on list-item
		// Add class "hover" on hover
		$(this).addClass('hover');
	}, function() {
		// Remove class "hover" on hover out
		$(this).removeClass('hover');
	});

	$("a.collapse").click(function() {
		// Toggle the description (slide up and down)
		$(".main_banner .block").slideToggle();
		// Toggle the class name of "show" (the hide/show tab)
		$("a.collapse").toggleClass("show");
	});

	// /////////////////////////////////menu////////////////////////////////////////


	if ($.browser.msie && parseInt($.browser.version) == 7) {
		// IE7 mode
	} else {

		$('.group').masonry( {
			singleMode : true,
			saveOptions : false,
			resizeable : false,
			itemSelector : '.li-1'
		});

	}

	$("#header ul.nav > li").hover(function() {

	// On hover over, add class "subhover"
		$(this).addClass("subhover");
		$("ul.nav li.subhover > ul").fadeIn("fast");
	},

	function() { // On Hover Out
		$("#header ul.nav li.subhover > ul").hide();
		// On hover out, remove class "subhover"
		$(this).removeClass("subhover");

	});

	// /////////////////////////////////media player////////////////////////////////////////

	// Loads in First Video
	jQuery("#YouVid li:first-child").each(setYoutube);

	// Loads Video
	jQuery("#YouVid li").click(setYoutube);

	// Generates List
	jQuery("#YouVid li.youtubevideo").each(
		function() {
			
			var VidID = $(this).attr("id");
			var VidTitle = $(this).text();
			
			$(this).html(
					" <img src='/phpThumb/phpThumb.php?src=http://img.youtube.com/vi/"
							+ VidID
							+ "/1.jpg&w=60&h=60&zc=1' alt='"
							+ VidTitle + "' title='" + VidTitle
							+ "'/><small>" + VidTitle
							+ "</small>");
		});

	$('.moremedia').click(function() {
		$('#YouVid').scrollTo('+=158px', 300);
		// Show first tab content
			$('.lessmedia').show();
		});
	$('.lessmedia').click(function() {
		$('#YouVid').scrollTo('-=158px', 300);
	});

	// /////////////////////////////////header show hide
	// menu///////////////////////////////
	$(".slider_text .close_button").click(function() {
		$(this).parents(".slider_text").hide("fast");
	});

	// /////////////////////////////img_zoom///////////////////////////////

	$("a.img_zoom[href$=.jpg],a[href$=.png],a[href$=.gif]").fancybox( {
		'overlayShow' : false,
		'type' : 'image',
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic'
	});

	// /////////////////////////////maps///////////////////////////////

	$(".legenda_toggle").click(function() {
		$(".maps_legenda ul").slideToggle("fast");
	});

});

/**
 * Called when a thumb is clicked
 * @return
 */
function setYoutube() {
	// Remove currently active
	$("#YouVid li").removeClass("YouTubeSelect");
	// Set this to active
	$(this).addClass("YouTubeSelect");
	
	// Get the youtube id
	var VidID2 = $(this).attr("id");
	
	// And the title...
	var VidTitle = $(this).children('small').first().text() + "";
	
	// And the description...
	var Viddescription = $(this).attr("title") + "";
	$(".description").html(Viddescription);
	$("#YouVidTitle").html(VidTitle);
	
	if($(this).hasClass('image')) {
		// Dealing with an image
		var imgsrc = $(this).children('img').first().attr('src');
		imgsrc = imgsrc.replace('&w=60&h=60&zc=1', '&w=360&h=234&zc=1');
		$("#YouTubeBox").html('<img src="' + imgsrc + '" alt="' + VidTitle + '" width="360" height="234" />');
		$("#YouTubeContainer > .caption").html($(this).children('.caption').first().html());
	} else {
		// Load the player
		$("#YouTubeBox").html("<iframe class='youtube-player' type='text/html' width='360' height='234' src='http://www.youtube.com/embed/"
						+ VidID2
						+ "' 'frameborder='0'> </iframe>");
	}
}

/**
 * Updates the iframe height to the height of the content
 */
function frameHeight() {
	if (window.parent != window.self) {
		// Loaded in iframe
		var iframes = window.parent.document.getElementsByTagName('iframe');
		for ( var i in iframes) {
			if (iframes[i].contentWindow
					&& iframes[i].contentWindow.document == document) {
				$(iframes[i]).height($('#main').height());
			}
		}
	}
}
function openShareFrame() {
	$('#shareFrame').toggle();
}

/**
* Explicitly size the banner, since IE & Firefox don't know how to handle height:auto
* @param banner Object The banner object
* @param safety int Counter to prevent endless loop
* @return
*/
function sizeBanner(banner, safety) {
	if(safety == undefined)
		safety = 0;
	
	try {
		banner.ref.height = banner.ref.TGetProperty("/", 9);
	}catch(ex) {
		// Sorry, get a new pc!
		if(safety > 255)
			return;
		
		var delay = function() {sizeBanner(banner, safety+1)};
		setTimeout(delay, 100);
	}
}
