var Page = function() {
	this.mapScale = 14;
	this.loaderImage = new Image();
	this.loaderImage.src = '/styles/loading.gif';
	this.sliders = new Array();
	this.tabsUsed = new Array();
	this.helpDiv = false;
	this.orderLocks = new Array();
	this.justRetestFlag = false;
	this.rightBlockCreated = -2;
}

Page.prototype.validate = function(formId) {
	var result = true;
	if($('#feedBackName').val().length == 0) {
		$('#feedBackName').parent().addClass('formSelected');
		result = false
	} else {
		$('#feedBackName').parent().removeClass('formSelected');
	}

	if($('#feedBackEmail').val().length == 0) {
		$('#feedBackEmail').parent().addClass('formSelected');
		result = false
	} else if(!$('#feedBackEmail').val().match(/^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}/)) {
		$('#feedBackEmail').parent().addClass('formSelected');
		result = false
	} else {
		$('#feedBackEmail').parent().removeClass('formSelected');
	}

	if($('#feedBackPhone').val().length == 0) {
		$('#feedBackPhone').parent().addClass('formSelected');
		result = false
	} else {
		$('#feedBackPhone').parent().removeClass('formSelected');
	}

	if($('#feedBackText').val().length == 0) {
		$('#feedBackText').parent().addClass('textareaSelected');
		result = false
	} else {
		$('#feedBackText').parent().removeClass('textareaSelected');
	}
	return result;
}

Page.prototype.showAddress = function(address) {
	if(!address) {
		var address = $('#contactAddress').val();
		if(!address) {
			alert('Neuvedena adresa');
			return false;
		}
	}
	if(!this.map) {
		this.geocoder = new google.maps.Geocoder();
		$('#contactMap').css('display', 'block');
	    var myOptions = {
	      zoom: this.mapScale,
	      disableDefaultUI: true,
	      navigationControl:true,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    this.map = new google.maps.Map(document.getElementById('contactMap'), myOptions);
	}
	this.geocoder.geocode({"address": address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
            Page.map.setCenter(results[0].geometry.location);
            if(!Page.marker) {
	            Page.marker = new google.maps.Marker({
	                map: Page.map, 
	                position: results[0].geometry.location,
	                draggable: false
	            });
            } else {
            	Page.marker.setPosition(results[0].geometry.location);
            }
          } else {
            alert("Geocode was not successful for the following reason: " + status);
          }
	});

}

Page.prototype.reloadCaptcha = function(imageId){
	if(document.getElementById(imageId)) {
		var image = document.getElementById(imageId);
	} else {
		return false;
	}
	image.parentNode.style.background = 'transparent url("' + this.loaderImage.src + '") no-repeat center center';
	image.style.visibility = 'hidden';
	image.onload = function() {
		this.style.visibility = 'visible';
		this.parentNode.style.background = 'none';
	};
	image.src = image.src.replace(/&nc=\d+/, '&nc='+(new String((new Date).getTime())).substr(8,5));
}

Page.prototype.commentReply = function(commentId){
	if(!this.commentHolder) {
		this.commentHolder = document.createElement('div');
		this.commentHolder.className = 'commentHolder';
		this.commentHolder.innerHTML = 'Váš nový komentář';
	}
	var comment = document.getElementById('comment' + commentId);
	if(!comment) return false;
	if(comment.nextSibling.nextSibling && comment.nextSibling.nextSibling.id) {
		comment.parentNode.insertBefore(this.commentHolder, comment.nextSibling.nextSibling);
	}
	else if(comment.nextSibling && comment.nextSibling.id) {
		comment.parentNode.insertBefore(this.commentHolder, comment.nextSibling);
	} else {
		comment.parentNode.appendChild(this.commentHolder);
	}
	document.getElementById('commentParent').value = commentId;
	
	//if(comment.nextSibling).tagName
	//comment.parentNode.insertAfter()
}

Page.prototype.articleVote = function(articleId, direction) {
	$.post("/ajax.php", {'action':'articleVote', 'articleId': articleId, 'direction': direction}, function(data){
		if (data>0) {
			$('#voteValue'+articleId).html(data);
			if($('#voteError'+articleId)) {
				$('#voteError'+articleId).css('display', 'none');
			}
			if($('#voteOk'+articleId)) {
				$('#voteOk'+articleId).css('display', 'block');
			}
		} else {
			if($('#voteError'+articleId)) {
				$('#voteError'+articleId).css('display', 'block');
				if(data) {
					$('#voteError'+articleId).html(data);
				}
			}
			if($('#voteOk'+articleId)) {
				$('#voteOk'+articleId).css('display', 'none');
			}
		}
	});
}
Page.prototype.changeCaptcha = function(img) {
	$('#'+img).css('visibility', 'hidden');
	if(typeof document.getElementById(img).onload == 'undefined') {
		document.getElementById(img).onload = function() {
			$('#'+img).css('visibility', 'visible');
		}
	}
	document.getElementById(img).src = document.getElementById(img).src.replace(/&nc=\d+$/, '&nc='+(new String((new Date).getTime())).substr(8,5));
}

