var paletClosed = true;

// If page is ready
$(document).ready(function(){	
	
	
	/* Main Menu */
	
	$('#main-menu li a').mouseover(function() {
		
		$('#sub-menu li').hide();
		$('#sub-menu li.'+$(this).attr('id')).show();
		
		$('#sub-menu').stop().show();
		$('#sub-menu').css('margin-top',$(this).parent().index() * 23);		
	});
	
	$('#main-menu').mouseover(function() {
		$('#sub-menu').stop().show();
	});
	
	$('#sub-menu').mouseover(function() {
		$('#sub-menu').stop().show();
	});
	
	$('#main-menu').mouseout(function() {
		$('#sub-menu').stop().hide();
	});
	
	$('#sub-menu').mouseout(function() {
		$('#sub-menu').stop().hide();
	});
	
	
	/* Inspiration chooser */	
	
	/*
	$('#inspiration-chooser table input').click(function() {		
		
		$wasChecked = $(this).attr('checked');
		
		
		
		$('#inspiration-chooser table input:checked').attr('checked', false);		
		if($wasChecked)
			$(this).attr('checked', true);

		$('.ez-checked').removeClass('ez-checked');
	
		// create array of selected categories
		var checkedCategories = $('#inspiration-chooser table input:checked').map(function(i,n) {
	        return $(n).val();
	    }).get(); 
		
		randomkey = randomPassword(15);
		
		$('#inspiration-result .holder').fadeOut(500,function(){		
			
			$.getJSON('/'+language+'/'+sector+'/inspiration/search/category-changed/', {ajax : true, randomkey : randomkey, checked_categories : checkedCategories}, function(json){				
				$('#inspiration-result .holder').empty();
				$('#inspiration-result .holder').append(json.content);
				$('#inspiration-result .holder').fadeIn();
				
				defaultInit();
			});		
		});	
	}); */
	
	
		
	
	$('#header-slider').cycle({
	    fx:      'fade',
	    timeout:  4000,
		random:  0
	});
	
	defaultInit();

	$('.options-holder input').ezMark();
	$('input.ezmark').ezMark();
	
	
	$('#palet #palet-btn').click(function(){
		if(paletClosed) {
			$('#palet').stop().animate({left : 0});
			paletClosed = false;
			$('#palet').addClass('open');
			
		} else {
			$('#palet').stop().animate({left : -42});
			$('#palet').removeClass('open');
			paletClosed = true;
		}
		return false;
	});
	$('#palet-zakelijk #palet-btn').click(function(){
		if(paletClosed) {
			$('#palet-zakelijk').stop().animate({left : 0});
			paletClosed = false;
			$('#palet-zakelijk').addClass('open');
			
		} else {
			$('#palet-zakelijk').stop().animate({left : -42});
			$('#palet-zakelijk').removeClass('open');
			paletClosed = true;
		}
		return false;
	});
	
	$('#palet, #palet-zakelijk').stop().css({left : 0});
	paletClosed = false;
	$('#palet, #palet-zakelijk').addClass('open');
	
	startSuggestionTicker();
	
});

function defaultInit() {
		
	Shadowbox.init({ 
		skipSetup: true,
		onOpen: function() {
                if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPad/i))) {
                        $("#sb-container").css("top", $(window).scrollTop());
                        $(window).bind('scroll', function() {
                                $("#sb-container").css("top", $(window).scrollTop());
                        });
                }
        },
        onClose: function() {
                if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPad/i))) {
                        $(window).unbind('scroll');
                }
        } 
	});
	Shadowbox.setup(); 
	
	/* NUMBER MENU */
	
	$('.number-menu a').click(function() {
		
		target = $(this);
		
		if($(this).parent().attr('id') == 'suggestion-box-number-menu') {
			stopSuggestionTicker();			
		}
		
		replaceHolderContent(target);
		return false;
		
	});

	
	
	if($('.addthis_toolbox').length > 0) {
		addthis.toolbox('.addthis_toolbox');
	}
	
	/*
	$('#inspiration-result-grid .box').click(function() {		
		
		target = $(this);
		holder 	= target.closest('.holder');
		var url = target.find('.invisible-link').attr('href')+'/';
		
		randomkey = randomPassword(15);
		
		holder.fadeOut(500,function(){
			$.getJSON(url, {ajax : true, randomkey : randomkey}, function(json){				
				holder.empty();
				holder.append(json.content);
				holder.fadeIn();				
				defaultInit();
			});	
		});
	
	});
	*/
	
	$('#guest-book-form #name').click(function() {
		
		$(this).attr('value', '');
		$(this).unbind('click');
		
	});	

}

function replaceHolderContent(target) {
	
	holder 	= target.closest('.holder');
	var url = target.attr('href')+'/';
		
	randomkey = randomPassword(15);
	
	holder.fadeOut(500,function(){
		$.getJSON(url, {ajax : true, randomkey : randomkey}, function(json){				
			holder.empty();
			holder.append(json.content);
			holder.fadeIn();
			defaultInit();
		});	
	});
	return false;
}

function showInspirationOptions(index, target) {
	
	$('#inspiration-chooser .number-menu a').removeClass('active');
	target.addClass('active');
	
	$('.options-holder').fadeOut('fast', function() {
		$('#options-holder-'+index).fadeIn();		
	});
	
	return false;
}



function randomPassword(length)
{
   chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
   pass = "";

   for(x=0;x<length;x++)
   {
      i = Math.floor(Math.random() * 62);
      pass += chars.charAt(i);
   }

   return pass;
}



var suggestionTickerTimer;
var currentSuggestionIndex = 0;
var numberOfSuggestions;
var suggestionLinks;



function startSuggestionTicker() {

	suggestionTickerTimer = $.timer(5000, function(){	
		nextSuggestion();		
	});
	
}

function stopSuggestionTicker() {
	if(suggestionTickerTimer != undefined) {
		$.clearTimer(suggestionTickerTimer);
	}
}

function nextSuggestion() {
	suggestionLinks = $('#suggestion-box-number-menu').children();
	numberOfSuggestions = suggestionLinks.length;
	
	var index = currentSuggestionIndex+1;
	
	if(index > (numberOfSuggestions -1)) {
		index = 0;
	}	

	replaceHolderContent($(suggestionLinks[index]));
	
	currentSuggestionIndex = index;
	
	startSuggestionTicker();	
}




function suggestionReadMore() {
	
	stopSuggestionTicker();	
	
	suggestionBox = $('#suggestion-box');
	
	suggestionBox.find('.detail-box').fadeIn('slow');
	suggestionBox.find('.start-box').fadeOut('slow');	
	suggestionBox.find('.side-content').fadeOut('slow');	
	suggestionBox.find('.direct-links').fadeIn('slow');
}

function suggestionBackToStart() {
	
	startSuggestionTicker();
	
	suggestionBox = $('#suggestion-box');
	
	suggestionBox.find('.detail-box').fadeOut('slow');
	suggestionBox.find('.start-box').fadeIn('slow');	
	suggestionBox.find('.side-content').fadeIn('slow');	
	suggestionBox.find('.direct-links').fadeOut('slow');
}


 



