var VCI_CORE = {};

$(function(){
		jQuery.VCI_CORE.navigation();
		jQuery.VCI_CORE.sitemap();
		jQuery.VCI_CORE.link_target('a.xLink', '_blank');
		jQuery.VCI_CORE.page_admin_form();
		jQuery.VCI_CORE.preview_page();
		jQuery.VCI_CORE.border_swap();
		jQuery.VCI_CORE.request_form_swap();
		jQuery.VCI_CORE.popup_link('#nbccedpEligibilityCriteriaLink');
});

jQuery.VCI_CORE = {

	popup_link : function(ID, h, w) {
		var btn = $(ID);
		
		if (btn.length > 0) {
			var href = $(btn).attr('href');
			var rel = $(btn).attr('rel');
			var title = $(btn).attr('title');
			
			var m_height =(h != null) ? h : $(window).height() * .7;
			
			$(btn).click(
				function () {
					$.post(href, {},
						function(data){
							
							$('body').append('<div id="modal"></div>');
							$('#modal').css({ opacity: '0.5'});
							
							$('body').append('<div id="modalContainer"></div>');
							$('#modalContainer').css({ display: 'none'});
							$('#modalContainer').append('<div id="modalBox"></div>');
							
							$('#modalBox').css({ height:m_height+"px"});
							
							$('#modalBox').append('<div id="modalBoxTop"><a href="#" id="closeModal">Close</a></div>');
							$('#modalBox').append('<div id="modalBoxContentContainer"></div>');
							$('#modalBoxContentContainer').append('<div id="modalBoxContent"></div>');
							
							var holder = $('#modalBoxContent');
							
							if (title.length > 0){
								$('#modalBoxTop').prepend('<h2>'+title+'</h2>');	
							}
							
							var content = $(data).find('#contentWrapper');
							
							$(holder).html(content);
							$('#modalContainer').show(300);
						
							var close_btn = $('#modalBoxTop #closeModal');
							$(close_btn).click(
								function(){
									$('#modalBox').hide(300);
									$('#modal').fadeOut(300,function(){
										$('#modal').remove();
										$('#modalContainer').remove();
									});
									return false;
								}
							);
						}
			
					);	
					
					return false;
				}
			);
			
		}
	},

	navigation : function() {
		$('#navigation').find('li').children('ul').css({display: 'none'});
		$('#navigation').find('li').hover(
			function(){
				//$(this).children('a').not('.select').css({background: 'transparent url(/images/nav/nav_bkgd.jpg) repeat-x 0 -26px;', color: '#000'});
				$(this).children('ul').css({display: 'block'});
			},
			function() {
				//$(this).children('a').not('.select').css({background: 'transparent', color: '#FFF'});
				$(this).children('ul').css({display: 'none'});
			}
		);
		
		var sub_nav = $('#navigation').find('li>ul');
		
		$(sub_nav).each(
			function(){
				var child_pages = $(this).children();
				if (child_pages.length === 0) {
					$(this).parent().remove();	
				}
			}
		);
		
	},
	
	sitemap : function () {
		var sitemap = $('#sitemap');
		
		if (sitemap.length > 0) {
			var sub_nav = $(sitemap).find('li>ul');
		
			$(sub_nav).each(
				function(){
					var child_pages = $(this).children();
					if (child_pages.length === 0) {
						$(this).parent().remove();	
					}
				}
			);
		}		
	},
	
	multi_tabs : function(linkList, items) {
		
		$(items).css({display: 'none'});
		$(linkList).find('a').click(
			function() { 
				var href = $(this).attr('href');
				
				$(linkList).find("a").not(this).removeClass("select");
				$(this).addClass("select");
				
				$("div" + items +":visible").not(href).css({display: 'none'});
				$(href).css({display: 'block'});
				
				return false;
			}
		);
		
		if (location.hash.length > 0){
			var tempLink = $(location.hash+'_btn');
			
			if (tempLink.length > 0) {
				$(tempLink).click();	
			} else {
				$(linkList).find('a:first').click();
			}
		} else {
			$(linkList).find('a:first').click();
		}
	},
	
	
	Compactor : function ( list, btnLink, targetItem ) {
		
		$(list).find(targetItem).css({display: "none"});
		$(list).find(btnLink).addClass("link").hover( function () {
			$(this).addClass(" linkOver");
		}, function () {
			$(this).removeClass("linkOver");
		}).toggle( function () {
			$(this).siblings(targetItem+":visible").slideUp("fast");
			$(this).siblings(btnLink).removeClass("linkOn");
			$(this).addClass(" linkOn").next(targetItem+":hidden").slideDown("fast");
			
		}, function () {
			$(this).removeClass("linkOn").next(targetItem+":visible").slideUp("fast");
		});
		
		$(list).find(btnLink+':first').click();
	},
	
	link_target : function (ID, target) {
		$(document).find(ID).each(
			function (){
				$(this).click(
					function (){
						var xLink = $(this).attr('href');
						window.open(xLink, target);
						return false;
					}
				);
			}
		);		
	},
	
			
	delete_alert : function()
	{
		$('.delete').click(function(){
			return confirm('Are you sure you want to delete this? It can not be undone.'); // OLD SCHOOL :)
		});
	},
	
	page_admin_form : function()
	{
		var form = $('#adminForm');
		
		if (form.length > 0) {
			var submission = $(form).attr('action');
			var btn = $(form).find('a.preview');
			
			if (btn.length > 0) {
				var page = $(btn).attr('rel');
				$(btn).attr({'href': page});
				
				$(btn).click(
					function (){
						$(form).attr({'action':page});
						
						var href = $(form).find('#href').attr('value');
												
						if (href && href.length > 0) {
							$(form).attr({'target':'_blank'});
						} else {
							$(form).attr({'target':'_self'});
						}
						
						$(form).submit();
						return false;
					}
				);
				
				$(form).find('input.submit').click(
					function (){
						$(form).attr({'action':submission, 'target':'_self'});
					}
				);			
			}
		}		
	},
		
	preview_page : function() 
	{
		if ($('#container').hasClass('previewPage')){
			$('a').not('.confirmPreview').click(
				function () {
					return confirm ('Are you sure you wish to navigate away from this page.  It is a preview of a page submission. If changes are not confirmed they will not be saved.');
				}				
			);
		}
	},
	
	border_swap : function()
	{
		var content = $('#content');
		var side_bar = $('#sideBar');
		
		if (content.length > 0 && side_bar.length > 0) {
			
			var content_h = $(content).height();
			var side_bar_h = $(side_bar).height();
			var max_height = side_bar_h;
			
			if (side_bar_h < content_h ) {
				max_height = content_h;
				$(content).css({'border-right' : '8px solid #dbd9d9'});
				$(side_bar).css({'border-left' : '0'});
			} else {
				$(content).css({'height' : max_height+'px'});
			}
			
			var column_1 = $('#column_1');
			var column_1_h = $(column_1).height();
			
			if (column_1_h < max_height) {
				$(column_1).css({'height' : max_height+'px'});
			}
			
		}
				
	},
	
	request_form_swap : function()
	{
		var form = $('#submissionForm');
		if (form.length > 0) {
			var request_type = $('input[name=request_type]:checked').val() 
			
			var booth = $('#submissionForm .booth');
			var speaker = $('#submissionForm .speaker');
		
			var booth_btn = $('#request_type_booth');
			var speaker_btn = $('#request_type_speaker');
			
			$(booth_btn).click(
				function () {
					$(booth).show(300);
					$(speaker).hide(300);
				}
			);
			
			$(speaker_btn).click(
				function () {
					$(speaker).show(300);
					$(booth).hide(300);
				}
			);
			
			if (request_type && request_type === 'Booth') {
				booth_btn.click();	
			}
			
			if (request_type && request_type === 'Speaker') {
				speaker_btn.click();	
			}
			
		}
	}
	
};