Page.prototype.gallerySlider = function(direction) {
	var left = $('#galleryHolder ul').css('margin-left').replace('px', '') - 0;
	var width = $('#galleryHolder ul').width();
	if(width+2*left<287 && direction<0) return false;
	if(left >= 0 && direction>0) return false;
	$('#galleryHolder ul').css('margin-left', (left + direction * 95) + 'px');
}

Page.prototype.createSlider = function(holder, direction, serviceId, maxValue, stepValue, defaultNum, defaultValue) {
	if(direction == 'v') {
		this.fixSliderHeight(holder, serviceId);
		defaultNum = $('#' + holder + ' .sliderParent' + serviceId + ' div.sliderValue').size() - defaultNum + 1;
	}
	$("#" + holder + " .slider" + serviceId).slider(
		{min:1, max:maxValue, step:stepValue, orientation:(direction == 'h' ? 'horizontal' : 'vertical'), value: defaultNum, animate: true, change: function(event, ui) {
			var holder = this.parentNode.parentNode.parentNode.parentNode.id;
			var newId = this.className.substr(0, this.className.indexOf(' '));
			var parentId = this.parentNode.className.substr(0, this.parentNode.className.indexOf(' '));
			if('vertical' == $('#' + holder + ' .' + newId).slider('option', 'orientation')) {
				ui.value = $('#' + holder + ' .'+parentId+' .sliderValue').size() - ui.value + 1;
			}
			if(this.parentNode && this.parentNode.className) {
	    		var iteration = 1;
	    		$('#' + holder + ' .'+parentId+' .sliderValue').each(function(cnt, el) {
	    			if(iteration == ui.value) {
	    				if(!Page.justRetestFlag) {
	    					Page.justRetestFlag = true;
	    					var valueId = el.className.substr(0, el.className.indexOf(' '));
	    					$('#' + holder + ' .'+valueId).click();
	    				} else {
	    					Page.justRetestFlag = false;
	    				}
	    				$(el).addClass('green');
	    				//el.className = valueId + ' green sliderValue';
	    			} else {
	    				$(el).removeClass('green');
	    				//el.className = valueId + ' sliderValue';
	    			}
	    			++iteration;
	    		})
	    	}
			Page.calculate(holder);
		}
	});
	if(defaultValue) {
		$('#' + holder + ' .sliderValue' + defaultValue).addClass('green');
	} else {
		$('#' + holder + ' .sliderParent' + serviceId + ' div.sliderValue').first().addClass('green');
	}
	
}

Page.prototype.changeSlider = function(holder, serviceId, valueNum, valueId, price) {
	if(price == 88888 || price == 99999) {
		setTimeout("alert('Zašlite nám, prosím, nezáväznú objednávku, pripravíme Vám riešenie na mieru');", 500);
		//$('#btnNezavaznaObjednavka').click();
		if(price == 99999) {
			$('#' + holder + ' .valueComment' + serviceId).show();
		}
		this.orderLocks[serviceId] = serviceId;
	} else {
		$('#' + holder + ' .valueComment' + serviceId).hide();
		if(this.orderLocks[serviceId] != 'undefined') {
			this.orderLocks.splice(serviceId, 1);
		}
	}
	if(this.orderLocks.length) {
		var hasData = false;
		for(var i=0; i<this.orderLocks.length; i++) {
			if(this.orderLocks[i]) {
				hasData = true;
				break;
			}
		}
		if(hasData) {
			$('.btnObjednat').hide();
			//this.calculate(holder);
		} else {
			$('.btnObjednat').show();
		}
	} else {
		$('.btnObjednat').show();
	}
	if(this.justRetestFlag) {
		this.justRetestFlag = false;
		return false;
	} else {
		this.justRetestFlag = true;
	}
	
	if('vertical' == $('#' + holder + ' .slider' + serviceId).slider('option', 'orientation')) {
		valueNum = $('#' + holder + ' .sliderParent' + serviceId + ' div.sliderValue').size() - valueNum + 1;
	}
	$('#' + holder + ' .sliderParent' + serviceId + ' div.sliderValue').removeClass('green');
	$('#' + holder + ' .sliderValue' + valueId).addClass('green');
	$('#' + holder + ' .slider' + serviceId).slider('value', valueNum);
	//this.sliders[serviceId].value(valueNum);
}

