/*
	Author:	Jens Krause - www.websector.de
	Date: 	2011-05-23 
 */

/* #misc.
 *******************************************/

var lastUpdate = "09-05-2011";

function initBackButton() {
	$('a.back').click(function() {
		parent.history.back();
		return false;
	});
};

/* #downloads
 *******************************************/

function initDownloads(event) {
	// private
	$("input#cbPrivate").click(function(event) {
		toggleDownloadButton(event);
	});
	$("li#downloadPrivate p").click(function(event) {
		inactiveDownloadButtonClickHandler('Private');
	});
	// commercial
	$("input#cbCommercial").click(function(event) {
		toggleDownloadButton(event);
	});
	$("li#downloadCommercial p").click(function(event) {
		inactiveDownloadButtonClickHandler('Commercial');
	});
	// unlimited
	$("input#cbUnlimited").click(function(event) {
		toggleDownloadButton(event);
	});
	$("li#downloadUnlimited p").click(function(event) {
		inactiveDownloadButtonClickHandler('Unlimited');
	});
};

function toggleDownloadButton(event) {

	event.stopPropagation();

	var name = $(event.target).attr("id");
	var id = name.substring(2, name.length);
	var isChecked = $("input#cb" + id).is(":checked");

	var anchor = $("li#download" + id + " a");
	var paragraph = $("li#download" + id + " p");
	var label = $("label#lbl" + id);
	var labelAnchor = $("label#lbl" + id + " a");
	var errorTxtPrivate = $("p#errorTxt" + id);

	if (label.hasClass('error')) {
		label.removeClass('error');
	}

	if (labelAnchor.hasClass('error')) {
		labelAnchor.removeClass('error');
	}

	if (isChecked) {
		// hide paragraph
		paragraph.hide();

		// remove 'hidden' class if available
		// we do need it anymore

		if (anchor.hasClass('hidden')) {
			anchor.removeClass('hidden');
		}
		// show anchor
		anchor.show();
	} else {
		// hide anchor
		anchor.hide();
		// show paragraph
		paragraph.show();
	}
	;
};

function inactiveDownloadButtonClickHandler(id) {

	var label = $("label#lbl" + id);
	var anchor = $("label#lbl" + id + " a");
	label.addClass('error');
	anchor.addClass('error');
}

/*******************************************************************************
 * #faq
 ******************************************************************************/

//
// initialize faqs
function initFAQs() {
	//
	// add event handler to all questions

	$(".question").mouseover(function(event) {
		faqMouseOverHandler(event);
	});

	$(".question").mouseout(function(event) {
		faqMouseOutHandler(event);
	});

	$(".question").click(function(event) {
		faqClickHandler(event);
	});

}

//
// change the visibility of all answers on click
function faqClickHandler(event) {
	//
	// get question
	var question = $(event.target);

	if (question.hasClass('questionClicked'))
		question.removeClass('questionClicked');
	else
		question.addClass('questionClicked');

	var answer = $(event.target).next();
	//
	// hidden class is set "by hand"
	// so first we have to remove it to show an answer
	if (answer.hasClass('hidden')) {
		answer.removeClass('hidden');
		return;
	}

	answer.toggle();

}

//
// question mouse out handler
function faqMouseOutHandler(event) {
	var question = event.target;
	$(question).removeClass('questionOver');
}
//
// question mouse out handler
function faqMouseOverHandler(event) {
	var question = event.target;
	$(question).addClass('questionOver');
}

/*******************************************************************************
 * #pimpit
 ******************************************************************************/

var lang;
var LANG_DEFAULT = "EN";

function initPimpit(langID) {

	lang = (langID != null) ? langID : LANG_DEFAULT;
	//
	// create select box
	$("SELECT").selectBox('create');
	//
	// add event listener
	$("SELECT").selectBox().change(function(event) {
		pimpitChangeHandler($(this).val());
	});

	// $("SELECT").selectBox('value', 0);
	//
	// trigger selecting first list entry 
	pimpitChangeHandler($("SELECT").selectBox().val());

	//
	// init syntax highlighter
	//	$.SyntaxHighlighter.init({
	//        'highlight':true
	//    });

	// $.syntax();

}

