/*!
 * Content Syndication Initialization Script for VTwonen v1.0
 *
 * Entry script which loads all needed functions like jQuery, Fancybox and all functions
 * This script uses jQuery http://jquery.com and Fancybox http://fancybox.net/
 *
 * Copyright 2011, TIE Kinetix
 * Siebe van Ineveld
 * http://contentsyndication.tiekinetix.com
 *
 * Date: Thu Sept 22, 2011
 *
 * 2011-09-22 SvI	Initial version.
 */

function csp_loadScript(url, callback){
// Dynamically load an external javascript file using a readystate check. Needed to load jQuery dynamically on the page where this script will exist.
	var script = document.createElement("script");
	script.type = "text/javascript";
	if (script.readyState){  //IE
		script.onreadystatechange = function(){
			if (script.readyState == "loaded" || script.readyState == "complete"){
            	script.onreadystatechange = null;
				callback();
			}
		};
	} else {  //Others
		script.onload = function(){
			callback();
		};
	}
	script.src = url;
	document.getElementsByTagName("head")[0].appendChild(script);
} // End csp_loadScript();

function csp_loadFancyBox(scriptSrc, cssHref){
	if (!$.fancybox) {
		// Check if Fancybox is not already loaded
		csp_loadScript(scriptSrc, function() {
			$("head").append('<link rel="stylesheet" href="'+cssHref+'" type="text/css" media="screen" />');
			csp_documentReady();
		}); // Fancybox is now for sure loaded
		} else {
		csp_documentReady();
	}
} // End csp_loadFancyBox();

function csp_setFancyBox(){
	//  Set fancybox
	$(elId).fancybox({
		'width'				: 840,
		'height'			: '100%',
		'margin'			: 20,
		'autoScale'			: false,
		'autoDimensions'	: false,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'none',
		'overlayShow'		: true,		
		'overlayColor'		: '#fff',
		'overlayOpacity'	: 0.85,
		'type'				: 'iframe'
	}); // End fancybox settings
} // End appendCSPBanner();

function csp_documentReady(){
	$(document).ready(function() {
		$(elId).attr("href", cspSrc);
		csp_setFancyBox();
	});
} // End csp_documentReady();

function csp_init() {
// load all necessary tools
	var jQuerySrc = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js";
//	var jQueryMouseWheel = "http://vtwonen.homedeco.sanoma.tiekinetix.com/global/files/homedeco/script/fancybox/jquery.mousewheel-3.0.4.pack.js";
	var fancyBoxSrc = "http://vtwonen.homedeco.sanoma.tiekinetix.com/global/files/homedeco/script/fancybox/jquery.fancybox-1.3.4.pack.js";
	var fancyBoxCssHref = "http://vtwonen.homedeco.sanoma.tiekinetix.com/global/files/homedeco/script/fancybox/jquery.fancybox-1.3.4.css";

	if (typeof jQuery == 'undefined'){
		// jQuery is unavailable, start loading with a readystate/onload check, then load FancyBox
		csp_loadScript(jQuerySrc, function (){
			csp_loadFancyBox(fancyBoxSrc, fancyBoxCssHref);
		});
	} else {
		csp_loadFancyBox(fancyBoxSrc, fancyBoxCssHref);
	}
}

// Initialization
var cspSrc = "http://vtwonen.homedeco.sanoma.tiekinetix.com/global/files/homedeco/";
var elId = "#csp-homedeco";
csp_init();
