
//var attachAutoComplete;
$(document).ready(function() {

	/**
	* Nav Bar Fix 
	**/
	var navHeight = $('#nav ul').height() + 111 + 50 + 64; // UL + logo + footer bar + margins
	if( navHeight > $(window).height() ) {
		var newNavHeight = Math.max( navHeight, $("#main").height() ) + 50;
		$('#nav').css({ 
			height: newNavHeight+'px',
			position: 'absolute'
		});
	}

	if( $("#content").height() < $("#main").height() ) {
		$("#content").height( $("#main").height() );
	}
	
	$('a.extend').click(function(){
		$("#footer").height(250);
		$('#content').height("height", $('#content').height()+250);
		$('#nav').height("height", $('#nav').height()+250);
		$(this).css("visibility", "hidden");
		$('a.collapse').css("visibility", "visible");
	});
	
	$('a.collapse').click(function(){
		$("#footer").height(50);
		$('#content').height("height", $('#content').height() - 250);
		$('#nav').height("height", $('#nav').height() - 250);
		
		$(this).css("visibility", "hidden");
		$('a.extend').css("visibility", "visible");
	});

	/**
	* Footer Extended
	**
	$(window).scroll(function() {
		var diff = 0;
		var max = $('#main').height()+300;
		var current = $(window).height() + $(window).scrollTop();

		//
		// start the expansion when within 200 pixels from bottom
		if( current > max - 250 ) {
			if( current < max ) {
				diff = 250 - (max - current);
				$("#footer").height(diff+50);
			}
		} else {
			$("#footer").height(50);
		}

	}); */
	
	//
	// Add to calendar popup
	$('a.btn_addtocalendar').click(function(e) {
		document.getElementById("shareItems").innerHTML = '<li><a href="/events/google.dT/' + $(this).data('id') + '" class="google" target="_blank">Google Calendar</a></li><li><a href="/events/addtocalendar.dT/' + $(this).data('id') + '" class="outlook">Outlook</a></li><li><a href="/events/addtocalendar.dT/' + $(this).data('id') + '" class="ical">iCal</a></li>';
			
		overlay($(this).attr('class'));
	});
	
	//Show the subscribe popup
	$('a.btn_subscribe').click(function(){
		overlay($(this).attr('class'));
	});
	
	//close popups	
	$('.close').click(function(){
		overlay('close');
	});

	/**
	* Search Box
	**/
	$("#search input[type='text']").css({ 
		width: '185px' 
	}).focus( function() {
		// widen, show button
		$(this).animate({
			width: '260px'
		}, 500);
	}).blur( function() {
		// widen, show button
		$(this).animate({
			width: '185px'
		}, 500);
	})
	$('input[name=="keyword"]').click(function(){
		$('input[name="keyword"]').val() = '';
		$('input[name="keyword"]').title() = '';
	});

	$("input[type='text']").focus( function() {
		// clear
		if($(this).val() == $(this).attr('title')) { 
			$(this).css({ color: '#ccc' });
		}
	}).blur( function() {
		// un-clear
		if($(this).val() == '') { 
			$(this).val($(this).attr('title'));
			$(this).css({ color: '#8c8d90' });
		}
	}).keydown( function(e) {
		if( $(this).attr('title') && $(this).attr('title').length > 0 && $(this).val() == $(this).attr('title') ) {
			$(this).css({ color: '#000' });
			$(this).val('');
		}
	}).keyup( function(e) {
		if( $(this).val().length==0 && e.keyCode!=8 /* not backspace */ && $(this).attr('title').length>0 ) {
			$(this).css({ color: '#8c8d90' });
			$(this).val($(this).attr('title'));
		}
	});


	/**
	* Media Center : News : Expand/Collapse
	**/
	$("a.btn_readmore").click( function() {
		var $article = $(this).closest('li');
		$article.children('p.more').slideUp('slow');
		$article.children('div.moreBody').slideDown('slow');
		$article.children('p.actions').slideDown('slow');
		
		return false;
	});
	$("a.btn_collapse").click( function() {
		var $article = $(this).closest('li');
		$article.children('p.more').slideDown('slow');
		$article.children('div.moreBody').slideUp('slow');
		$article.children('p.actions').slideUp('slow');
			
		return false;
	}); 

	/**
	* Content Feeds : Expand/Collapse
	**/	
	$("a#feedMoreBtn").click( function() {
		$('.feedMoreBody').slideDown('slow');
		$("a#feedLessBtn").css('visibility', 'visible');
		$("a#feedMoreBtn").css('visibility', 'hidden');
		$('div#content').css('height', ($('div#content').height() + 500).toString() + 'px');
		$('div#nav').css('height', ($('div#nav').height() + 500).toString() + 'px');
		
		return false;
	});
	$("a#feedLessBtn").click( function() {
		$('.feedMoreBody').slideUp('slow');
		$("a#feedLessBtn").css('visibility', 'hidden');
		$("a#feedMoreBtn").css('visibility', 'visible');
		$('div#content').css('height', ($('div#content').height() - 500).toString() + 'px');
		$('div#nav').css('height', ($('div#nav').height() - 500).toString() + 'px'); 
		
		return false;
	});
	
	/**
	* Related resources : Expand/Collapse
	**/	
	$("a#resourceMoreBtn").click( function() {
		$('.resourceMoreBody').slideDown('slow');
		$("a#resourceLessBtn").css('visibility', 'visible');
		$("a#resourceMoreBtn").css('visibility', 'hidden');
		$('div#content').css('height', ($('div#content').height() + 500).toString() + 'px');
		$('div#nav').css('height', ($('div#nav').height() + 500).toString() + 'px');
	
		return false;
	});
	$("a#resourceLessBtn").click( function() {
		$('.resourceMoreBody').slideUp('slow');
		$("a#resourceLessBtn").css('visibility', 'hidden');
		$("a#resourceMoreBtn").css('visibility', 'visible');
		$('div#content').css('height', ($('div#content').height() - 500).toString() + 'px'); 
		$('div#nav').css('height', ($('div#nav').height() - 500).toString() + 'px'); 
		
		return false;
	});

	/**
	* Media Center : News Date Select
	**/
	$("#news_month").change(function() {
		this.form.submit();
	});

	/**
	* Media Center : Calendar date selections
	**/
	$("input#startDate").change(function() {
		this.form.submit();
	});
	
	$("input#endDate").change(function() {
		this.form.submit();
	});
	
	/**
	* Events Calendar : News Date Select
	**/
	$("#events_month").change(function() {
		this.form.submit();
	});

	/** attach date picker **/
	$('input.dateselect').datepicker({dateFormat: 'yy-mm-dd'});
	
	/** show/hide date/time fields for resource submission **/
	$("#expire").click(function(){
		if($("#expire").attr("checked") == "checked"){
			$("#resourceDateTime").css("visibility", "visible");
			$("#submitResource").css("bottom", "0px");			
		}
		else {
			$("#resourceDateTime").css("visibility", "hidden");	
			$("#submitResource").css("bottom", "150px");
		}
	});
	
	/**
	* Subscribe: Content feeds, events
	**/
	$("#subscribeForm").submit(function(){
		var data = '';
		var url = '';
		var message = '';
		var username = $("#username").val();
		var email = $("#email").val();
		var formType = $("#formType").val();
		
		
		if(username.length == 0){
			$("#attachment-error").css("visibility", "visible");
			document.getElementById("attachment-error").innerHTML = "Please enter your name.";
			return;
		}
		if(email.length == 0){
			$("#attachment-error").css("visibility", "visible");
			document.getElementById("attachment-error").innerHTML = "Please enter your email.";
			return;
		}
		
		if(formType == 'events'){
			var type = $("#type").val();
			if(type.length == 0){
				$("#attachment-error").css("visibility", "visible");
				document.getElementById("attachment-error").innerHTML = "Please choose an event category.";
				return;
			}
			
			if($("#eventId").length){
				data = 'data=' + username + '^' + email  + '^' + $("#eventId").val() + '^' + type;
				url = '/subscribe/event/';
				message = 'You have been subscribed to the ' + type + ' event category.';
			}
		}	
		else if($("#pageTopics").length){
			data = 'data=' + username + '^' + email + '^' + $("#pageTopics").val();
			url = '/subscribe/topics/';
			message = 'You have been subscribed to these contents.';
		}
	
		//make new call
		ajaxCall(data, url ,'', message);		
		overlay("close");
	
		return false;
	});

});


