var bubbleTimeout1 = null;
var bubbleTimeout2 = null;
var bubbleTimeout3 = null;
var bubbleTimeout4 = null;

function debug(value) {
	try {
		console.info(value);
	} catch (ex) {}
}

$(document).ready(function() {
        /*LAYER ACTIVATE*/
	$('#layerActivate').hide();
	$('#layerActivate a.iconClose').click(function() {
		$('#layerActivate').fadeOut();
		$('.bodyMobil #promo a.btn').removeClass('btnActive');
		return false;
	});
	$('.bodyMobil #promo a.btn,#layerActivateIn').click(function() {
		$('#layerActivate').fadeIn();
		$('.bodyMobil #promo a.btn').addClass('btnActive');
		if (typeof pT != "undefined") {pT._trackPageview("/index-mobil-jak-aktivovat");}
		return false;
	});



        /*LAYER PHONE*/
        $('.layerPhone').hide();
        $('.box535122GreenB .btn13830White').click(function() {
                $('.layerPhoneA').fadeOut('slow');
		$('.layerPhoneB').fadeIn('slow');
		$('#content').addClass('height615');
		$('html, body').animate({scrollTop:800}, 'slow');
		if (typeof pT != "undefined") {pT._trackPageview("/index-mobil-vice-chytry");}
		return false;
	});
        $('.box535122GreenA .btn13830White').click(function() {
                $('.layerPhoneB').fadeOut('slow');
		$('.layerPhoneA').fadeIn('slow');
		$('#content').addClass('height480');
		$('html, body').animate({scrollTop:800}, 'slow');
		if (typeof pT != "undefined") {pT._trackPageview("/index-mobil-vice-telefon");}
		return false;
	});
        $('.layerPhone .linkClose').click(function() {
		$('.layerPhoneA, .layerPhoneB').fadeOut('slow');
		$('#content').removeClass('height480');
		$('#content').removeClass('height615');
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});

        /*FAQ*/
        var showText="Rozbalit";
        var hideText="Sbalit";
        $("#faq .item1Link").append('<span class="text">'+showText+'</span>');
        $('#faq .item1 ul').hide();
        $('#faq .item2 .answer').hide();
        $('#faq .item1 a.item1Link').click(function() {
                $(this).next('#faq  .item1 ul').toggle('fast');
                $(this).toggleClass('opened');
                if ($(this).children('.text').html()==showText) {
                        $(this).children('.text').html(hideText);
                }
                else {
                        $(this).children('.text').html(showText);
                }
                return false;
        });
        $('#faq .item1 a.item2Link').click(function() {
                $(this).next('#faq  .item2 .answer').toggle('fast');
                $(this).toggleClass('opened');
                return false;
        });

        /*FORM BOX*/
        $('#formBoxClosed').hide();
        $('.formBoxOpenedOk').show();
        $('.formBoxOpenedError').show();
        $('#formBox .linkOpen').click(function() {
		$('#formBoxOpened').show('fast');
		$('#formBoxClosed').hide('fast');
		if (typeof pT != "undefined") {pT._trackPageview("/denni-balicky-objednat");}
		return false;
	});
        $('#formBox .linkClose').click(function() {
		$('#formBoxClosed').show('fast');
		$('#formBoxOpened').hide('fast');
		return false;
	});

        /*LAYER EXAMPLES*/
	$('#layerExamples').hide();
        $('#layerExamples a.linkClose').click(function() {
		$('#layerExamples').fadeOut();
		return false;
	});
	$('#contentTitle a.linkQuestion').click(function() {
		$('#layerExamples').fadeIn();
		if (typeof pT != "undefined") {pT._trackPageview("/denni-balicky-priklad-vyuziti");}
		return false;
	});

        /*LAYER HOW IT WORKS*/
	$('#layerHowItWorks').hide();
        $('#layerHowItWorks a.linkClose').click(function() {
		$('#layerHowItWorks').fadeOut();
		//$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
	$('.box958White a.linkHowItWorks').click(function() {
		$('#layerHowItWorks').fadeIn();
		//$('html, body').animate({scrollTop:800}, 'slow');
		if (typeof pT != "undefined") {pT._trackPageview("/denni-balicky-jak-funguje");}
		return false;
	});

        /*LAYER SERVICES*/
	$('#layerServices').hide();
        $('#layerServices a.linkClose').click(function() {
		$('#layerServices').fadeOut();
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
	$('#boxVyberte a.linkQuestion').click(function() {
		$('#layerServices').fadeIn();
		$('html, body').animate({scrollTop:800}, 'slow');
		if (typeof pT != "undefined") {pT._trackPageview("/denni-balicky-sprava-moje-sluzby");}
		return false;
	});

	/*FORM VALIDATION*/
        $('.valid2 button').click(function(){
                $('.formError', $(this).parents('form')).remove();
                var error = '';
                var errorConnection = '';
                var errorModem = '';
				var hasNumber = false;
				var wrongNumber = null;

                $('.require', $(this).parents('form')).each(function(){

                        if($(this).attr('type') == 'text') {
                                if($(this).hasClass('requireMail')) {
                                        if($(this).val() == '') {
                                                error += 'Pole <strong>'+ $(this).prevAll('span').html().replace(':', '') +'</strong> není vyplněno.</br>';
                                                $(this).parent().addClass('error');
                                        }
                                        else {
                                                var email = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
                                                if(!email.test($(this).val())) {
                                                        error += '<strong>E-mail</strong> není zadán ve správném tvaru (např. jmeno@server.cz)<br/>';
                                                        $(this).parent().addClass('error');
                                                }
                                                else {
                                                        $(this).parent().removeClass('error');
                                                }
                                        }
                                }
				else if(!hasNumber && ($(this).attr('name') == 'cp' || $(this).attr('name') == 'co')) {
					if (($(this).val() != '')) {
						hasNumber = true;
					}
					else if (!wrongNumber) {
						wrongNumber = $(this);
					}
				}
                                else {
                                	if($(this).attr('name') != 'cp' && $(this).attr('name') != 'co' && $(this).attr('name') != 'personal_id' && $(this).attr('name') != 'personal_id_second_part' && $(this).val() == '') {

                                			error += 'Pole <strong>'+ $(this).prevAll('span').html().replace(':', '') +'</strong> není vyplněno.<br/>';
                                		$(this).parent().addClass('error');
                                	} else {
                                		$(this).parent().removeClass('error');
                                	}
                                }
                        }

                        if($(this).attr('type') == 'hidden') {
                        	if($(this).attr('name') == 'connection_type' && $(this).val() == '') {
                        	        errorConnection += 'Vyberte si, jak se budete připojovat.<br/>';
                        	        $(this).parent().addClass('error');
                        	} else {
                        	        $(this).parent().removeClass('error');
                        	}

                        	if($(this).attr('name') == 'modem_type' && $(this).val() == '') {
                        	        errorModem += 'Vyberte si modem.<br/>';
                        	        $(this).parent().addClass('error');
                        	} else {
                        	        $(this).parent().removeClass('error');
                        	}
                        }

                        if($(this).attr('type') == 'checkbox') {

				var checked = $(this).attr('checked');
				if (checked == 0)
				{
					error += 'Je třeba zaškrtnout <strong>Souhlas s podmínkami</strong>.<br/>';
				}

                        }
                })

//				if (!hasNumber) {
//					error += 'Pole Č. popisné nebo Č. orientační není vyplněno.<br/>';
//					$('#cp').parent().addClass('error');
//					$('#co').parent().addClass('error');
//
//				} else {
//					$('#cp').parent().removeClass('error');
//					$('#co').parent().removeClass('error');
//				}

                if(error != '') {
                	var scrollHeight = 0;

                	if(error != '') {
                        $('form .form-errors').prepend('<p class="formError">'+ error +'</p>');
                        scrollHeight = 1050;
                    }

					$('html, body').animate({scrollTop:scrollHeight}, 'fast');

					if (typeof pT != "undefined") {pT._trackPageview("/denni-balicky-form-error");}
                    return false;
                }
        });

	$('#isicLink').click(function() {
		promptVoucher();
		return false;
	});

	/*RECOMMEND TO FRIEND FORM VALIDATION*/
	$('.valid3 button').click(function() {
		$('.formError', $(this).parents('form')).remove();
                var error = '';

                $('.require', $(this).parents('form#sendFriend')).each(function(){

                        if($(this).attr('type') == 'text') {
                                if($(this).hasClass('requireMail')) {
                                        if($(this).val() == '') {
                                                error += 'Pole <strong>'+ $(this).prevAll('span').html().replace(':', '') +'</strong> není vyplněno.<br/>';
                                                $(this).parent().addClass('error');
                                        }
                                        else {
                                                var email = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
                                                if(!email.test($(this).val())) {
                                                        error += '<strong>'+ $(this).prevAll('span').html().replace(':', '') +'</strong> není zadán ve správném tvaru (např. jmeno@server.cz)<br/>';
                                                        $(this).parent().addClass('error');
                                                }
                                                else {
                                                        $(this).parent().removeClass('error');
                                                }
                                        }
				} else {
					if ($(this).val() == '') {
                                                error += 'Pole <strong>'+ $(this).prevAll('span').html().replace(':', '') +'</strong> není vyplněno.<br/>';
                                                $(this).parent().addClass('error');
					} else {
						$(this).parent().removeClass('error');
					}
				}
			}
		});

                if(error != '') {
                	var scrollHeight = 0;

                	if(error != '') {
                        $('.valid3 .send-friend-form-errors').prepend('<p class="formError">'+ error +'</p>');
                        scrollHeight = 1200;
                    }

					$('html, body').animate({scrollTop:scrollHeight}, 'fast');

					if (typeof pT != "undefined") {pT._trackPageview("/doporucit-znamemu-form-error");}
                    return false;
                }
	});

	// promo slideshow
	animateDot($('.slideShow.active .dotSlider .dotActive'));

	$('.slideShow .orderPromo').hover(function(e) {
			$animated = $('.slideShow.active .dotSlider :animated');
			$animated.stop();
		},
		function(e) {
			animateDot($animated.parent());
		});

	$('.slideShow a.orderPromo[href|="#objednavkaFormInternet1"]').click(function(e) {
			$animated = $('.slideShow.active .dotSlider :animated');
			$animated.stop();
			$('html, body').animate({scrollTop:710}, 'fast');
			return false;
		});

	$('.dotSlider div').click(function(e) {
		// zastaveni animace a nastaveni na zacatek
		$animated = $('.slideShow.active .dotSlider .dotActive .:animated');
		$animated.stop().dequeue().width(21);

		$id = $(this).attr('id').substring(9,10) - 1;
		//console.log($id);
		$active = $('.slideShow.active');
		$now = $('.slideShow:eq(' + $id +')');
		//console.log($now);
		$active.removeClass('active');
		$active.hide();
		$now.addClass('active');
		$now.show();
		$('.slideShow.active .dotSlider .dotActive span').width(0);
		animateDot($('.slideShow.active .dotSlider .dotActive'));
	});

	// Check box toggle ISIC
	if (!$('#isicDiscount').attr("checked"))
		$('#isicPart').hide();

	$('#isicDiscount').click(function(e) {
		if($(this).attr("checked")) {
			$('#isicPart').show();
			$('#isicPart input').each(function(){
				$(this).addClass('require');
			});
		} else {
			$('#isicPart').hide();
			$('#isicPart input').each(function(){
				$(this).removeClass('require');
			});
		}
	});
	$('input[name=packageType]').click(function(e) {
		if ($(this).val() == 'withoutModem') {
			$('#isicDiscount').removeAttr('checked').attr('disabled','disabled');
			$('#isicDiscount').click();
		} else {
			$('#isicDiscount').removeAttr('disabled');
		}
	});

	// Check box toggle ASUS
	if (!$('#asusDiscount').attr("checked"))
		$('#asusPart').hide();

	$('#asusDiscount').click(function(e) {
		if($(this).attr("checked")) {
			$('#asusPart').show();
			$('#asusPart input').each(function(){
				$(this).addClass('require');
			});
		} else {
			$('#asusPart').hide();
			$('#asusPart input').each(function(){
				$(this).removeClass('require');
			});
		}
	});
	$('input[name=packageType]').click(function(e) {
		if ($(this).val() == 'withoutModem') {
			$('#asusDiscount').removeAttr('checked').attr('disabled','disabled');
			$('#asusDiscount').click();
		} else {
			$('#asusDiscount').removeAttr('disabled');
		}
	});

	// Check box toggle CSOB
	if (!$('#csobDiscount').attr("checked"))
		$('#csobPart').hide();

	$('#csobDiscount').click(function(e) {
		if($(this).attr("checked")) {
			$('#csobPart').show();
			$('#csobPart input').each(function(){
				$(this).addClass('require');
			});
		} else {
			$('#csobPart').hide();
			$('#csobPart input').each(function(){
				$(this).removeClass('require');
			});
		}
	});
	$('input[name=packageType]').click(function(e) {
		if ($(this).val() == 'withoutModem') {
			$('#csobDiscount').removeAttr('checked').attr('disabled','disabled');
			$('#csobDiscount').click();
		} else {
			$('#csobDiscount').removeAttr('disabled');
		}
	});

	// UI Dialog
	$('#coverageLink, #coverageLink2').click(function(e) {
		e.preventDefault();
		var $this = $(this);
		var horizontalPadding = 30;
		var verticalPadding = 30;
			$('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
				title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
				autoOpen: true,
				width: 825,
				minHeight: 500,
				modal: true,
				resizable: true,
				autoResize: true,
				dialogClass: "overflowHidden",
				overlay: {
					opacity: 0.5,
					background: "black"
				}
		}).width(825 - horizontalPadding).height(550 - verticalPadding);
	});

	$('#faqLink, #videoLink').click(function(e) {
		e.preventDefault();
		var $this = $(this);
		var horizontalPadding = 30;
		var verticalPadding = 30;
			$('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
				title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
				autoOpen: true,
				width: 825,
				minHeight: 600,
				modal: true,
				resizable: true,
				autoResize: true,
				overlay: {
					opacity: 0.5,
					background: "black"
				}
		}).width(825 - horizontalPadding).height(600 - verticalPadding);
	});

	// Bubble
	$('#popupBubble1').hover(function(e) {
		clearTimeout(bubbleTimeout1);
	},
	function(e) {
		bubbleTimeout1 = setTimeout('hideBubble1()', 200);
	});

	$('#bubble1').hover(function(e) {
		clearTimeout(bubbleTimeout1);
		this.width = $(this).width();
		this.posLeft = $(this).position().left;
		this.posTop = $(this).position().top;
		$('#popupBubble1').css({left:this.posLeft - this.width/2 + 75}).css({top:this.posTop-102}).show();
	},
	function(e) {
		bubbleTimeout1 = setTimeout('hideBubble1()', 200);
	});

	$('#popupBubble2').hover(function(e) {
		clearTimeout(bubbleTimeout2);
	},
	function(e) {
		bubbleTimeout2 = setTimeout('hideBubble2()', 200);
	});

	$('#bubble2').hover(function(e) {
		clearTimeout(bubbleTimeout2);
		this.width = $(this).width();
		this.posLeft = $(this).position().left;
		this.posTop = $(this).position().top;
		$('#popupBubble2').css({left:this.posLeft - this.width/2 + 28}).css({top:this.posTop-287}).show();
	},
	function(e) {
		bubbleTimeout2 = setTimeout('hideBubble2()', 200);
	});

	$('#popupBubble3').hover(function(e) {
		clearTimeout(bubbleTimeout3);
	},
	function(e) {
		bubbleTimeout3 = setTimeout('hideBubble3()', 200);
	});

	$('#bubble3').hover(function(e) {
		clearTimeout(bubbleTimeout3);
		this.width = $(this).width();
		this.posLeft = $(this).position().left;
		this.posTop = $(this).position().top;
		$('#popupBubble3').css({left:this.posLeft + this.width/2 - 117}).css({top:this.posTop + 14}).show();
	},
	function(e) {
		bubbleTimeout3 = setTimeout('hideBubble3()', 200);
	});

	$('#popupBubble4').hover(function(e) {
		clearTimeout(bubbleTimeout4);
	},
	function(e) {
		bubbleTimeout4 = setTimeout('hideBubble4()', 200);
	});

	$('#bubble4').hover(function(e) {
		clearTimeout(bubbleTimeout4);
		this.width = $(this).width();
		this.posLeft = $(this).position().left;
		this.posTop = $(this).position().top;
		$('#popupBubble4').css({left:this.posLeft + this.width/2 - 117}).css({top:this.posTop + 14});
	},
	function(e) {
		bubbleTimeout4 = setTimeout('hideBubble4()', 200);
	});
});

function selectConnectionType(connectionType) {
	var arr = [ "connection-type-1", "connection-type-2", "connection-type-3" ];

	jQuery.each(arr, function() {
		$("#" + this).removeClass('active');
	});

	$("#" + connectionType).addClass('active');

	$("#connection_type").attr('value', connectionType);
}

function selectModemType(modemType) {
	var arr = [ "modem-type-1", "modem-type-2" ];

	jQuery.each(arr, function() {
		$("#" + this).removeClass('active');
	});

	$("#" + modemType).addClass('active');

	$("#modem_type").attr('value', modemType);
}

function promptVoucher() {
	voucher = prompt('Vložte kód, který najdete na spodní straně voucheru', '');
	if (voucher == null || voucher == '') {
		return;
	}
	$.ajax({
		url: 'test-voucher',
		data: ({v : voucher}),
		success: function(data) {
			if (data != '-')
				document.location = data;
			else
				promptVoucher();
		}
	});
}

function IsNumeric(number){
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(number));
}

/* PROMO */

function animateDot($dot) {
	$span = $dot.find('span');
	// timer se meni podle pomeru sirky obrazku
	$timer = 17000;
	if ($span.width() > 0) {
		$timer = 17000 - ($span.width()/21) * 17000;
	}
	$span.animate(
		{
			width: ['21px', 'linear']
		},
		$timer,
		function() {
			$promo = $dot.parent().parent();
			if ($promo.hasClass('active')) {
				promoSwitch();
			}
		}
	);
}

function promoSwitch() {
	$active = $('.slideShow.active');
	$next = $active.next(':not(.slideRemove)');
	if ($next.length == 0) {
		$next = $('.slideShow').first();
	}
	if ($active.attr('id') != $next.attr('id')) {
		$active.fadeOut(600, function(){});
		$active.removeClass('active');
		$next.fadeIn(600, function(){});
		$next.addClass('active');
	}
	$('.slideShow.active .dotSlider .dotActive span').width(0);
	animateDot($('.slideShow.active .dotSlider .dotActive'));

};

function hideBubble1() {
	$('#popupBubble1').hide();
}

function hideBubble2() {
	$('#popupBubble2').hide();
}

function hideBubble3() {
	$('#popupBubble3').hide();
}

function hideBubble4() {
	$('#popupBubble4').css({left: -1000});
}

