/**
 * @author Giedrius Dubinskas <giedrius.dubinskas@metasite.net>
 *			Arturas Paleicikas <arturas.paleicikas@metasite.net>
 */

var Fold = {
	TAG: 'h3',
	FOLD_ALL_ELEMENT_ID: 'foldAllLink', 
	CLASSES: {
			folded:		'switch',
			expanded:	'switch_expanded'
		},
	CONTENT_CLASSES: {
			folded:     'folded',
			expanded:   'expanded'
		},
	TRANSLATIONS: {
			lt: ['Išskleisti viską', 'Suskleisti viską'],
			en: ['View all', 'Hide all'],
			lv: ['Parādīt visu', 'Paslēpt visu'],
			ru: ['Показать все', 'Спрятать все']
		},

	foldAll: function(){
		var $Fo = this;
		$(".switch_expanded").removeClass('switch_expanded').addClass('switch');
		$(".expanded").removeClass("expanded").addClass("folded");
		$Fo.construct(null);
	},

	expandAll: function(){
		var $Fo = this;

		$(".switch").removeClass('switch').addClass('switch_expanded');
		$(".folded").removeClass("folded").addClass("expanded");
		$Fo.construct(null);
	},

	init: function(sources){
		this.sources = sources;
		var $Fo = this;

		$($Fo.TAG+ '.' +$Fo.CLASSES.folded +','+ $Fo.TAG+ '.' +$Fo.CLASSES.expanded)
		//$([$Fo.TAG, $Fo.CLASSES.folded].join('.') + ':first')
			.before('<div id="'+ $Fo.FOLD_ALL_ELEMENT_ID +'"></div>');
	},
	
	construct: function(hash){
		var $Fo = this;
		$Fo.STATE = 0;
		$Fo.TOTAL_ELEMS = 0;
		$Fo.TOTAL_FOLDED_ELEMS = 0;
		$Fo.TOTAL_EXPANDED_ELEMS = 0;
		
		var c = 1;
		var refold	= function(i, element){
			var $obj = $(element);
			var tagName = element.tagName.toLowerCase();
			var className = element.className.toLowerCase();

			if($Fo.TAG == tagName){
				//element.id = 'q'+c++;

				switch(className){				
					case $Fo.CLASSES.folded:
						if(hash && (hash.length > 0) && (element.id && element.id == hash)){
							$obj.attr('class', $Fo.CLASSES.expanded);	
							$.scrollTo('#'+ element.id, 800);
							setTimeout(function(){$Fo.construct(null)}, 1);
						}

						$obj
							.unbind('click')
							.click(function(){
								$obj.addClass($Fo.CLASSES.expanded).removeClass($Fo.CLASSES.folded);						
								setTimeout(function(){$Fo.construct(null)}, 1);
							});
						
						//bind
						$Fo.STATE = 1;
						$Fo.TOTAL_ELEMS++;
						$Fo.TOTAL_FOLDED_ELEMS++;
					break;

					case $Fo.CLASSES.expanded:
						$obj
							.unbind('click')
							.click(function(){
								$obj.addClass($Fo.CLASSES.folded).removeClass($Fo.CLASSES.expanded);						
								setTimeout(function(){$Fo.construct(null)}, 1);
							});

						// bind
						$Fo.STATE = 2;
						$Fo.TOTAL_ELEMS++;
						$Fo.TOTAL_EXPANDED_ELEMS++;
					break;
				}
			}
			else
			{
				switch($Fo.STATE){
					case 1:
					$obj.addClass($Fo.CONTENT_CLASSES.folded).removeClass($Fo.CONTENT_CLASSES.expanded);
					break;

					case 2:
					$obj.addClass($Fo.CONTENT_CLASSES.expanded).removeClass($Fo.CONTENT_CLASSES.folded);
					break;
				}
			}
			
			if($Fo.TOTAL_ELEMS > 0){
				var $foldAll = $('#'+ $Fo.FOLD_ALL_ELEMENT_ID);
					
				$foldAll.empty();
				if ($Fo.TOTAL_FOLDED_ELEMS == $Fo.TOTAL_ELEMS) {
					$foldAll
						.append('<a href="javascript:;" onclick="window.Fold.expandAll();return false;" class="down">'+ $Fo.TRANSLATIONS[lang][0] +'</a>');
				
				} else if($Fo.TOTAL_EXPANDED_ELEMS == $Fo.TOTAL_ELEMS){
					$foldAll
						.append('<a href="javascript:;" onclick="window.Fold.foldAll();return false;" class="up">'+ $Fo.TRANSLATIONS[lang][1] +'</a>');
				
				} else {
					$foldAll
						.append('<a href="javascript:;" onclick="window.Fold.expandAll();return false;" class="down">'+ $Fo.TRANSLATIONS[lang][0] +'</a>');
				}
			}
		}
		
		// folding all binded elements
		$.each($Fo.sources, function(i,element){
			$(element+ ' > *').each(refold);
		});	
	}
};

var TableFold = {
	E: {'opened':'tableFoldOpen', 'closed': 'tableFoldClosed', 'ignore':'tableFoldIgnore'},
	
	construct: function(sources){
		var $TFo = this;
		var TOTAL_HEADS = 0;
		var STATE = 0;

		var refold = function(i, element){
			var $obj = $(element);
			var className = element.className || null;

			switch(className)
			{
				// opened
				case $TFo.E.opened:
				$obj
					.unbind('click')
					.click(function(){
						$obj.removeClass($TFo.E.opened);
						$obj.addClass($TFo.E.closed); 
						setTimeout(function(){$TFo.construct(sources)}, 1);
						return false;
					});
				
				STATE = 1;
				TOTAL_HEADS++;
				break;

				// closed
				case $TFo.E.closed:
				$obj
					.unbind('click')
					.click(function(){
						$obj.removeClass($TFo.E.closed);
						$obj.addClass($TFo.E.opened); 
						setTimeout(function(){$TFo.construct(sources)}, 1);
						return false;
					});

				STATE = 2;
				TOTAL_HEADS++;
				break;
				
				// ignored
				case $TFo.E.ignore:
				break;

				default:
				switch(STATE)
				{
					case 0:
					case 2:
					if(TOTAL_HEADS > 0)
						$obj.hide();
					break;

					case 1: 
					$obj.show();
					break;
				}
			}
		};

		$(sources)
			.each(function(i,element){
				$(element + ' tbody > *').each(refold);
			});
	}
};

$(document).ready(function(){
	// SIMPLE FOLDING
	var hash = window.location.hash.replace(/^#/, '');
	var $Fo = window.Fold;

	// elements to fold
	$Fo.init(['#content', '#content div.subFold', 'form.countryListForm'], 'h3');
	$Fo.construct(hash, false);

	// TABLE FOLDING
	var $TFo = window.TableFold;
	
	// use only tables 
	$TFo.construct(['table.tableFold']);
});