/** 
* Jquery cookie plugin 
**/
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('j.5=u(9,a,2){6(h a!=\'v\'){2=2||{};6(a===m){a=\'\';2.3=-1}4 3=\'\';6(2.3&&(h 2.3==\'n\'||2.3.k)){4 7;6(h 2.3==\'n\'){7=w C();7.B(7.z()+(2.3*A*o*o*E))}l{7=2.3}3=\'; 3=\'+7.k()}4 8=2.8?\'; 8=\'+2.8:\'\';4 b=2.b?\'; b=\'+2.b:\'\';4 c=2.c?\'; c\':\'\';d.5=[9,\'=\',q(a),3,8,b,c].t(\'\')}l{4 g=m;6(d.5&&d.5!=\'\'){4 e=d.5.x(\';\');D(4 i=0;i<e.f;i++){4 5=j.r(e[i]);6(5.p(0,9.f+1)==(9+\'=\')){g=y(5.p(9.f+1));s}}}F g}};',42,42,'||options|expires|var|cookie|if|date|path|name|value|domain|secure|document|cookies|length|cookieValue|typeof||jQuery|toUTCString|else|null|number|60|substring|encodeURIComponent|trim|break|join|function|undefined|new|split|decodeURIComponent|getTime|24|setTime|Date|for|1000|return'.split('|'),0,{}))

/**
* Styleswitch stylesheet switcher built on jQuery
**/
$(document).ready(function() {
	var c = $.cookie('style');
	if (c) switchStylestyle(c);
});

function switchStylestyle(styleName) {
	$('link[@rel*=style][@title]').each(function(i) {
		this.disabled = true;
		if (this.getAttribute('title') == styleName) this.disabled = false;
	});
	$.cookie('style',styleName, {expires: 365});
}

function textSize(value) {
	var textSize = $.cookie('textSize');
	if (isNaN(textSize)) {textSize = 85}
	if (textSize < 75) {textSize = 75}
	if (textSize > 145) {textSize = 145}
	self.status = textSize+" / "+value;
	if (textSize === null) {
		// cookie is not created so create
		$.cookie('textSize','85', {expires: 365});
		
	}
	textSize = parseFloat(textSize) + parseFloat(value);
	$.cookie('textSize',textSize, {expires: 365});
	$('#content').css("font-size",textSize+"%");
}

// Initialise Things
jQuery(function(){
	if ($.cookie('style') != "textonly") {
		// Things to initialise if not text-only

		// start superfish
	        $("ul.sf-menu").superfish(); 

		// Home tabs
		try {
			$('#faq-wrap').minitabs(1, 'none'); 
		} catch(err) {
			// faq-wrap doesn't exist
		}

		// Concertina and toggle Function
		$(document).ready(function(){
			$(".csc-frame-07 h2").siblings().hide();
			$(".csc-frame-07 h2").click(function(){
			    $(".csc-frame-07 h2").siblings().slideUp("fast");
			    $(".concertina-open").removeClass("concertina-open");
			    $(this).parent().addClass("concertina-open");
			    $(this).siblings().slideDown("fast",function(){});
			});
			
			$(".csc-frame-08 h2").siblings().hide();
			$(".csc-frame-08 h2").click(function(){
			    $(this).siblings().slideToggle("fast");
			    if( $(this).parent().hasClass("toggle-open") ){
				$(this).parent().removeClass("toggle-open");
			    } else {
				$(this).parent().addClass("toggle-open");
			    }
			});
			
		});

		// attach ajax news functions
		$('.tx-ttnews-browsebox a').click(ajaxNews);
	
		// check if an onqform was submitted and if it was inside a toggle or concertina open and scroll to	
		if($(".onqform_error_message").length > 0) {
			$(".onqform_error_message:first").parents(".csc-frame").siblings().show();
			$(".onqform_error_message:first").parents("div.csc-frame h2").eq(0).addClass("toggle-open");
			pScroll = $(".onqform_error_message:first").parents(".csc-frame").offset().top;
			$('html,body').animate({scrollTop: '+=' + pScroll + 'px'}, 1000);
			//alert("onq error detected");
		}

	}
	// change text size if textSize cookie is set
	if ($.cookie('textSize') != null) {
		$('#content').css("font-size",$.cookie('textSize')+"%");
	}



	// Login 
	$('#member_login #tx-newloginbox-pi1-login_submit').click(memberLoginSubmit);
	$("#member_login #bottom").click(function(){
		if( $(this).hasClass('open') ){
			$("#member_login").animate({
				top: "-176px" //TODO should come down according to height of #member_login div
			}, 500, function(){
				if (jQuery.browser.msie) {
				  if ((parseInt(jQuery.browser.version) > 6)) {
					$("#member_login").css("z-index","600");
					$("#accessibility_menu").css("z-index","700");	
					}
				} else {
					$("#member_login").css("z-index","600");
					$("#accessibility_menu").css("z-index","700");
				}
			});
			$("#member_login #login-arrow").attr("src","/fileadmin/template/main/cyt/img/login-arrow-down.gif");
			$(this).removeClass("open");
		} else {
			$("#member_login").animate({
				top: "0px" //TODO should come down according to height 
			}, 500);
			$("#member_login #login-arrow").attr("src","/fileadmin/template/main/cyt/img/login-arrow-up.gif");
				if (jQuery.browser.msie) {
				  if ((parseInt(jQuery.browser.version) > 6)) {
					$("#member_login").css("z-index","700");
					$("#accessibility_menu").css("z-index","600");
					}
				} else {
					$("#member_login").css("z-index","700");
					$("#accessibility_menu").css("z-index","600");
				}
			$(this).addClass("open");
		}
		});
});

// Member login
function memberLogin(button) {
	$(button).attr("onClick","");
	$('#member_login #top').load('/index.php?id=12177', function() {
		$('#member_login #top .tx-newloginbox-pi1-forgotP').hide();
		$('#member_login #tx-newloginbox-pi1-login_submit').click(memberLoginSubmit);
		$('#member_login #top').fadeIn('fast');
	});
	return false;
}

function memberLoginSubmit() {
	$('#member_login #top #tx-newloginbox-pi1-login_submit').replaceWith("<img src='/fileadmin/template/main/cyt/img/mini-throbber.gif' /> Logging in");
	jname = $("#member_login #top #tx-newloginbox-pi1-user").val();
	jpass = $("#member_login #top #tx-newloginbox-pi1-pass").val();
	$.post('/index.php?id=12177',{
		user: jname,
		pass: jpass,
		pid: '438',
		redirect_url: '',
		submit: 'login',
		logintype: 'login'
		}, function(data) {
			$('#member_login #top').html(data);
			$('#member_login #top #tx-newloginbox-pi1-login_submit').click(memberLoginSubmit);
	}, "html");
	return false;
}

// Ajax News Browser function
function ajaxNews() {
	//alert("Ajax news box");
	link = $(this).attr('href');
	contentWrapper = $(this).parents().filter('.content_wrapper');
	contentId = $(contentWrapper).attr('id');
	$(contentWrapper).prepend('<div class="throbber"></div>');
	//alert(link + " - " + contentId);	
	$(contentWrapper).load(link+" #"+contentId, function() {
		$('.tx-ttnews-browsebox a').click(ajaxNews);
	});	
	return false;
}




