/* ------------------------------------------------
global.js	Javascript functionality
Date: 		update july 2011
Version: 	2.0
Author: 	Pim Hoogendoorn - http://www.willenium.nl
Email: 		pim.hoogendoorn[at]willenium.nl	
-----------------------------------------------------*/

$(document).ready( function()
{
	$('body').addClass('domenabled');
	// Superfish delays nav hide()
	$('#navigation ul').superfish();
	/* Load default functions */
	devecht.init();
	// Add the attribute to the Flickr pictures
	$('.flickrgallery a').attr('rel', 'external');
	//$(document).pngFix();	// PNG fix
});

if (typeof devecht === 'undefined') devecht = {};

var devecht = {
	init: function()
	{
		//this.sortableHome();
		this.openInNewWindow();
		this.stripeTables();
		this.roundedCorners();
		this.datePicker();
		this.quickJump();
		this.toggleAnim();
		this.showPoll();
		this.smileys();
	},
	resizeIframe: function() {
		// Set frameheight
		var correctDomain = document.domain;
		document.domain = 'mijnclub.nu';

		console.log(document.getElementById('frame'));
		/*var frame = '',
			frameHeight = frame.find('document').height() + $('#branding').outerHeight(true),
			parentWidth = frame.parent().innerWidth();
			//document.domain = 'mijnclub.nu';
			

		if(frame.parents('body').attr('id') != 'home'){
			frame.css({
				width: parentWidth,
				height: frameHeight
			});
		}*/

	},
	resizeIframeToFitContent: function(iframe) {
	    // This function resizes an IFrame object to fit its content.
	    // The IFrame tag must have a unique ID attribute.
	    //iframe.height = document.frames[iframe.id].document.body.scrollHeight;
	},
	
	sortableHome: function()
	{
		// Toggle the content divs
		if($('#content-secondary').length>0){
			toggleContent();
			//dragdrop('#content-secondary','rightColumn');
			//dragdrop('#content-tertiary','leftColumn');
			
			// Make the divs sortable
			$('#content-secondary, #content-tertiary').sortable({
				items:'.box',
				helper:'clone',
				placeholder:'drag',
				//containment:'parent',
				cursor:'pointer',
				cancel:'span, a, input, label',
				opacity:'0.6'
			});
		}
	}, 
	// Open links in new window
	openInNewWindow: function() 
	{
		$('a[rel="external"]').each(function() {
			$(this).attr({
				title: "Deze link zal openen in een nieuw venster"
			}).click(function(){
				window.open(this.href);
				return false;
			});
		});
	},

	sortableHome: function ()
	{
		// 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'
			});
		}
	},
	
	toggleAnim: function()
	{
		// FAQ & EVENT page ---------------
		$('#faq div').not('p.inleiding').hide();
		$('#faq h2').toggle(function() {
			$(this).next('div').slideDown(1000);
		}, function() {
			$(this).next('div').fadeOut(800);
		});
	
		// EVENT -------
		$('#evenementen li.details').hide();
		$('#evenementen li span.more').toggle(function() {
			$(this).parent().next('li').slideDown(1000);
		}, function() {
			$(this).parent().next('li').slideUp(800);
		});
		//---------------------
	},

	quickJump: function(){
		// 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;
			});
		}
	},

	datePicker: function()
	{
		// Insert datepicker on report page
		if(document.getElementById("verslagen")){
			$('#datepicker').datepicker({
				dateFormat: "yy-mm-dd" // xxxx-xx-xx
			});
		}
	},

	roundedCorners: function()
	{
		// Function needs corner.js
		/*	
		$("#content-secondary h3, #content-tertiary h3").corner("top 5px");
		$(".roundedbtm").corner("bottom 10px");
		$(".rounded").corner("10px");
		$(".article").corner("15px");
		*/
	},

	stripeTables: function()
	{
		// Stripe tables
		$('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
		$("table tbody tr").mouseover(function() {
			$(this).addClass("over");
		}).mouseout(function() {
			$(this).removeClass("over");
		});
	},

	dragdrop: function(div, column) 
	{
		$(div).sortable({
			//connectWith:['#content-tertiary']
			update: function(){
				var Value = $(div).sortable('serialize');
				createCookie(column, Value, 365);
			}
		});
	},

	showPoll: function()
	{
		$('#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(){
				}
			});
			
		});
	},

	smileys: function()
	{
		// 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;
		});
	}

}
