/*
 * $ Default javascript file
 *
 * Copyright (c) 2007 That's-id Multimedia
 */

/*------------------------------------------------------------------------------
	Methode om CSS background-image cache te forceren voor IE6
------------------------------------------------------------------------------*/

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


/*------------------------------------------------------------------------------
	onLoad handler
------------------------------------------------------------------------------*/

var bodyInit = true;
function onLoad(root)
{
	if (typeof root != 'string') root = 'body';

	initSIFR(root);

	// one time initialization
	if (bodyInit) {
		// dynamic handlers
		$.dynamic.registerAction('index', {blockId: 'indexEntries', hdl: reloadEntryIndex});
		$.dynamic.registerAction('reviews');
		//if (typeof($.historyInit)=='function') $.historyInit($.dynamic.loadHash);
		
		// werkenbij button
		$('#werkenbij .btn').css('cursor', 'pointer').toggle(function(){
			$(this).addClass('active');
			$('#werkenbij-dropdown').show();
			if (typeof pageTracker != 'undefined') pageTracker._trackPageview("/werkenbij/up"); 
			initSIFR('#werkenbij-dropdown');
		}, function(e){
			$(this).removeClass('active');
			if (typeof pageTracker != 'undefined') pageTracker._trackPageview("/werkenbij/down");
			$('#werkenbij-dropdown').hide();
		});
		
		// overlay events
		if (typeof $.overlay == 'object') {
			$.overlay.defaultOptions.imageNavText = 'Afbeelding % van %';	
			$.overlay.event.bind('show', function(e,$popup,options) { onLoad('#popup_'+options.name); });
			if (typeof(supersleight) == "object" ) {
				$.overlay.event.bind('open show', function(e,$popup,options) {
					supersleight.limitTo('popup_'+options.name);
					supersleight.run();
				});
			}
		}
	}
	
	// validation
	if (typeof $.validator == 'object') {
		if (typeof(checkEntryForm)=='function') {
			$(root+' form#frm_reg3').validatorEnable({hdlCallback: checkEntryForm});
			$(root+' form#frm_idee').validatorEnable({hdlCallback: checkEntryForm, unloadWarning: true});
		}
		$(root+' form#frm_rate, '+root+' form#frm_review, '+root+' form#frm_subreview').validatorEnable({hdlCallback: setRating});
		$(root+' form').validatorEnable();
	}
	
	if (typeof $.overlay == 'object') {
		$(root+' img[rel=zoom]').setGallery({thumbId: 4, enableImgNav: false, minHeight: 300});
		$(root+' .entImages td').setGallery({thumbId: 4, minHeight: 300});
	}

	if (typeof $.tooltip == 'object') {
		$(root+' div.formInfo').tooltip({bodyHandler: function(){
			return $(this).children('div.content').html();
		}});
	}
	
	if (root=='body' || root=='#indexEntries') {
		$('#indexEntries a[rel=dynamic]').dynamicLink();
	}
	if (root=='body' || root=='#reviews') {
		$('#reviews a[rel=dynamic]').dynamicLink();
	}
	
	// run supersleight
	if (typeof(supersleight) == "object" ) {
		if (bodyInit) {
			supersleight.run();
		} else {
			supersleight.limitTo(root.substring(1));
			supersleight.run();
		}
	}
	
	bodyInit = false;
}


/*------------------------------------------------------------------------------
	Entry index
-----------------------------------------------------------------------------*/
var indexData = {order: 1, period: 'all', page: 1}, noInit=false;

function reloadEntryIndex(action, options, data)
{
	noInit = true;
	$('#SFRM_index_order_'+data.order).attr('checked', 'checked').click();
	$('#SFRM_index_period_'+data.period).attr('checked', 'checked').click();
	noInit = false;
	
	return $.dynamic.loadBlock(action, options, data);
}

function setIndexOrder(ob)
{
	if (ob.value!=indexData.order) {
		if (ob.value!=1) {
			 $('#indexPeriods').removeClass('ghosted');
			 $('#indexPeriods input[type=radio]').attr('disabled', '');
		} else {
			 $('#indexPeriods').addClass('ghosted');
			 $('#indexPeriods input[type=radio]').attr('disabled', 'disabled');
			 $('#indexPeriods input[type=radio]:last').attr('checked', 'checked');
		}
		$("#indexOrders label").removeClass('act');
		$("#indexOrders label[for='"+ob.id+"']").addClass('act');
		indexData.order = ob.value;
		indexData.page = 1; // reset page number
		if (!noInit) $.dynamic.setAction('index', indexData);
	}
}

function setIndexPeriod(ob)
{
	if (ob.value != indexData.period) {
		$("#indexPeriods label").removeClass('act');
		$("#indexPeriods label[for='"+ob.id+"']").addClass('act');
		indexData.period = ob.value;
		if (!noInit) $.dynamic.setAction('index', indexData);
	}
}


/*------------------------------------------------------------------------------
	Entry details
-----------------------------------------------------------------------------*/

/* video status */
function checkVideoStatus()
{
	$.ajax({
		url: '?action=video',
		success: function(c) {
			if (c==='false') {
				setTimeout(checkVideoStatus,4000);
			} else {
				$('#entVideo .sub').html(c);
			}
		}
	});
}

/* Set rating */
function setRating(validatedOK,form)
{
	if (validatedOK) {
		var data, 
			result = doBgFormPost(form);
			
		if (result) {
			try {
				eval('data='+result);
			} catch(err){};
			if (data) {
				if (data.score) {
					$('#idee_score').html(data.score);
					onLoad('#idee_score');
				}
				if (data.reviews) {
					$('#reviews').html(data.reviews);
					onLoad('#reviews');
				}
				showDialog('<h3>Bedankt voor je stem/reactie</h3><p>Venster sluit in 3 seconden</p>', false, 3000);
				
				if (data.rat_Id) location.hash = '#rev'+data.rat_Id;
			}
		}
	}
	return false;
}

/*------------------------------------------------------------------------------
	Various
-----------------------------------------------------------------------------*/

function checkCookies()
{
	var cookieName = 'testCookie';
	document.cookie = cookieName + '=cookieValue';
	var cookiesEnabled = document.cookie.indexOf(cookieName) != -1;
	if (!cookiesEnabled) {
		location.href='/niy/error/nocookies';
	}
}


function setIndex(pub_Id, page, archive)
{
	container = 'index_content';
	$.get('/dynamics/getindex.php',{ pub_Id: pub_Id, page: page, archive: archive },
		function(msg) {
			document.getElementById('index_container').innerHTML=msg;
		});
}

function doBgFormPost(form)
{
	var result = false;

	$.ajax({
		async: false,
		url: form.action || location.href,
		type: form.method || 'post',
		data: $(form).serialize(),
		success: function(msg) {
			if (msg) result = msg;
		},
		error: function(msg) {
			alert('error '+msg);
		}
	});
	
	return result;
}



function showDialog(text, noCloseButton, timeout)
{
	$.overlay.open({
		name: 'dialog',
		content: text,
		minHeight: 50,
		enableCloseButton: (!noCloseButton),
		maskTrigger: (!noCloseButton),
		animation: 'none',
		showHandler: function($popup, options){
			if(timeout) setTimeout(function() { $.overlay.hide($popup, options); }, timeout);
		}
	});
}

function openPopup(name, url)
{
	$.overlay.open({
		name: name,
		url: url,
		minHeight: 100
	});
	return false;
}

function serializeObject(ob)
{
	var url = [];
	$.each(ob, function(name, value)
	{
		url.push(name+'='+escape(value));
	});
	
	return url.join('&');
}
