$(document).ready(function(){
	
	var theme_url = '/wp-content/themes/uti/';
	
	$('#top_menu').utiTopMenu();
	$('#top_content_tabs').utiTabs();
	uti_signup();
	
	if($('.tab_mail').length > 0){
		$('.slider').utiSlider();
	}
	
	$('#home_bottom_content .height_wrapper').equalHeights();
	
	$('#home_domain_box input').click(function(event) {
		switch($(this).attr('type')) {
			case 'submit':
				$(this).parents('form').submit();
				break;
		}
		event.stopPropagation();
		return false;
	});

	$('.form-valuation #submit').click(function() {
//		console.log(this, $(this).parents('form'));
        $(this).parents('form').submit();
	});

        
	$("#kayako_sitebadgebg.off").live('mouseenter', function(event)
        {
                event.stopPropagation();
                $(".kayako_sitebadgebg").animate({'width':120}, 200);
                $('.txt1', this).fadeOut(100);
                $('.txt2', this).fadeIn(100);	
                $(this).removeClass('off');
                $(this).addClass('on');
	});

	$("#kayako_sitebadgebg.on").live('mouseleave', function(event)
        {
                event.stopPropagation();                
                $(".kayako_sitebadgebg").animate({'width': 40}, 200, function() {
                    $(this).removeClass('on');
                    $(this).addClass('off');
                });

                $('.txt2', this).fadeOut(100);
                $('.txt1', this).fadeIn(100);	
	});
 
	$('.gallery-item a').lightBox({
		'imageBtnPrev': theme_url + 'images/lightbox-btn-prev.gif',
		'imageBtnNext': theme_url + 'images/lightbox-btn-next.gif',
		'imageLoading': theme_url+ 'images/lightbox-ico-loading.gif',
		'imageBtnClose': theme_url + 'images/lightbox-btn-close.gif' 
	});
	
	$('#content .facebook a').click(function(event) {
		event.stopPropagation();
		event.preventDefault();
		var fb = $(this).parent('.facebook');
		var hl = fb.children('.facebook_holder');
		if (!fb.hasClass('open')) {
			hl.show();
			fb.animate({
				width: 310
			}, 200, function() {
				fb.addClass('open');
			});
		} else {
			fb.removeClass('open');
			fb.animate({
				width: 0
			}, 200, function() {
				hl.hide();
			});
		}
	});
});	

(function($){
	$.fn.utiTopMenu = function(options){
		
		var settings = {
			
		}
		
		$.extend(settings, options);
		
		var main = $(this);
		attach();
		
		function attach(){
			main.find('li').each(function(index, el){
				if($(el).parent().attr('id') == 'top_menu' && $(el).find('.sub-menu').length > 0){
					$(el).mouseover(function(){
						$(this).find('a').eq(0).css('background-image', 'url(/wp-content/themes/uti/img/submenu_top_arrow.png)');
						$(this).find('.sub-menu').eq(0).css('display', 'block');
					});
					
					$(el).mouseout(function(){
						$(this).find('a').eq(0).css('background-image', 'none');
						$(this).find('.sub-menu').eq(0).css('display', 'none');
					});
				}
			});
		}
		
	};
})(jQuery);


(function($){
	$.fn.utiSlider = function(options){
		
		var settings = {
			
		}
		
		$.extend(settings, options);
		
		var elements = $(this).find('li').not('.hover');
		var main = $(this);
		var hover = $(this).find('.hover');
		change(elements[0]);
		attach();
		
		function attach(){
			elements.each(function(index, el){
				$(el).click(function(){
					change(this);
				});
			});
		}
		
		function change(el){
			el = $(el);
			if(el.hasClass('one_digit')){
				var to_add = -5;
			}
			else {
				var to_add = -0;
			}
			
			hover.animate({
				'left': el.position().left + to_add
			});
			
			// find tab
			var tab = el.parents('.tab');
			
			// price
			var new_price = el.find('.price').html();
			var big_price = new_price.split(',')[0];
			var small_price = new_price.split(',')[1];
			tab.find('.boxes_smaller .prize_big').html(big_price + ',');
			tab.find('.boxes_smaller .prize_small').html(small_price);
			
			// descr
			tab.find('form .descr').html(el.find('.descr').html());
			
			// href
			tab.find('form .trial').unbind('click').bind('click', function(event) {
				event.stopPropagation();
				event.preventDefault();
				eval(el.find('.trial').html());
			});
			tab.find('form .order').unbind('click').bind('click', function(event) {
				event.stopPropagation();
				event.preventDefault();
				eval(el.find('.order').html());
			});
		}
		
	};
})(jQuery);