//
//change the visibility of all answers on click
function pimpitChangeHandler(id) {

	var flashVars = {
		XMLPath : "xml/examples.php?configID=" + id,
		langID : lang,
		cacheXML : "true"
	};

	// console.log(" XMLPath " + "xml/examples.php?configID=" + id);

	pimpitShowExample(flashVars, id);

	pimpitCodeExample(flashVars, id);

}

function getPrePath() {
	return (lang == LANG_DEFAULT) ? "" : "../";
}

function pimpitShowExample(flashvars, id) {

	//
	// 1. empty wrapper (OBJECT etc...)
	$('#flashcontentWrapper').empty();

	//
	// create and add flashcontent DIV
	var containerID = "flashcontent" + id;
	var flashcontentDIV = '<div id="' + containerID + '">&nbsp;</div>';
	$(flashcontentDIV).appendTo("#flashcontentWrapper");

	//
	// use swfobject to show example
	var params = {
		quality : "best",
		allowFullScreen : "true",
		menu : "false",
		allowScriptAccess : "sameDomain",
		bgcolor : "#F2F2F2",
		wmode : "opaque"
	};
	var attributes = {
		id : "ws-slideshow",
		name : "ws-slideshow"
	};

	swfobject.embedSWF(getPrePath() + "ws-slideshow.swf?lastUpdate="
			+ lastUpdate, containerID, "960", "500", "9.0.28",
			"expressInstall.swf", flashvars, params, attributes);
}

function pimpitCodeExample(flashVars, id) {

	//
	// 1. empty code wrapper
	$('#codeContentWrapper').empty();

	//
	// create and add flashcontent DIV

	$.ajax({
		url : flashVars.XMLPath,
		context : document.body,
		dataType : "text",
		success : pimpitXMLLoaded,
		error : function() {
			alert("Error loading XML file");
		}
	});

}

function pimpitXMLLoaded(xml) {

	//
	// 1. empty code wrapper
	$('#codeContentWrapper').empty();

	var codePRE = '<pre class="syntax xml">' + safe_tags(xml) + '</pre>';
	$(codePRE).appendTo("#codeContentWrapper");
	$.syntax({
		blockSelector : "pre.syntax",
		blockLayout : "fixed",
		context : $('#codeContentWrapper')
	});

}

/*
 * Fast way to replace HTML Tags to HTML entities using RegEx
 * @see: http://stackoverflow.com/questions/5499078/fastest-method-to-escape-html-tags-as-html-entities
 */
function safe_tags(str) {
	return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g,
			'&gt;');
}

/* #configurator
 *******************************************/

function initConfigurator(event) {
	// license key
	$("input#cbLicenseKey").click(function(event) {
		toggleDownloadButton(event);
	});
	$("li#downloadLicenseKey p").click(function(event) {
		inactiveDownloadButtonClickHandler('LicenseKey');
	});
	//
	// trail version
	$("input#cbTrail").click(function(event) {
		toggleTrailVersion(event);
	});
	$("li#downloadTrail img").click(function(event) {
		inactiveDownloadButtonClickHandler('Trail');
	});
};


function toggleTrailVersion(event) {

	event.stopPropagation();

	//
	// remove error classes 
	var label = $("label#lblTrail");
	var labelAnchor = $("label#lblTrail a");

	if (label.hasClass('error')) {
		label.removeClass('error');
	}

	if (labelAnchor.hasClass('error')) {
		labelAnchor.removeClass('error');
	}
	
	var isChecked = $("input#cbTrail").is(":checked");
	var div = $("li#downloadTrail div");
	var img = $("li#downloadTrail img");
	
	if (isChecked) {
		//
		// we don't need the hidden class anymore
		// which is used for initial style only...
		if (div.hasClass('hidden'))
			div.removeClass('hidden');
		
		div.show();
		img.hide();
		
	} else {
		div.hide();
		img.show();
	}

}