/**
* handle the series events
**/
function handleSeriesEvents(seriesId){
	var seriesObj = document.getElementById("series");
	
	var seriesVal = seriesObj.getAttributeNode("value");
	seriesVal.value = seriesId;
	
	document.getElementById("calForm").submit();
	return false;
}

/**
* Reloads the image in the flickr photo viewer
**/
function loadBigImage(id){

	var smallImage = document.getElementById(id);
	var obj = document.getElementById("bigImage");
	var src = smallImage.src;
	
	obj.src = smallImage.src.slice(0, src.length - 6) + '.jpg';
	
	return false;
}

/**
* overlay : Toggles the visibility of the overlay div
**/
function overlay(elementclass) {
	
	if(elementclass == "btn btn_addtocalendar"){
		$("#addtocalendar").css("visibility", "visible");
		$("#addToEventHeader").css("visibility", "visible");
	}	
	else if(elementclass == "btn btn_subscribe"){		
		document.getElementById("attachment-error").innerHTML = "";
		$("#attachment-error").css("visibility", "hidden");
		$("#subscribe").css("visibility", "visible");
		$("#dialog").css("visibility", "visible");
	}	
	else if(elementclass == 'close'){
		document.getElementById("attachment-error").innerHTML = "";
		$("#attachment-error").css("visibility", "hidden");
		$(".overlay").css("visibility", "hidden");
		$("#dialog").css("visibility", "hidden");
		$("#addToEventHeader").css("visibility", "hidden");
	}	
	
}