(function($){
	$.fn.utiTabs = function(options){
		
		var settings = {
			
		}
		
		$.extend(settings, options);
		
		var elements = $(this).find('li');
		var main = $(this);
		
		create_structure(elements);
		attach();
		
		function create_structure(){
			var is_above = false;
			
			var above_3_elements = new Array();
			
			elements.each(function(index, el){
				if(index > 2){
					is_above = true;
					above_3_elements.push(el);
				}
			});
			
			$(above_3_elements).each(function(index, el){
				$(el).remove();
			});
			
			var more ='';
			
			if(is_above){
				more = '<li class="more"><a class="more_tab" href="#"><img src="/wp-content/themes/uti/img/top_content_tabs_more.png" alt=""/></a><ul>';
				$(above_3_elements).each(function(index, el){
					more += '<li>' + el.innerHTML + '</li>';
				});
				
				more += '</ul></li>';
			}
			
			var old_html = main.html();
			main.html(old_html + more);
		}
		
		function attach(){
			$('.more').mouseover(function(){
				$(this).find('ul').css('display', 'block');
			});
			
			$('.more').mouseout(function(){
				$(this).find('ul').css('display', 'none');
			});
		}	
		
	};
})(jQuery);

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","auto");
		});
	}
})(jQuery);

function uti_signup() {
	$('#newsletter_signup').submit(function(){
		if($(this).data('formstatus') !== 'submitting'){
			var form = $(this),
				formData = form.serialize(),
				formUrl = form.attr('action'),
				formMethod = form.attr('method'), 
				responseMsg = $('#signup_response');

			form.data('formstatus','submitting');

			responseMsg.hide()
					   .addClass('response-waiting')
					   .text('Proszę czekać...')
					   .fadeIn(200);

			$.ajax({
				url: formUrl,
				type: formMethod,
				data: formData,
				success:function(data){

					var responseData = jQuery.parseJSON(data), 
						klass = '';

					switch(responseData.status){
						case 'error':
							klass = 'response-error';
						break;
						case 'success':
							klass = 'response-success';
						break;	
					}

					responseMsg.fadeOut(200,function(){
						$(this).removeClass('response-waiting')
							   .addClass(klass)
							   .text(responseData.message)
							   .fadeIn(200,function(){
								   setTimeout(function(){
									   responseMsg.fadeOut(200,function(){
									       $(this).removeClass(klass);
										   form.data('formstatus','idle');
									   });
								   },3000)
								});
					});
				}
			});
		}

		return false;
	});
}

$(document).ready(function() {
	var weboferto = $('.weboferto');
	if (weboferto.length == 0)
		return;
	$.ajax({
		url			: 'https://store1.uti.pl/api/json.php',
		dataType	: 'jsonp',
		data		: 'data='+JSON.stringify({
			clientdataget	: {action: 'clientdataget'}
		}),
		success		: function(result) {
			if (typeof (result.clientdataget) == 'object') {
				$('.weboferto #clientId').val(result.clientdataget.account_list[0].account_id);
				$('.weboferto #clientLogged span').html(result.clientdataget.first_name + ' ' + result.clientdataget.last_name);
				$('.weboferto #clientLogged').show();
				$('.weboferto #clientNotLogged, .weboferto #clientLoginForm').hide();
			} else {
				$('.weboferto #clientId').val('');
				$('.weboferto #clientNotLogged').show();
				$('.weboferto #clientLogged, .weboferto #clientLoginForm').hide();
			}
		},
		error		: function() {
			$('.weboferto #clientNotLogged').show();
			$('.weboferto #clientLogged, .weboferto #clientLoginForm').hide();
		}
	});
	$('.weboferto #clientLogin, .weboferto #clientReLogin').click(function() {
		$('.weboferto #userlogin, .weboferto #userpassw').val('');
		$('.weboferto #clientLoginForm').slideToggle('fast');
	});
	$('.weboferto #clientRegister').click(function() {
		window.open('https://store1.uti.pl/store/sign_in.php');
	});
	$('.weboferto #clientLoginSubmit').click(function() {
		var userLogin = $('.weboferto #userlogin').val();
		var userPassw = $('.weboferto #userpassw').val();
		$.ajax({
			url			: 'https://store1.uti.pl/api/json.php',
			dataType	: 'jsonp',
			data		: 'data='+JSON.stringify({
				clientlogout	: {action: 'clientlogout'},
				clientlogin		: {action: 'clientlogin', data: {login_email: userLogin, login_password: userPassw}},
				clientdataget	: {action: 'clientdataget'}
			}),
			success		: function(result) {
				if (typeof (result.clientdataget) == 'object') {
					$('.weboferto #clientId').val(result.clientdataget.account_list[0].account_id);
					$('.weboferto #clientLogged span').html(result.clientdataget.first_name + ' ' + result.clientdataget.last_name);
					$('.weboferto #clientLogged').slideDown();
					$('.weboferto #clientNotLogged, .weboferto #clientLoginForm').slideUp();
				} else {
					$('.weboferto #clientId').val('');
					$('.weboferto #clientNotLogged').slideDown();
					$('.weboferto #clientLogged, .weboferto #clientLoginForm').slideUp();
				}
			},
			error		: function() {
				$('.weboferto #clientNotLogged').slideDown();
				$('.weboferto #clientLogged, .weboferto #clientLoginForm').slideUp();
			}
		});
	});
	$('.weboferto_order .wo_content form').submit(function() {
		if ($('.weboferto #clientId').val() == '') {
			alert('Zaloguj się na swoje konto w UTI przed wysłaniem formularza zapytania');
			return false;
		}
	}); 
});
