$(document).ready(function(){
	
	if($('#vouchers').length > 0){
		$('#vouchers').utiVouchers();
	}
	
	if($('#voucher_timer').length > 0){
		$('#voucher_timer').utiCountDown({
			'year':		2012,
			'month':	03,
			'day':		04,
			'hour':		00,
			'minute':	00,
			'second':	00
		});
	}
	
	$('.voucher #top_menu a').attr('target', '_blank');
	
	$('.voucher #top_logo').attr('href', '/voucher');
	
});


(function($){
	$.fn.utiCountDown  = function(options){
		
		var colonvisible = false;
		
		var settings = {
			
		}
		
		$.extend(settings, options);
		
		var main = $(this);
		var future = new Date(settings['year'], settings['month'] - 1, settings['day'], settings['hour'], settings['minute'], settings['second']);
		
		calculate();
		setInterval(
			function(){
				calculate();
			},
			500
		);
		
		function calculate(){
			var current = new Date();

			var diff = future.getTime() - current.getTime();
			
			var days = diff / (24 * 60 * 60 * 1000);
			var hours = (days % 1) * 24;
			var minutes = (hours % 1) * 60;
			var seconds = (minutes % 1) * 60;
			
			set_countdown(Math.floor(days), Math.floor(hours), Math.floor(minutes), Math.floor(seconds));
		}
		
		function set_countdown(days, hours, minutes, seconds){
			
			if(days < 10){
				var first_d = 0;
				var second_d = days;
			}
			else {
				days = days.toString();
				var first_d = days[0];
				var second_d = days[1];
			}
			
			if(hours < 10){
				var first_h = 0;
				var second_h = hours;
			}
			else {
				hours = hours.toString();
				var first_h = hours[0];
				var second_h = hours[1];
			}
			
			if(minutes < 10){
				var first_m = 0;
				var second_m = minutes;
			}
			else {
				minutes = minutes.toString();
				var first_m = minutes[0];
				var second_m = minutes[1];
			}
			
			if(seconds < 10){
				var first_s = 0;
				var second_s = seconds;
			}
			else {
				seconds = seconds.toString();
				var first_s = seconds[0];
				var second_s = seconds[1];
			}
			
			var days = main.find('.numdays');
			var nums = main.find('.numtime');
			var colons = main.find('.colon');
			
			days.eq(0).attr('class', 'numdays num_' + first_d);
			days.eq(1).attr('class', 'numdays num_' + second_d);
			nums.eq(0).attr('class', 'numtime num_' + first_h);
			nums.eq(1).attr('class', 'numtime num_' + second_h);
			nums.eq(2).attr('class', 'numtime num_' + first_m);
			nums.eq(3).attr('class', 'numtime num_' + second_m);
			nums.eq(4).attr('class', 'numtime num_' + first_s);
			nums.eq(5).attr('class', 'numtime num_' + second_s);
			
			colons.css('visibility', (colonvisible = !colonvisible) ? 'visible' : 'hidden');
		}
		
	};
})(jQuery);

(function($){
	$.fn.utiVouchers  = function(options){
		
		var settings = {
			
		}
		
		$.extend(settings, options);
		
		var main = $(this);
		var links = main.find('.box > .box_content');
		var mores = main.find('.box .voucher_box_more');
		var hover = main.find('.hover');
		
		var reftimeout = null;
		var refconfig = {
			'first':	80,	// poczta
			'second':	81,	// backup
			'third':	79,	// backup
			'fourth':	82,	// backup
			'fifth':	83	// domena
		}
		
		set('third');
		
		links.click(function(event){
			event.preventDefault();
			var el_class = $(this).attr('rel');
			change(el_class);
		});
		
		mores.click(function(event){
			event.stopPropagation();
		});
		
		var refresh = function() {
			$.ajax({
				url: 'http://cp-admin.uti.pl/VoucherCounter/counter.php',
				dataType: 'jsonp',
				success: function(data) {
					if (typeof(data) != 'object')
						return;
					for (plan in refconfig) {
						var digits = $('.box.' + plan + ' .num');
						var value = numpad(data[refconfig[plan]], digits.length);
						for (var i = 0; i < digits.length; i++) {
							$(digits[i]).attr('class', 'num num_' + value[i]);
						}
					}
				},
				complete: function() {
					setTimeout(refresh, 3000);
				}
			});
		}
		refresh();
		
		function numpad(number, length) {
			var str = '' + number;
			while (str.length < length) {
				str = '0' + str;
			}
			return str;
		}
		
		function set(el_class){
			var el = main.find('.box.' + el_class).eq(0);
			
			el.addClass('active');
			el.css('width', '282px');
			var position = el.position().left-8;
			hover.css('left', position);
			
			$('.voucher_hover_content').css('display', 'none');
			$('.voucher_hover_content.' + el_class).eq(0).css('display', 'block');
		}
		
		function change(el_class){
			var el = main.find('.box.' + el_class).eq(0);
			var previous_el = main.find('.box.active').eq(0);
			
			el.animate({
				'width': '282px'
			});
			
			previous_el.animate({
				'width': '222px'
			});
			
			el.addClass('active');
			previous_el.removeClass('active');
			
			var current_position = previous_el.position().left
			var new_position = el.position().left;
			
			if(current_position > new_position){
				var position = el.position().left-8;
			}
			else {
				var position = el.position().left-68;
			}
			
			
//			console.log(elcontent);
			
			hover.animate({
				'left': position
			});
			
//			hover.children().empty();
//			hover.append(el.find('.voucher_box_more, .voucher_box_order').clone());
			
			$('.voucher_hover_content').fadeOut();
			
			var content = $('.voucher_hover_content.' + el_class).eq(0).fadeIn();
			content.children().empty();
		}
		
	};
})(jQuery);