Page.prototype.fixSliderHeight = function(holder, serviceId) {
	$('#' + holder + ' .slider' + serviceId).css('height', $('#' + holder + ' .sliderParent' + serviceId + ' div.sliderDataVertical').height() - 27 + 'px');
}

Page.prototype.fixSliderHeightAll = function(holderId) {
	$('#servicesHolder'+holderId+' div.sliderParent').each(function(cnt,div) {
		if(!Page.tabsUsed[holderId]) {
			Page.fixSliderHeight('servicesHolder'+holderId, div.className.substr(0, div.className.indexOf(' ')).replace('sliderParent', ''));
		}
	})
	Page.tabsUsed[holderId] = true;
	if(this.rightBlockCreated < 0) {
		++this.rightBlockCreated;
	} else if(!this.rightBlockCreated) {
		$('#clearForm').hide();
		/*
		this.rightBlockCreated = 1;
		$('.content .rightCol').append($('.totalPrice::first'));
		$('.tabBlock2 .totalPrice').hide();
		this.createRightBlock($('.rightCol .totalPrice'));
		*/
	}
    if (holderId == 0) {
        $('#clearForm').show();
    }
}

Page.prototype.calculate = function(holder) {
	var price = 0;
	var hidden = 0;
	var serviceId = 0;
	var priceWithoutDiscount = 0;
	if($('.btnObjednat').css('display') == 'none') {
		/*
		$('#' + holder + ' .priceRow').hide();
		$('#' + holder + ' .discountRow').hide();
		$('#' + holder + ' .questionRow').show();
		alert(1);
		return false;
		*/
		$('.priceRow').hide();
		$('.discountRow').hide();
		$('.questionRow').show();
		return false;
	} else {
		$('.priceRow').show();
		$('.questionRow').hide();
		/*
		$('#' + holder + ' .priceRow').show();
		$('#' + holder + ' .questionRow').hide();
		*/
	}
	$('#' + holder + ' .sliderValue').each(function(cnt, el) {
		if(el.className.indexOf('green') > -1) {
			var parentId = el.parentNode.parentNode.className.substr(0, el.parentNode.parentNode.className.indexOf(' '));
			var elId = (el.className.indexOf(' ')>-1 ? el.className.substr(0, el.className.indexOf(' ')) : el.className);
			serviceId = parentId.replace('sliderParent', '');
			hidden = $('#'+holder+' .valuePrice' + elId.replace('sliderValue', ''));
			$('#'+holder+' .sliderParent' + serviceId + ' input[type=hidden]').each(function(j, hid) {
				hid.disabled = true;
			});
			//alert(hidden.val());
			if(hidden) {
				$('#' + holder + ' .valuePrice' + elId.replace('sliderValue', '')).attr('disabled', false);
				price += (hidden.val() - 0);
			}
		}
	});
	// Calculate select with prices in Kc
	$('#' + holder + ' .servicesHolder select').each(function(cnt, el) {
		var elId = el.className.indexOf(' ') > -1 ? el.className.substr(0, el.className.indexOf(' ')) : el.className;
		serviceId = elId.replace('select', '');
		if($('#' + holder + ' .selectIsOversale' + serviceId)) {
			return true;
		}
		hidden = $('#' + holder + ' .valuePrice' + el.value);
		$('#' + holder + ' .sliderParent' + serviceId + ' input[type=hidden]').each(function(j, hid) {
			hid.disabled = true;
		});
		if(hidden) {
			$('#' + holder + ' .valuePrice' + el.value).removeAttr('disabled');
			$('#' + holder + ' .valuePrice' + el.value).each(function(cnt, el) {
				el.attr('disabled', false);
			});
			price += (hidden.val() - 0);
		}
	});
	priceWithoutDiscount = price;
	// Calculate select with prices in %
	$('#' + holder + ' .servicesHolder select').each(function(cnt, el) {
		var elId = el.className.indexOf(' ') > -1 ? el.className.substr(0, el.className.indexOf(' ')) : el.className;
		serviceId = elId.replace('select', '');
		if(!$('#' + holder + ' .selectIsOversale' + serviceId)) {
			return true;
		}
		hidden = $('#' + holder + ' .valuePrice' + el.value);
		$('#' + holder + ' .sliderParent' + serviceId + ' input').each(function(j, hid) {
			hid.disabled = true;
		});
		if(hidden) {
			$('#' + holder + ' .valuePrice' + el.value).removeAttr('disabled');
			$('#' + holder + ' .valuePrice' + el.value).each(function(cnt, el) {
				el.disabled =  false;
			});
			//hidden.attr('disabled', false)
			price *= 1 + hidden.val()/100;
		}
	});
	var separators = {thousandSeparator: '.', decimalSeparator: ','};
	price = Math.round(price*100)/100;
	if($('.finalPrice')) {
		$('.finalPrice').html( $().number_format( price, separators ) );
		$('.finalPriceInput').val(price);
		if(priceWithoutDiscount > price) {
			$('.finalDiscount').html($().number_format( priceWithoutDiscount - price, separators) );
			$('.discountRow').show();
		} else {
			$('.discountRow').hide();
		}
	}
	/*
	if($('' + holder + ' .finalPrice')) {
		$('' + holder + ' .finalPrice').html(price);
		$('' + holder + ' .finalPriceInput').val(price);
		if(priceWithoutDiscount > price) {
			$('' + holder + ' .finalDiscount').html(priceWithoutDiscount - price);
			$('' + holder + ' .discountRow').show();
		} else {
			$('' + holder + ' .discountRow').hide();
		}
	}
	*/
}

