
var Site = {

	StartFunctions: [],
		
	// ======================================================================
	start: function() {
		this.PrepareModals();
		this.HideRedirectMessage();
		this.tweakLayout();
		this.tweakButtons();
		this.counterStart();
		this.RunStartFunctions();
		this.RunPage(document.body.id ? document.body.id : 'default');
		this.widgetFunctionalities();
	},
		
	// ======================================================================
	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();
			}
		}
	},
			
	// ======================================================================
	PrepareModals: function() {
		$('a.ajax-link').each(function() {
			var $this = $(this);
			if (!document.getElementById('widget')) {
				$this.click(function(){
					var href = $(this).attr('href');
					if (href.indexOf('?') != -1) href += "&"; else href += "?";
					href += "ajax=1";
					
					$.ajax({
						url: href,
						cache: false,
						success: function(html) {
							$.modal(html);
						}
					});
					
					return false;
				});
			}
			else {
				$this.attr('target', '_blank');
			}
		})
		
	},
	
	// ======================================================================
	HideRedirectMessage: function() {
		if ($('#redirect-message')) {
			setTimeout(function() {
				$('#redirect-message').fadeOut();
			},3000);
		}	
	},
		
	// ======================================================================
	RunPage: function(siteId) {
		switch(siteId) {
		
			case 'default':
			
			break;
		
		
		}
	},
		
	// ======================================================================
	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');
			$submit.after('<a class="' + classes + '" href="#">' + val + '</a>');
			$submit.next().click(function() {
				$submit.click();
				return false;
			});
			$submit.hide();
		});
	},
	
	// ======================================================================
	counterStart: function() {
		$('#counter').each(function() {
			var $counter = $(this);
			var allsec = parseInt($('#counter-sec').html());
			
			var days = parseInt(allsec / (3600*24), 10);
			var hours = parseInt((allsec - days*3600*24) / 3600, 10);
			var minutes = parseInt((allsec - days*3600*24 - hours*3600) / 60, 10);
			var seconds = parseInt((allsec - days*3600*24 - hours*3600 - minutes*60) / 60, 10);
			var f = function() {
				if (seconds < 0) {
					minutes--;
					if (minutes < 0) {
					    minutes = 0;
					    hours--;
					    if (hours < 0) {
						hours = 0;
						days--;
					    }
						minutes = 59;
					}
					seconds = 59;
				}
				
				
				$counter.html(days + 'd ' + hours + 'h ' + minutes + ':' + (seconds <= 9 ? '0' + seconds : seconds));
				
				
				seconds--;
			};
			if (allsec < 0) {
			    $counter.text('Edycja zakonczona');
			} else {
				f();
			    var timeInterval = setInterval(f, 1000);
			}
		});

		$('#counter-inc').each(function() {
			var $counter = $(this);			
			var minutes = 0;
			var seconds = 0;
			var f = function() {
				if (seconds > 59) {
					minutes++;
					seconds = 0;
				}
				
				$counter.text(minutes + ':' + (seconds < 9 ? '0' + seconds : seconds));
				seconds++;
			};
			f();
			var timeInterval = setInterval(f, 1000);
		});


	},

	widgetFunctionalities: function() {
		if (document.getElementById('widget')) {
			$('#regulation-link').attr('target', '_blank');
			var scroll = 180;
			if ($('#widget').hasClass('horizontal')) {
				scroll = 590;
				if ($.browser.msie && /MSIE 6.0/.test(navigator.userAgent)) {
					scroll += 22;
				}
			}
			function scrollTo(pos) {
				$('#questionare ol:first').animate({'left': -scroll*pos + 'px'}, 300);
			}
			
			$('form#questionare').each(function() {
				var current = 1;
				var l = 195;
				var $form = $(this);
				$form.find('li').removeClass('first-in-line');
				$form.find('.submit-questionare a.button').unbind('click');
				$form.find('.submit-questionare a.button').click(function() {
					$form.find('li:eq(' + current + ')').each(function() {
						var $li = $(this);
						
	
					});
					if (current != 3) {
						scrollTo(current++);
					}
					else {
						$form.submit();
					}
					return false;
				});
			});
		}
	}

}

$(function() {
	Site.start();
})


