
var Site = {

	StartFunctions: [],
	siteId: '',
	// ======================================================================
	getSiteId: function() {
		return document.body.id ? document.body.id : 'default';
	},
		
	// ======================================================================
	start: function() {
		if (this.getSiteId() != 'admin-site') {
			this.tweakLayout();
			this.tweakButtons();
			this.startRotator();
			this.addFuncionalities();
		}
		this.RunStartFunctions();
		this.RunPage(this.getSiteId());
		
		// news
		$('#news-world-select').change(function(){
			Site.fetchNews();
		});
		this.fetchNews();
		
		// news adder
		$('#news-world-select-add').change(function(){
			val = $(this).val();
			window.location = 'http://forum.hunted.pl/newthread.php?do=newthread&f=' + val;
		});		
		
		// poll
		var pollResults = 0;
		$('#poll-results').click(function(){
			if (pollResults) {
				$('#poll .anserw-item').show();
				$('#poll .result-item').hide();
				pollResults = 0;
			} else {
				$('#poll .anserw-item').hide();
				$('#poll .result-item').show();
				pollResults = 1;
			}
			return false;
		});
		
		// content images
		$('#content-body img').each(function(){
			w = $(this).width();
			if (w > 610) {
				hp = $(this).height() / w;
				$(this).width(580);
				$(this).height(580 * hp);
				$(this).wrap('<a href="#" class="fancy-img" />');
			}
		});
		var I = 10;
		$('a.fancy-img').each(function(){
			src = $(this).find('img').attr('src');
			id = "img-" + I;
			++I;
			$el = $('<div style="display: none;" id="' + id + '"><img src="' + src + '" alt="" /></div>');
			$(this).attr('href', '#' + id);
			$(this).append($el);
			$(this).fancyZoom({ directory: 'http://hunted.pl/img/fancy-zoom', closeOnClick: true });
		});
	},
		
	// ======================================================================
	addStartFunction: function(f) {
		this.StartFunctions.push({
			f: f,
			started: false
		});
	},
		
	// ======================================================================
	RunStartFunctions: function() {
		for(var I in this.StartFunctions) {
			if (!this.StartFunctions[I].started) {
				this.StartFunctions[I].started = true;
				this.StartFunctions[I].f();
			}
		}
	},
		
	// ======================================================================
	RunPage: function(siteId) {
		switch(siteId) {
		
			case 'default':
			
			break;
		
		
		
		
		}
	},
	
	// ======================================================================
	fetchNews: function() {
		var url='/pobierz-newsy/' + $('#news-world-select').val();
		$('#news-list-out').html('<div style="text-align: center;"><img src="img/loader-bar.gif" alt="" /></div>');
		$.ajax({
			url: url,
			cache: false,
			success: function(html) {
				$('#news-list-out').html(html);
			}
		});
	},
	
	// ########################################################################
	tweakLayout: function() {
		$('html').addClass('js');
		
		// Sign inputs for IE
		if ($.browser.msie) {
			$('input').each(function() {
				if ($(this).attr('type') == 'text') {
					$(this).addClass('text');
				}
				else if ($(this).attr('type') == 'password') {
					$(this).addClass('password');
				}
				else if ($(this).attr('type') == 'checkbox') {
					$(this).addClass('checkbox');
				}
				else if ($(this).attr('type') == 'radio') {
					$(this).addClass('radio');
				}
			});
		}
		
		//Unify height in questions
		$('#questionare ol').each(function() {
			var $this = $(this);
			var $p = $this.find('> li > p');
			var $ul = $this.find('> li > ul');
			var height = 0;
			$p.each(function() {
				if ($(this).height() > height) {
					height = $(this).height();
				}
			});
			$p.height(height);
			height = 0;
			$ul.each(function() {
				if ($(this).height() > height) {
					height = $(this).height();
				}
			});
			$ul.height(height);
		});
	},
	
	tweakButtons: function() {
		$('input.button').each(function() {
			var $submit = $(this);
			var classes = this.className;
			var val = $submit.attr('value');
			var alt = $submit.attr('alt');
			if (alt != null) {
				$submit.after('<a class="' + classes + '" href="#"><span class="btn-l">' + val + '</span><span class="btn-r"></span><span class="ico ' + alt + '"></span></a>');
			}
			else {
				$submit.after('<a class="' + classes + '" href="#"><span class="btn-l">' + val + '</span><span class="btn-r"></span></a>');
			}
			$submit.next().click(function() {
				$submit.click();
				return false;
			});
			$submit.hide();
		});
		$('input.te-search-button').each(function() {
			var $submit = $(this);
			var classes = this.className;
			$submit.after('<a class="' + classes + '" href="#">Szukaj</a>');
			$submit.next().click(function() {
				$submit.click();
				return false;
			});
			$submit.hide();
		});
		$('input.shout-search-button').each(function() {
			var $submit = $(this);
			var classes = this.className;
			$submit.after('<a class="' + classes + '" href="#">Wyślij</a>');
			$submit.next().click(function() {
				$submit.click();
				return false;
			});
			$submit.hide();
		});
		$('input.login-button').each(function() {
			var $submit = $(this);
			var classes = this.className;
			$submit.after('<a class="' + classes + '" href="#">Zaloguj</a>');
			$submit.next().click(function() {
				$submit.click();
				return false;
			});
			$submit.hide();
		});
		$('input.search-button').each(function() {
			var $submit = $(this);
			var classes = this.className;
			$submit.after('<a class="' + classes + '" href="#">Szukaj</a>');
			$submit.next().click(function() {
				$submit.click();
				return false;
			});
			$submit.hide();
		});
	},
	
	startRotator: function() {
		$('#rotator').each(function() {
			var $rot = $(this);
			var $rot_panes = $rot.find('#rot-panes');
			var $panes = $rot.find('#rot-panes .pane');
			$panes.hide();
			$rot_panes.find('.pane:first').show();
			var tabs_html = '<ul id="rot-tabs">';
			$panes.each(function(i) {
				var $pane = $(this);
				$pane.attr('id', 'rot-pane' + ++i);
				tabs_html += '<li id="rot-tab' + i + '"><a href="#">' + i + '</a></li>';
			});
			tabs_html += '</ul>';
			$rot_panes.after(tabs_html);
			var $tabs = $rot.find('#rot-tabs');
			$tabs.find('li:first').addClass('selected');
			$tabs.find('li').each(function(i) {
				var $li = $(this);
				$li.attr('id', 'rot-tab' + ++i)
				$li.find('a').click(function() {
					$tabs.find('.selected').removeClass('selected');
					$li.addClass('selected');
					var id = $li.attr('id').replace('rot-tab', '');
					$panes.hide();
					$rot_panes.find('#rot-pane' + id).show();
					return false;
				});
			});
		});
		
		$('#small-rotator').each(function() {
			var $rot = $(this);
			var $rot_panes = $rot.find('.panes');
			var $panes = $rot.find('.panes .pane');
			$panes.hide();
			$rot_panes.find('.pane:first').show();
			var tabs_html = '<ul id="small-rot-tabs">';
			var captions = new Array();
			$panes.each(function(i) {
				var $pane = $(this);
				captions[i] = $pane.find('img:first').attr('alt');
				$pane.attr('id', 'rot-pane' + ++i);
				tabs_html += '<li id="small-rot-tab' + i + '"><a href="#">' + i + '</a></li>';
			});
			tabs_html += '</ul>';
			$rot_panes.after(tabs_html);
			$rot_panes.after('<div class="caption"></div>');
			var $tabs = $rot.find('#small-rot-tabs');
			var $captions = $rot.find('.caption');
			$captions.html('Następnie <strong>' + captions[1] + '</strong>');
			$tabs.find('li:first').addClass('selected');
			var rotInterval;
			$tabs.find('li').each(function(i) {
				var $li = $(this);
				$li.attr('id', 'rot-tab' + (i+1))
				$li.find('a').click(function() {
					clearInterval(rotInterval);
					$tabs.find('.selected').removeClass('selected');
					$li.addClass('selected');
					var id = $li.attr('id').replace('rot-tab', '');
					$panes.hide();
					$rot_panes.find('#small-rot-pane' + id).show();
					$rot_panes.find('.pane:eq(' + i + ')').show();
					if (i == captions.length - 1) {
						$captions.html('Następnie <strong>' + captions[0] + '</strong>');
					}
					else {
						$captions.html('Następnie <strong>' + captions[i+1] + '</strong>');
					}
					startRotating();
					return false;
				});
			});

			function startRotating() {
				rotInterval = setInterval(function() {
					if ($tabs.find('li.selected').nextAll().length > 0) {
						$tabs.find('li.selected').next().find('a').click();
					}
					else {
						$tabs.find('li:first a').click();
					}
				}, 5000);
			}
			
			startRotating();
		});
	},
	
	addFuncionalities: function() {
		//adding _blank
		$('a.blank').attr('target', '_blank');
		
		//captions to te-search
		$('#tibian-earth .field input').each(function() {
			var caption = 'wyszukaj w tibian earth';
			var $this = $(this);
			if ($this.val() == '' || $this.val() == caption) {
				$this.addClass('pre').val(caption);
				$this.focus(function() {
					$this.val('').unbind('focus').removeClass('pre');
				});
			}
		});
		
		//captions to shoutbox-search
		$('#shoutbox .field input').each(function() {
			var caption = 'nick';
			var $this = $(this);
			if ($this.val() == '' || $this.val() == caption) {
				$this.addClass('pre').val(caption);
				$this.focus(function() {
					$this.val('').unbind('focus').removeClass('pre');
				});
			}
		});
		$('#shoutbox .texta textarea').each(function() {
			var caption = 'wiadomość';
			var $this = $(this);
			if ($this.val() == '' || $this.val() == caption) {
				$this.addClass('pre').val(caption);
				$this.focus(function() {
					$this.val('').unbind('focus').removeClass('pre');
				});
			}
		});
		$('#user-box .field.login input').each(function() {
			var caption = 'login';
			var $this = $(this);
			if ($this.val() == '' || $this.val() == caption) {
				$this.addClass('pre').val(caption);
				$this.focus(function() {
					$this.val('').unbind('focus').removeClass('pre');
				});
			}
		});
		$('#user-box .field.pass input').each(function() {
			var caption = 'hasło';
			var $this = $(this);
			if ($this.val() == '' || $this.val() == caption) {
				$this.hide().before('<input type="text" value="hasło" class="pre" />');
				$this.prev().focus(function() {
					$(this).remove();
					$this.show().focus();
				});
			}
		});
		
		//login box features
		$('#login-box .key').click(function() {
			$('#login-box').hide();
			$('#user-box').show();
			return false;
		});
		$('#user-box .key').click(function() {
			$('#user-box').hide();
			$('#login-box').show();
			return false;
		});
	}	
	

}

$(function() {
	Site.start();
})

