var Page = function() {
	this.mapScale = 14;
	this.loaderImage = new Image();
	this.loaderImage.src = '/styles/loading.gif';
	this.messages = new Array();
}

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){
	var src = $('#' + img).attr('src').replace(/&nc=\d+$/, '&nc='+(new String((new Date).getTime())).substr(8,5));
	$('#' + img).css('visibility', 'hidden').attr('src', src).css('visibility', 'visible');
}

Page.prototype.changeCaptcha = function(imageId) {
	this.reloadCaptcha(imageId);
}

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.onload = function() {
	var timeout    = 500;
	var closetimer = 0;
	var ddmenuitem = 0;

	function jsddm_open() {
		jsddm_canceltimer();
		jsddm_close();
		ddmenuitem = $(this).find('ul').css('visibility', 'visible');
	}

	function jsddm_close() {
		if(ddmenuitem) {
			ddmenuitem.css('visibility', 'hidden');
		}
	}

	function jsddm_timer() {
		closetimer = window.setTimeout(jsddm_close, timeout);
	}

	function jsddm_canceltimer() {
		if(closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}
	$(document).ready(function() {
		$('#menu > li').bind('mouseover', jsddm_open);
		$('#menu > li').bind('mouseout',  jsddm_timer);
	});
	document.onclick = jsddm_close;
	if(jQuery.browser.msie) {
		$.getScript("/js/DD_belatedPNG_0.0.8a.js", function(){
			DD_belatedPNG.fix ('#tabs h2 a');
		});
	}
}


Page.prototype.validateOrderForm = function() {
	    $("#feedback").validate({
			rules: {
				'name': "required",
				'email': "required",
				'phone': "required",
				'subscribeCaptcha': "required",
				'email': {
					required: true,
					email: true
				}
			},
			messages: {
				'name': this.messages['required'],
				'email': this.messages['required'],
				'phone': this.messages['required'],
				'subscribeCaptcha': this.messages['required'],
				'email': {
					required: this.messages['required'],
					email: this.messages['requiredEmail']
				}
			},
		});
}


var Page = new Page();
$(function() {
  Page.onload();
});
