$(document).ready(function () {

/* InnerLabels */
	$("form input.inner-label, form textarea.inner-label").each(function () {
		this.$label = $("label[for="+this.id+"]");
		this.$label.height($(this).height()); 
		this.$label.hide(); 
		$(this).focus(function () {
			this.value = this.value.replace(/^ +| +$/, ''); 
			if (this.value == this.$label.text()) {
				this.value = ''; 
			}
		}); 
		$(this).blur(function () {
			this.value = this.value.replace(/^ +| +$/, ''); 
			if (this.value == '') {
				this.value = this.$label.text(); 
			}
		}).blur(); 
		$(this).parents('form').not('.inner-label-ready').each(function () {
			$(this).addClass('inner-label-ready'); 
			$(this).submit(function () {
				$(this).find("input.inner-label, textarea.inner-label").focus(); 
			}); 
		}) 
	});  

/* FontSizeController */
	var currentFontSizeClass = $.cookie('font-size-class'); 
	if (currentFontSizeClass) $('body').addClass(currentFontSizeClass); 
	var $li = $('<li class="font-size"></li>'); 
	$li.append($('<a class="small"></a>'))
		.append($('<a class="medium"></a>'))
		.append($('<a class="big"></a>')); 
	$li.find('a').click(function () {
		var newFontSizeClass = $(this).attr('class');
		if (currentFontSizeClass) $('body').removeClass(currentFontSizeClass);
		$('body').addClass(newFontSizeClass); 
		$.cookie('font-size-class', newFontSizeClass, { path: '/' }); 
		currentFontSizeClass = newFontSizeClass; 
	});
	$('#language ul').append($li); 

/* Collapsible Sections */
	// Taking care of styles 
	var style = "div.collapsible { margin: 10px 0; padding: 0 0 0 14px; background: url('/system/modules/ro.grapefruit.eon.website/resources/images/lista-exp-bullet.gif') no-repeat 0 .3em; } div.section-content { overflow: hidden; } div.collapsible h4 { cursor: pointer; } div.collapsed { background-image: url('/system/modules/ro.grapefruit.eon.website/resources/images/lista-col-bullet.gif'); }";
	$('head').append($('<style type="text/css">/*<![CDATA[*/ '+style+' /*]]>*/</style>'));
	// Applying classes 
	var $collapsibles = $(".section.collapsible");
	$collapsibles.each(function () {
		$(this).append($('<div class="section-content"></div>').append($(this).find('> *').not('h4'))); 
	});
	$collapsibles.addClass("collapsed"); 
	$collapsibles.find("> .section-content").hide(); 
	$collapsibles.find("> h4").each(function () {
		$(this).click(function (e) {
			var $parent = $(this).parent(".section.collapsible"); 
			$parent.toggleClass("collapsed"); 
			$parent.find('> .section-content').slideToggle();   
		}).hover(function () {
			$(this).css({ textDecoration: 'underline', color: '#F21C0A' }); 
		}, function () {
			$(this).css({ textDecoration: '', color: '' }); 
		}); 
	}); 
	// Expandind the first of each collapsible group
	var anchor = location.href.split('#', 2)[1]; 
	if (anchor) $("a[name="+anchor+"]").parents(".section.collapsible").toggleClass("collapsed").find('> .section-content').show();
	else $collapsibles.parent().find("> .section.collapsible:first > .section-content").show().parent(".section.collapsible").toggleClass("collapsed");

/* Print Version */
	// <p><a class="print">Print Version</a></p>
	$('#footer').prepend($('<p></p>').append($('<a class="print">Print Version</a>').css('cursor', 'pointer').click(function (e) { 
		e.stopPropagation(); 
		var wasDisabled = $('link[title=Print Preview]')[0].disabled; 
		$('link[title=Print Preview]')[0].disabled = false; 
		window.print(); 
		$('link[title=Print Preview]')[0].disabled = wasDisabled; 
		return false; 
	}))); 

/* Opening in new window */
	$('a.open-new-window').click(function (e) {
		e.stopPropagation(); 
		window.open($(this).attr('href'), 'fromeonwebsite'+Math.floor(Math.random()*10000));
		return false; 
	});

/* Zoomable images */
	$('img.zoomable').each(function () {
		$(this).css('cursor', 'url(/system/modules/ro.grapefruit.eon.website/resources/images/zoom-in.cur), pointer');
		this.$clone = $('<img />');
		this.$clone[0].original = this; 
		this.$clone.css('cursor', 'url(/system/modules/ro.grapefruit.eon.website/resources/images/zoom-out.cur), pointer');
		this.$clone.load(function () {
			var $this = $(this.original);
			var $clone = $(this); 
			var width = $this.width(); 
			var height = $this.height(); 
			var newWidth = $clone.width(); 
			var newHeight = $clone.height();
			var viewportWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth; 
			var viewportHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight; 
			$this.addClass('replaced'); 
			$this.css('visibility', 'hidden');
			$clone.css({
					width: width, 
					height: height, 
					display: 'block'
				}).animate({
					left: (viewportWidth - newWidth)/2, 
					top: Math.max(0, (viewportHeight - newHeight)/2), 
					width: newWidth, 
					height: newHeight
				}, 332); 
			$('body').one('click', function (e) {
				e.stopPropagation(); 
				var $original = $this; 
				$clone.animate({
						left: $original.offset().left, 
						top: $original.offset().top, 
						width: $original.width(), 
						height: $original.height()
					}, 332, function () { 
						$original.removeClass('replaced').css('visibility', 'visible'); 
						$clone.hide(); 
					}); 
				return false; 
			}); 
		}); 
	}); 
	$('img.zoomable').click(function (e) {
		e.stopPropagation(); 
		var $this = $(this); 
		var $clone = this.$clone; 
		$('body').append($clone); 
		$clone.addClass('replacement'); 
		$clone.css({
			position: 'absolute',  
			left: $this.offset().left,  
			top: $this.offset().top,  
			width: 'auto', 
			height: 'auto', 
			display: 'none',  
			zIndex: 10
		}); 
		$clone.attr('src', $this.attr('alt')); 
		return false; 
	}); 

/* Cycling images */
	$('img.cycling').each(function () {
		var $this = $(this); 
		this.srcs = $this.attr('src').split(/&next=/);
		if (this.srcs.length < 2) return; 
		this.index = this.srcs.length-1; 
		this.activeFaderIndex = 0; 
		this.otherFaderIndex = 1; 
		this.$fader = $($this.clone().get().concat($this.clone().get())); 
		for (var i = 0; i < this.$fader.size(); i++) this.$fader[i].original = this;
		$('body').append(this.$fader);
		this.$fader.removeClass('cycling').addClass('replacement'); 
		this.$fader.css('position', 'absolute'); 
		this.$fader.css('left', $this.offset().left+'px'); 
		this.$fader.css('top', $this.offset().top+'px');
		$this.addClass('replaced'); 
		$this.css('visibility', 'hidden'); 
		this.next = function () {
			this.index++; 
			if (this.index == this.srcs.length) this.index = 0; 
			this.activeFaderIndex = (this.activeFaderIndex+1)%2; 
			this.otherFaderIndex = (this.activeFaderIndex+1)%2;
			this.$fader.eq(this.otherFaderIndex).css('z-index', 4);
			this.$fader.eq(this.activeFaderIndex).one('load', function () {
				var delay = parseFloat(this.original.srcs[this.original.index].split(/delay=/)[1])*1000; 
				if (isNaN(delay)) delay = 500; 
				$(this).show().css({ opacity: 1 }); 
				this.original.$fader.eq(this.original.otherFaderIndex).fadeOut(1000).animate({ opacity: 0 }, delay, function () {
					this.original.next(); 
				}); 
			}).attr('src', this.srcs[this.index]).css('z-index', 3); 
		};
		this.next(); 
	}); 

/* Textual buttons */
	$('input.textual.button, button.textual').each(function () {
		var label = this.value; 
		if (label == null) label = $(this).text(); 
		var $link = $('<a>'+label+'</a>');
		$link.css('cursor', 'pointer'); 
		$link[0].button = this; 
		$link.click(function () {
			$(this).after(this.button); 
			this.button.click();
			$(this.button).remove(); 
		}); 
		$(this).after($link); 
		$(this).remove(); 
	}); 
 
/* Banner Links */
	(function (clickFunction) {
		$('#right .banner').each(function () {
			var $this = $(this); 
			if ($this.find('ul.link-list').size() > 0) return; 
			$this.attr('style', 'cursor: pointer !important;'); 
			$this.click(clickFunction);
		});
	})(function (e) {
		e.stopPropagation(); 
		var $link = $(this).find('a:first'); 
		if ($link.hasClass('open-new-window')) $(this).find('a:first').click(); 
		else location.href = $(this).find('a:first').attr('href'); 
		return false; 
	}); 

}); 

