$(function () {
	initForms();
	init_DownloadList();
});

function init_DownloadList(){

    $(".download_list li a.download_opener").removeAttr('href');

    $(".download_list li").click( function(){
        $('span.download_info',this).slideToggle(300, function(){
        	if(!$(this).is(':hidden')) $(this).css('display','block');
        });
        $(this).toggleClass('clicked');
    });

    $(".download_list a.download").mouseover( function(){
        $(this).parents('li').eq(0).unbind("click");
    });

    $(".download_list a.download").mouseout( function(){
        $(this).parents('li').eq(0).bind("click", function(){
            $('span.download_info',this).slideToggle(300);
            $(this).toggleClass('clicked');
        });
    });
}

function initFormLabelToDefault() {
	
	var forms = $('form');
	
		forms.submit(function(){
			$(this)
				.find('input[type=text], textarea')
				.each(function(){
					toggleLabel($(this), 'remove')
				});
		})	
		.find('input[type=text], textarea')
		.each(function(){
			
			var $this = $(this);
			toggleLabel($this, 'add');
			
			$this.blur(function(){
				toggleLabel($this, 'add');
			});
			
			$this.focus(function(){
				toggleLabel($this, 'remove');
			});			
		});
		
		forms
			.find('select')
			.each(function(){
				var $this = $(this);
				var first = $this.children().eq(0);
				var label = !$this.is('.required') ? $this.attr('title') : $this.attr('title')+' *';
				
				if (first.text() == '') {
					first.html(label);
				}
				else {
					$this.prepend('<option>'+label+'</option>');
				}
				
				if ($this.children().eq(0).is(':selected')) {
					$this.addClass('default');
				}
				$this.blur(function(){
					if ($this.children().eq(0).is(':selected')) {
						$this.addClass('default');
					}
				});
				
				$this.focus(function(){
					$this.removeClass('default');
				});	
			});
}


function toggleLabel(obj, type){
	var input = $(obj);
	var value = input.val();

	var label = !input.is('.required') ? input.attr('title') : input.attr('title')+' *';
	
	if (type == 'remove' && value == label) {
		input.val('');
	}	
	if (type == 'add' && jQuery.trim(value) == '') {
		input.val(label);
	}	
	
	if (input.val() == label) {
		input.addClass('default');
	}
	else {
		input.removeClass('default');
	}
}

function initForms() {
	$('form label.qflabel').hide();
	initFormLabelToDefault();
}

