/*===========================
*
* jQuery Functions
* Date Created: 29/04/2009
* Last Update: 24/09/2009
*
*===========================*/

$(document).ready(function(){
	$("a.toggle").click(function(){
		$(this).next("div.container-toggle").slideToggle("slow").siblings("p:visible").slideUp("slow");
		$(this).toggleClass('toggle-active');
	});

	$('.setclear').each(function() {
		var default_value = this.value;
		$(this).focus(function(){
			if(this.value == default_value) {
				this.value = '';
			}
		});

		$(this).blur(function(){
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});

	/*===========================
	*
	* Pluginname: jquery.stylish-select.min.js
	* Date Created: 24/09/2009
	* Last Update: 24/09/2009
	*	Source: http://scottdarby.com/
	* Description: jQuery plugin to replace a select drop down box with a stylable unordered list
	*
	*===========================*/
	$('#language').sSelect();

	/*===========================
	*
	* Pluginname: jquery.tipsy.js
	* Date Created: 24/09/2009
	* Last Update: 24/09/2009
	*	Source: http://onehackoranother.com/projects/jquery/tipsy/
	* Description: tooltip plugin for jQuery
	*
	*===========================*/
	$('#cUser a').tipsy({gravity: 's'});
	$('#cWall a').tipsy({gravity: 's'});
	$('#cMsg a').tipsy({gravity: 's'});

	$('img#biggerfont').tipsy({gravity: 's'});
	$('img#normalfont').tipsy({gravity: 's'});


	/*===========================
	*
	* Pluginname: jquery.switchstylesheet.js
	* Date Created: 24/09/2009
	* Last Update: 24/09/2009
	*	Source: http://scottdarby.com/
	* Description: Can create multiple groups of alternate stylesheets to change
	*
	*===========================*/
	$(".switch-fontsize").switchstylesheet( { seperator:"size"} );

	/*===========================
	*
	* Pluginname: -
	* Date Created: 23/11/2009
	* Last Update: 23/11/2009
	*	Source: -
	* Description: jQuery plugin to replace a select drop down box with a stylable unordered list
	*
	*===========================*/
	$('.form-select').sSelect();

	/*===========================
	*
	* Pluginname: -
	* Date Created: 23/11/2009
	* Last Update: 23/11/2009
	*	Source: -
	* Description: jQuery plugin for styling file input elements
	*
	*===========================*/
	$("input#groupimage").filestyle({
		image: "fileadmin/project/myallergo.de/templates/images/bg-select-file.png",
		imageheight : 24,
		imagewidth : 129,
		width : 266
	});

	/*===========================
	*
	* Pluginname: -
	* Date Created: 23/11/2009
	* Last Update: 07/12/2009
	*	Source: -
	* Description: -
	*
	*===========================*/
	$('input.radio').checkBox();
	$('input.checkbox').checkBox();

	/*===========================
	*
	* Pluginname: shadowbox.js
	* Date Created: 09/12/2009
	* Last Update: 09/12/2009
	*	Source: http://www.shadowbox-js.com
	* Description: -
	*
	*===========================*/
	Shadowbox.init();

	/*===========================
	 * adtag style
	 * Date Created: 14/12/2009
	 * Last Update: 14/12/2009
	 *===========================*/
	checkWallpaper();

	/*========================
	 * calendar datepicker
	 * =========================*/
	$('#date_start_day').datepick({
		dateFormat: 'dd.mm.yy',
		showOn: 'button',//both
		buttonImageOnly: true,
		buttonImage: 'fileadmin/project/myallergo.de/templates/javascript/jquery.datepick/calendar-green.gif'
	},$.datepick.regional['de']);

	$('#date_end_day').datepick({
		dateFormat: 'dd.mm.yy',
		showOn: 'button',//both
		buttonImageOnly: true,
		buttonImage: 'fileadmin/project/myallergo.de/templates/javascript/jquery.datepick/calendar-green.gif'
	},$.datepick.regional['de']);

	$('#end_of_iteration_day').datepick({
		dateFormat: 'dd.mm.yy',
		showOn: 'button',//both
		buttonImageOnly: true,
		buttonImage: 'fileadmin/project/myallergo.de/templates/javascript/jquery.datepick/calendar-green.gif'
	},$.datepick.regional['de']);
	
	$('.date-picker').datepick({
		dateFormat: 'dd.mm.yy',
		showOn: 'button',//both
		buttonImageOnly: true,
		buttonImage: 'fileadmin/project/myallergo.de/templates/javascript/jquery.datepick/calendar-green.gif'
	},$.datepick.regional['de']);
	
	/*========================
	 * calendar datepicker
	 * =========================*/
	$('.time-picker').timePicker();

	/*pretty foto jqeury*/
	function setSaveButton(obj) {
		if ($('button', obj.parent().parent().parent().parent().parent().parent().parent()) != null)
			$('button', obj.parent().parent().parent().parent().parent().parent().parent()).slideDown();
	}

	$("a[rel^='myAllergoVideo1']").prettyPhoto();
	$("a[rel^='myAllergoVideo2']").prettyPhoto();

	/**
	 * Contact permissions.
	 */
	$('.contact-permissions-topic').click(function() {
		value = $(this).attr('checked');
		if (value == true) {
			$('.contact-permissions-list li input', $(this).parent()).each(function() {
				$(this).attr('checked', 'checked');
			});
		} else {
			$('.contact-permissions-list li input', $(this).parent()).each(function() {
				$(this).attr('checked', false);
			});
		}

		// check if a save button is in form fieldset and show it
		setSaveButton($(this));
	});

	$('ul.contact-permissions-list').each(function() {
		$(this).css('display', 'none');
	});

	$('.contact-permissions-open-list').click(function() {
		status = $(this).html();
		if (status == '-') {
			$(this).html('+');
			$('ul.contact-permissions-list', $(this).parent()).css('display', 'none');
		} else {
			$(this).html('-');
			$('ul.contact-permissions-list', $(this).parent()).css('display', 'block');
		}
	});

	$('input.contact-permissions-list-item').click(function() {
		var allChecked = 0;
		$('li', $(this).parent().parent()).each(function() {
			allChecked += $('input', this).attr('checked');
		});

		if (allChecked == $('li', $(this).parent().parent()).length) {
			$('.contact-permissions-topic', $(this).parent().parent().parent()).attr('checked', true);
		} else {
			$('.contact-permissions-topic', $(this).parent().parent().parent()).attr('checked', false);
		}

		// check if a save button is in form fieldset and show it
		setSaveButton($(this));
	});
	
	if ($('ul.contact-permissions').html() != null) {
		$('ul.contact-permissions .contact-permissions-list').each(function() {
			var allChecked = 0;
			$('li', $(this)).each(function() {
				allChecked += $('input', this).attr('checked');
			});

			if (allChecked == $('li', $(this)).length) {
				$('.contact-permissions-topic', $(this).parent()).attr('checked', true);
			}
		});
	}
	
	/**
	 * Intro Login Site Slider.
	 */
	function slide(to) {
		positionCurrentBox = $('.the-box').index($('.the-box.the-current-box'));

		if (to == positionCurrentBox) return;

		steps = (to - positionCurrentBox) * 350;
		$('.box-container').animate({"top": "-="+steps+"px"}, "slow");

		// navigation changes
		$('.the-current-list-item').removeClass('the-current-list-item');
		$('.the-navigation-list li:eq('+to+')').addClass('the-current-list-item');

		// box changes
		$('.the-box.the-current-box').removeClass('the-current-box');
		$('.the-box:eq('+to+')').addClass('the-current-box');
	}

	$('ul.the-navigation-list li').hover(function() {
		positionNavigation = $('.the-navigation-list li').index($(this));
		positionCurrentBox = $('.the-box').index($('.the-box.the-current-box'));

		if (positionNavigation == positionCurrentBox) return;

		steps = (positionNavigation - positionCurrentBox) * 350;
		$('.box-container').animate({"top": "-="+steps+"px"}, "slow");

		// navigation changes
		$('.the-current-list-item').removeClass('the-current-list-item');
		$('.the-navigation-list li:eq('+positionNavigation+')').addClass('the-current-list-item');

		// box changes
		$('.the-box.the-current-box').removeClass('the-current-box');
		$('.the-box:eq('+positionNavigation+')').addClass('the-current-box');
	});
});
