
//==========================================
//=== Re-render any eye candy on the page, such as a slideshows
//==========================================
function reload_eye_candy() {
	
	//======================================
	//=== LIGHTBOX
	//======================================
	
	//any images that do not have the special class=image on them are to be rendered using a lightbox
	$('div.custom_page_content img[class!="no_lightbox"]').each(function (index, item) {
		
		//if this image is inside a list element, do not add the lightbox!!! This is how other eye candy makes use of images...
		if ($(item).parents('ul,ol').length > 0) {
			return;
		}
		
		//make sure an a tag is not already wrapped around it
		if ($(item).parents('a').length > 0) {
			//add no_lightbox class to the image
			$(item).attr('class', 'no_lightbox');
			return;
		}
		
		//wrap an a tag around the image
		$(item).wrap("<a href='"+($(item).attr('original')||item.src)+"' title='"+(item.title||item.alt)+"'></a>");
		
	});
	
	//find all a tags that contain img tags, and convert them to a fancy box
	$('div.custom_page_content a:has(img[class!="no_lightbox"])').fancybox({
		overlayOpacity: 0.7, 
		overlayColor: '#000000'
	});
	
	//find all a tags that contain img tags, and convert them to a fancy box
	$('a[rel="lightbox"]').fancybox({
		overlayOpacity: 0.7, 
		overlayColor: '#000000'
	});
	
	
	//======================================
	//=== SLIDESHOWS
	//======================================
	
	//find an load the slideshows
	$(".eye_candy_slideshow").image_preloader({onFinish: function ($container) {
		
		//now generate the slideshow
		$container.cycle({ 
			fx:           		($container.attr('transition_effect') || 'fade'), 			// name of transition effect (or comma separated names, ex: fade,scrollUp,shuffle) 
			timeout:       		($container.attr('slide_speed') || 4000),  				// milliseconds between slide transitions (0 to disable auto advance) 
			speed:         		($container.attr('transition_speed') || 1000),  			// speed of the transition (any valid fx speed value) 
			next:          		($container.attr('disable_click_next') ? null : $container),	// css selector of item to use to fire the next slide, blank it out using: null
			sync:          		($container.attr('sync_transitions') ? true : false),     		// true if in/out transitions should occur simultaneously 
			random:        	($container.attr('random_slides') ? true : false),     		// true for random, false for sequence (not applicable to shuffle fx) 
			pause:         		($container.attr('pause_on_hover') ? true : false),     		// true to enable "pause on hover" 
			fastOnEvent:   	(($container.attr('transition_speed')/2) || 0)    			// force fast transitions when triggered manually (via pager or prev/next); value == time in ms 
		});
		
	}});
	
}

//==========================================
//=== Render any eye candy on the page, such as a slideshows
//==========================================
$(document).ready(function () {
	
	//load up the eye candy
	reload_eye_candy();
	
	//add lightbox to specific images
	$('a[rel="lightbox"]').fancybox({
		overlayOpacity: 0.7, 
		overlayColor: '#000000',
		showNavArrows: false
	});
	
});

//==========================================
//=== RENDER THE HEADER ITEMS
//==========================================
$(document).ready(function(){ 
	
	//MAIN PAGES - MAKE SORTABLE
	$('ul.template_navigation_tabs[logged_in]').sortable({
		axis: "x",
		tolerance: 'pointer',
		distance: 5,
		update: function(e, ui) {
			$.post(
				'/ajax/navigation_main_save_order.php', 
				$('ul.template_navigation_tabs').sortable("serialize", {key:"main_pages_sortable[]"})
			);
		}
	});
	
	//MAIN PAGES - MAKE MOVEABLE
	$('ul.template_navigation_absolute[logged_in] li').draggable({
		snap: true,
		distance: 10,
		snapTolerance: 5,
		containment:'div.template_header'
	});
	
	//MAKE MOVEABLE
	$('#site_template_header_logo, #site_template_header_decoration, #site_template_header_title_1, #site_template_header_title_2, #site_template_header_title_3, ').draggable({
		snap: false,
		distance: 5,
		containment:'div.template_header'
	});
	
	//REGISTER THE HEADER AS A DROP ZONE (FOR: TABS, LOGO, TITLE)
	$('div.template_header').droppable({
		drop: function(event, ui) { 
			switch (ui.draggable.attr('id')) {
				case 'site_template_header_logo': 
						$.get('/ajax/template_header_save_logo_position.php?left=' + ui.draggable.position()['left'] + '&top=' + ui.draggable.position()['top']);
						return;
						break;
				case 'site_template_header_decoration': 
						$.get('/ajax/template_header_save_decoration_position.php?left=' + ui.draggable.position()['left'] + '&top=' + ui.draggable.position()['top']);
						return;
						break;
				case 'site_template_header_title_1': 
						$.get('/ajax/template_header_save_title_position.php?title_id=1&left=' + ui.draggable.position()['left'] + '&top=' + ui.draggable.position()['top']);
						return;
						break;
				case 'site_template_header_title_2': 
						$.get('/ajax/template_header_save_title_position.php?title_id=2&left=' + ui.draggable.position()['left'] + '&top=' + ui.draggable.position()['top']);
						return;
						break;
				case 'site_template_header_title_3': 
						$.get('/ajax/template_header_save_title_position.php?title_id=3&left=' + ui.draggable.position()['left'] + '&top=' + ui.draggable.position()['top']);
						return;
						break;
			}
			$.get('/ajax/navigation_main_save_position.php?page_id=' + ui.draggable.attr('id').replace('main_pages_sortable_', '') + '&left=' + ui.draggable.position()['left'] + '&top=' + ui.draggable.position()['top']);
			return;
			
		}
	});
	
	//RELATED PAGES
	$('#site_navigation_related_pages[parent_id]').sortable({
		tolerance: 'pointer',
		distance: 5,
		items: 'li[class!="unsortable"]',
		update: function(e, ui) {
			$.post(
				'/ajax/navigation_related_save_order.php?parent_id=' + $(ui.item).parent().attr('parent_id'), 
				$('#site_navigation_related_pages').sortable("serialize", {key:"related_pages_sortable[]"})
			);
		}
	});
	
}); 

//==========================================
//=== Make every other row in a table another colour
//==========================================
$(document).ready(function () {
	$('.table tr:nth-child(odd)').css({backgroundColor:"#EEEEEE"});
});
