$(window).load( function() {

	// Frontpage
	$('#hero').nivoSlider({
		controlNav: false,
		directionNav: false,
		animSpeed: 523,
		pauseTime: 5023,
		effect: 'sliceDownLeft',
		slices: 16
	});
	
	// Top Menu
	if($('#cheader').length > 0){
	
		// IE Bugfix
		$('#ch_bot ul li:last').hide();
	
		// Hover effect
		$('#ch_top ul li').hover(function(e) {
				$(this).hoverFlow(e.type, { height: 53, marginTop: 1 }, 223, 'swing').find('.ch_bkg').stop().animate({ opacity: 0 }, 'fast');
			}, function(e) {
				$(this).hoverFlow(e.type, { height: 34, marginTop: 20 }, 263, 'easeOutBack').find('.ch_bkg').stop().animate({ opacity: 1 }, 'fast');
		});
		// Click extend
		$('#ch_top ul li').click(function(e){
			$(this).hoverFlow(e.type, { height: 34, marginTop: 20 }, 263, 'easeOutBack').find('.ch_bkg').stop().animate({ opacity: 1 }, 'fast');
			var base = $(this);
			var link = base.find('a').attr('href');
			location.href = link;
		});
		
	}
	


	// CMS areas
	var cke_bodycopy = {
		toolbar:
		[
			['Bold', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'ShowBlocks', '-', 'Find','Replace'],
			['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
			['UIColor']
		],
		resize_enabled: false,
		width: 510
	};
	
	// Start/Stop boxes
	$('#frm_start, #frm_stop').datepicker({
		maxDate: '+3Y',
		defaultDate: "+1d",
		
		showOn: 'both',
		buttonImage: 'images/calendar.gif',
		buttonImageOnly: true,
		buttonText: ''
	});
	
	$('textarea.editor').ckeditor(cke_bodycopy);

	// Datetime picker
	$('#frm_date').datetimepicker({
		ampm: true,
		stepMinute: 5
	});
	$('#datepickerBtn').click(function(){
		$('#frm_date').focus();
		return false;
	});
	
	// Application Search
	$('#btnSearch').click(function(){
		var start = $('#frm_start').val();
		var stop = $('#frm_stop').val();
		var type = $('#selectTypes').val();
		var keyword = $('#frm_keyword').val();
		
		$('#wrapper #appMain .scroller').css('background-image', 'url(' + '/images/loading.gif' + ')');
		$('#mainView').fadeOut(650, function(){
			$.get('ajaxy.php', { action: 'main', start: start, stop: stop, type: type, keyword: keyword },
				function(data){
					$('#mainView').html(data);
					$('#mainView').fadeIn(650,function(){
						$('#wrapper #appMain .scroller').css('background-image', 'none');
					});
			});
		});
		return false;
	});
	
	// Show Ilist
	if($('#appSidebar').length > 0) $('#appSidebar .eitem:hidden').fadeIn(900);
	
	// Application Add
	$('.btnAdd').live('click', function() {
		var base = $(this);
		var id = $(this).attr('rel');

		$('#appSidebar .eitem:visible').fadeOut(200);
		
		// Send selection to sidebar		
		$.get('iajax.php', { action: 'add', id: id},
			function(data){
				
				// Refresh sidebar
				$.get('ajaxy.php', { action: 'sidebar' },
					function(data){
						//$('#appSidebar').html(data);
						//$('#appSidebar .eitem:hidden').fadeIn(900);
						
						document.getElementById('appSidebar').innerHTML = data;
						$('#appSidebar .eitem:hidden').fadeIn(900);
					});
				
			});
		
		return false;
	});
	
	// Application Remove
	$('.btnRemove').live('click', function() {
		var base = $(this);
		var id = $(this).attr('rel');

		$('#appSidebar .eitem:visible').fadeOut(200);
		
		// Send selection to sidebar		
		$.get('iajax.php', { action: 'remove', id: id},
			function(data){
				
				// Refresh sidebar
				$.get('ajaxy.php', { action: 'sidebar' },
					function(data){
						//alert(data);
						//$('#appSidebar').empty().append(data);
						//var newHTML = data;
						//$('#appSidebar').empty();
						//$('#appSidebar').html(newHTML);
						document.getElementById('appSidebar').innerHTML = data;
						$('#appSidebar .eitem:hidden').fadeIn(900);
					});
				
			});
		
		return false;
	});
	
	// Applicaton Extended Info
	$('.btnInfo').live('click', function() {
		var base = $(this);
		var id = $(this).attr('rel');
		
		$.colorbox({href: "/itinerary/moreInfo.php?id="+id, width:"823px", height:"653px", iframe:true});
		
		return false;
	});
	
	// Application Email
	$('#appEmail').click(function(){
		popitup();
		return false;
	});
	function popitup()
	{
		popit = window.open ('email.php','Email',"location=1,status=1,scrollbars=1,width=500,height=270");
		//popit.moveTo(0,0);
	} 

	
});