Page.prototype.showHelp = function(text, holder) {
	if(!this.helpDiv) {
		this.helpDiv = document.createElement('div');
		this.helpDiv.id = 'helpDiv';
		$('.obal1').append(this.helpDiv);
	} else {
		this.helpDiv.style.display = 'block';
	}
	this.helpDiv.innerHTML = text;
	var offset = $('#'+holder+' sup').offset();
	this.helpDiv.style.top = offset.top + 'px';
	this.helpDiv.style.left = offset.left + 'px';
}

Page.prototype.hideHelp = function() {
	this.helpDiv.style.display = 'none';
}

Page.prototype.createRightBlock = function(block, header) {
	var holder = $('<div class="rightBox"><strong class="header">'+header+'</strong></div>');
	holder.attr('id', 'rightBlockPrice');
	holder.append(block);
	holder.append($('.buttons::first').clone());
	$('.content .rightCol').append(holder);
	//$('.tabBlock2 .buttons').hide();
	$('.rightCol .buttons img').css('width', '140px').css('margin', '10px 8px');
	//block.css('position', 'fixed').css('z-index', '9999').css('width', '316px').css('border', '1px solid #00722D').css('top', '100px').css('padding', '15px 10px');
	holder.css('position', 'fixed').css('z-index', '9999').css('width', '316px').css('top', '100px');
	block.css('margin', 0);
	$('.totalPrice td').css('padding', '10px 5px').css('font-size', '1.2em').css('background', '#00722D').css('color', '#FFF');
	$('.totalPrice td.right').css('white-space', 'nowrap');
	$('.priceRow td').css('font-weight', 'bold');
	$(window).bind('scroll', function() {
		Page.scrollRightBlock(holder);
	});
	Page.scrollRightBlock(holder);
}


Page.prototype.scrollRightBlock = function(block) {
	var sTop = $(window).scrollTop();
	if(sTop < 300) {
		block.css('position', 'absolute').css('top', ($('.breadcrumbs span').length ? '416px' : '398px')); //416px
		$('.rightCol').css('padding-top', (block.height() + 20) + 'px');
	} else {
		block.css('position', 'fixed').css('top', '100px');
		if(sTop < 1400) {
			$('.rightCol').css('padding-top', (sTop - ($('.breadcrumbs span').length ? 310 : 292)) + (block.height()) + 'px');
		} else {
			$('.rightCol').css('padding-top', '0');
		}
	}
}

/*
Page.prototype.prepareOrder = function(holder) {
	var order = '';
	var serviceId = 0;
	var value = 0;
	var valuePrice = 0;
	var resultTable = document.createElement('table');
	var resultRow, resultCell;
	$('#' + holder + ' big').each(function(cnt, service) {
		serviceId = service.id.replace('paramName', '');
		resultRow = document.createElement('tr');
		resultCell = document.createElement('td');
		resultCell.innerHTML = service.innerHTML;
		resultRow.appendChild(resultCell)
		if(serviceId) {
			value = $('#sliderParent' + serviceId + ' div.green').html();
			if(value) {
				resultCell = document.createElement('td');
				resultCell.innerHTML = value;
				resultRow.appendChild(resultCell)
				resultTable.appendChild(resultRow);
			}
		}
	});
	var input = document.createElement('textarea');
	input.name = 'formData';
	input.appendChild(resultTable);
	$('#' + holder).append(input);
	return true;
}
*/
var Page = new Page();
