// JavaScript Document
// Global v1.0 - 03/02/2009
// ----------------------------
$(document).ready(function(){
	//$(document).pngFix();	// PNG fix
	$('body').addClass('domenabled');	// Add domenabled class
	// Check users screen resolution for optimal layout
	if (screen.width>=1024){
		$('body').addClass('highscreenres');
	}
	$('#navigation ul').superfish();	// Superfish for navigation
	// Add sIFR headers
	/*
	if($('#content')){
		var cooper = { 
			src: './static/swf/cooperstd.swf',
			wmode: 'transparent'			
		};
		sIFR.activate(cooper);
		sIFR.replace(cooper, {
		   selector: 'h1',
		   css: { 
			 		'.sIFR-root': { 'color': '#BC0C0C' }
			 }
		 });
	}
	*/
	
	/* Load default functions */
	sortableHome(); // Make divs sortable
	stripeTables(); // Stripe all tables
	roundedCorners(); // Round corners on homepage
	datePicker(); // Insert datepicker
	quickJump(); // Add quickjump
	toggleAnim(); // Toggles
	showPoll(); // Showpoll
	smileys(); // Add smileys to user-reaction
	openInNewWindow(); // Open links in new window
	// Add the attribute to the Flickr pictures
	$('.flickrgallery a').attr('rel', 'external');

// End $(document).ready;
});

// FUNCTIONS 
function sortableHome(){
	// Toggle the content divs
	if(document.getElementById('content-secondary')){
		toggleContent();
		dragdrop('#content-secondary','rightColumn');
		dragdrop('#content-tertiary','leftColumn');
		
		// Make the divs sortable
		$('#content-secondary, #content-tertiary').sortable({
			items:'.box, .magazine',
			helper:'clone',
			placeholder:'drag',
			//containment:'parent',
			cursor:'pointer',
			cancel:'span, a, input, label',
			opacity:'0.3'
		});
	}
}

function toggleAnim(){
	// FAQ & EVENT page ---------------
	$('#faq #content div').not('p.inleiding').hide();
	$('#faq h2').toggle(function() {
		$(this).next('div').slideDown(1000);
	}, function() {
		$(this).next('div').fadeOut(800);
	});

	// EVENT -------
	$('#evenementen #content li.details').hide();
	$('#evenementen #content li span.more').toggle(function() {
		$(this).parent().next('li').slideDown(1000);
	}, function() {
		$(this).parent().next('li').slideUp(800);
	});
	//---------------------
}

function quickJump(){
	// Scroll to for #quickjump menu---------------
	if(document.getElementById("quickjump")){
		//Quickjump, shows how to scroll the whole window
		$('#quickjump a').click(function(){
			//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
			$.scrollTo( this.hash, 800,{ });

			var target = $(this).attr('href');
			target = target.substr(11);
			$('table'+target).effect("pulsate", { times:1}, 2000);
			//$.scrollTo( this.hash, 800, { onAfter:animateScroll() });
			// Highlight --> TO DO: setTimeout for animation
			return false;
		});		

		$('a.top').click(function() {
			// Act on the event
			$.scrollTo('#top', 800, {});
			//return false;
		});
	}
}

function datePicker(){
	// Insert datepicker on report page
	if(document.getElementById("verslagen")){
		$('#datepicker').datepicker({
			dateFormat: "yy-mm-dd" // xxxx-xx-xx
		});
	}
}

function roundedCorners(){
	// Function needs corner.js
	/*	
	$("#content-secondary h3, #content-tertiary h3").corner("top 5px");
	$(".roundedbtm").corner("bottom 10px");
	$(".rounded").corner("10px");
	$(".article").corner("15px");
	*/
}

function stripeTables(){
	// Stripe tables
	$('#content table tbody tr:even, #content-secondary .results table tbody tr:even').css("background-color", "#e3f1ff");
	$('#content-tertiary table tbody tr:even').css("font-style", "italic");
	
	// Stripe tables when hovered
	$("#content table tbody tr").mouseover(function() {
		$(this).addClass("over");
	}).mouseout(function() {
		$(this).removeClass("over");
	});
}

function dragdrop(div, column) {
	$(div).sortable({
		//connectWith:['#content-tertiary']
		update: function(){
			var Value = $(div).sortable('serialize');
			createCookie(column, Value, 365);
		}
	});
}

function openInNewWindow() {
	$('a[rel="external"]').each(function() {
		$(this).attr({
			href: "javascript:window.open('" + $(this).attr("href") + "');void(0);", 
			title: "Link opent in een nieuw venster"
		});
	});
}

function showPoll(){
	$('#left_6 input').click(function() {
		var vote = this.value;
		var pollid = $('#left_6 input#pollid').attr('value');
		
		// Send the answer, get the results
		$.ajax({
		 	type: "POST",
			url: "../includes/inc/poll.php",
			data: {
				vote: ""+vote+"",
				pollid: ""+pollid+""
			},
			success: function(data){
				if(data.length >0) {
					$('#left_6 .content').html(data).hide();
					$('#left_6 .content').slideDown('slow');
				}
			},
			error: function(){
				// An error occured
				console.debug("Er is een fout opgetreden.");
			}
		});
		
	});
}

function smileys(){
	// Insert smileys and UBB code---------------------
	// Smileys
	$('.layout #smileys ul li a').click(function() {
		var reaction = document.getElementById('reactie');
		var smiley = $(this).attr('href');
		reaction.value = reaction.value+smiley;
		// Focus the field
		$('#reactie').focus();
		return false;
	});
}

