var Calendar = (function(){
	var global = {
		updateSelection: function(){
			var newId = global.selectedDateForm.val();
			var selectFunction = function(){
				(this.id == newId) && (this.select());
			}
			jQuery.each(global.bigDates, selectFunction);
			jQuery.each(global.dates, selectFunction);
		},
		selectedDate: false,
		selectedBigDate: false
	}
	global.selectedDate = false;
	//date
	var initDate = function(element){
		var self 				= this;
		self.element 		= element;
		self.selected 	= false;
		self.id 			  = self.element.find('span.date-id').text();
		self.dateTitle	= self.element.find('span.date-title').text();
		self.element.click(function(){
			global.selectedDateForm.val(self.id);
			global.updateSelection();
		});
		if (global.selectedDateForm.val()==self.id){
			self.select();
		}
	}
	initDate.prototype = {
		unselect: function(){
			var self = this;
			self.element.attr('class', 'free');
			self.selected = false;
		},
		select: function(){
			var self 			= this;
			if (!self.selected){
				self.selected = true;
				global.selectedDateTitle.text(self.dateTitle);
				self.element.attr('class', 'select');
				if (global.selectedDate){
					global.selectedDate.unselect();
				}
				global.selectedDate 	= self;
			}
		}
	}
	// big dates
	var initBigDate = function(element, offset){
		var self 			= this;
		self.offset 	= offset
		self.element 	= element;
		self.visible 	= false;
		self.selected = false;
		self.id 			= self.element.find('span.date-id').text();
		self.title 		= self.element.find('span.date').text()+' '+self.element.find('span.month').text();
		self.element.click(function(){
			global.selectedDateForm.val(self.id);
			global.updateSelection();
			//self.select();
		});
	}
	initBigDate.prototype = {
		unselect: function(){
			var self = this;
			if (self.selected){
				self.selected = false;
				self.element.removeClass('select');
			}
		},
		select: function(){
			var self = this;
			if (!self.selected){
				self.selected = true;
				self.element.addClass('select');
				global.selectedBigDateTitle.text(self.title);
				global.selectedBigDate && global.selectedBigDate.unselect();
				global.selectedBigDate = self;
				if ((self.offset < global.main.offset)){
					global.main.offset = self.offset;
					global.main.showBigDates();
				}
				if(self.offset >= (global.main.offset+5)){
					global.main.offset = self.offset - 4;
					global.main.showBigDates();
				}
			}
		},
		hide: function(){
			var self = this;
			if (self.visible) {
				self.element.hide();
				self.visible = false;
			}
		},
		show: function(){
			var self = this;
			if (!self.visible) {
				self.element.show();
				self.visible = true;
			}
		}
	}
	//calendar
	var init = function(){
		var self 					= this;
		global.main 			= self;
		self.register = jQuery('#register');
		if (self.register.length > 0){
			self.calendar 		= jQuery('#calendar');
			self.calendarBg 	= self.calendar.find('div.background');
			self.closeButton 	= self.calendar.find('div.window div.header div.close');
			self.closeButton.click(function(){
				self.closeCalendar();
			});
			self.calendarBg.click(function(){
				self.closeCalendar();
			});
			self.register.find('form span.calendar').click(function(){
				self.showCalendar();
			});
			//init dates
				global.selectedDateForm 		= self.register.find('input[name=date]');
				global.selectedDateTitle 		= self.calendar.find('span.selected-date-title');
				global.selectedBigDateTitle = self.register.find('span.selected-date-title');
				global.dates = [];
				self.calendar.find('div.window div.calendars ul.days li.free').each(function(){
					global.dates.push(new initDate(jQuery(this)));
				});
			//init big dates
			global.bigDates = [];
			self.register.find('div.register-form form div.dates ul li').each(function(){
				global.bigDates.push(new initBigDate(jQuery(this), global.bigDates.length));
			})
			self.offset = 0;
			//bind buttons
			self.leftScrollButton 	= self.register.find('div.register-form form div.dates div.scroll-left');
			self.rightScrollButton 	= self.register.find('div.register-form form div.dates div.scroll-right');
			self.leftScrollButton.click(function(){
				self.moveBigDates(-1);
			});
			self.rightScrollButton.click(function(){
				self.moveBigDates(+1);
			});
			self.showBigDates();
			global.updateSelection();
		}
	}
	init.prototype = {
		disableButtons: function(){
			var self = this;
			if (self.offset <= 0){
				self.leftScrollButton.addClass('scroll-left-disabled');
			}else{
				self.leftScrollButton.removeClass('scroll-left-disabled');
			}
			if (self.offset >= (global.bigDates.length-5)){
				self.rightScrollButton.addClass('scroll-right-disabled');
			}else{
				self.rightScrollButton.removeClass('scroll-right-disabled');
			}
		},
		moveBigDates: function(offset){
			var self 			= this;
			var newOffset = self.offset + offset;
			if ((newOffset >= 0)&&(newOffset <= (global.bigDates.length-5))){
				self.offset = newOffset;
				self.showBigDates();
			}
		},
		showBigDates: function(){
			var self = this;
			jQuery.each(global.bigDates, function(key){
				if ((key >= self.offset)&&(key < (self.offset+5))){
					this.show();
				}else{
					this.hide();
				}
			})
			self.disableButtons();
		},
		closeCalendar: function(){
			var self = this;
			self.calendar.hide();
		},
		showCalendar: function(){
			var self = this;
			self.calendar.show();
			self.calendarBg.css('opacity', '0.5');
		}
	}
	jQuery(function(){
		new init();
	});
})();

///////////////////////////////////////////////////////

var AjaxHistory = function(){
	var init = function(){
		var self = this;
		jQuery(function(){
			jQuery.browser.msie && self.addIframe();
		});
	}
	init.prototype = {
		
		globalSetHash: function(hash){
			var location = window.document.location.href;
			window.document.location.href = location.replace(new RegExp('#[^\\s]*'), '#'+hash);
		},
		
		addIframe: function(){
			var self = this;
			self.iframe = jQuery('<iframe></iframe>').hide();
			jQuery('body').append(self.iframe);
			self.setHash(self.getHash());
		},
		
		getHash: function(){
			var match = (window.document.location.href.match(new RegExp('#[^\\s]*')));
			if (match) {
				return match[0].replace(new RegExp('#'), '');
			} else {
				return '';
			}
		},
		setHash: function(hash){
			var self = this;
			var location = window.document.location.href;
			if (location.match(new RegExp('#[^\\s]*'))){
				window.document.location.href = location.replace(new RegExp('#[^\\s]*'), '#'+hash);
			} else {        
				window.document.location.href = location+'#'+hash;
			}
			//for ie
			if (jQuery.browser.msie){
				var frame = self.iframe.get(0);
				var output = frame.contentDocument || frame.contentWindow.document;
				output.open();
				output.write('<script type="text/javascript">window.onload = function(){parent.AjaxHistory.globalSetHash("'+hash+'")}</script>');
				output.close();
			}
		}
	}
	return new init();
} ();

////////////////////////////////////////////////////

var Menu = (function(){
	var initLink = function(element, parent){
		var self 										= this;
		self.element 									= element;
		self.parent 									= parent;
		self.anchor 									= self.element.find('a');
		self.hash 										= self.anchor.attr('href').replace(new RegExp('#', 'gi'), '');
		self.parent.links[self.hash] 	                = self;
		self.item 										= self.parent.main.find('div.about-items div.'+self.hash);
		self.element.click(function(){
			AjaxHistory.setHash(self.hash);
			return false;
		});
	}
	initLink.prototype = {
		unselect: function(){
			var self = this;
			self.element.removeClass('select');
			self.item.removeClass('select');
		},
		select: function(){
			var self = this;
			self.element.addClass('select');
			self.item.addClass('select');
		}
	}
	
	var init = function(){
		var self 	= this;
		self.main = jQuery('#about');
		if (self.main.length > 0){
			//do it
			self.links = {};
			self.main.find('#menu li').each(function(){
				new initLink(jQuery(this), self);
			})
			self.lastHash = 'what';
			self.waiter();
		}
	}
	init.prototype = {
		getHash: function(){
			var self = this;
			var hash = AjaxHistory.getHash();
			if ((hash.length == 0)||(typeof(self.links[hash]) == 'undefined')){
				hash = 'what';
			}
			return hash;
		},
		waiter: function(){
			var self 		= this;
			var newHash = self.getHash();
			if (self.lastHash != newHash){
				self.lastHash && self.links[self.lastHash].unselect();
				self.links[newHash].select();
				self.lastHash = newHash;
			}
			setTimeout(function(){
				self.waiter();
			}, 300);
		}
	}
	jQuery(function(){
		new init();
	})
})();

//////////////////////////////////////
    
    
    function Submit() {
        var inputCorrect = true;
        var data = new Array();
        var i = 0;
        $("#form>input").each(function(index, element) {
            try {
                ClearCheckerStyles(element);

                if (Checker[element.name].Func(element.value)) {
                    $(element).addClass("success");

                    data[i++] = { name: element.id, value: element.value };
                }
                else {
                    $(element).addClass("error");
                    $(element).next().append(Checker[element.name].Message);
                    inputCorrect = false;
                }
            }
            catch (ex) {
            }
        });

        $("#form>select").each(function(index, element) {
            try {
                ClearCheckerStyles(element);

                if (Checker[element.name].Func(element.value)) {
                    $(element).addClass("success");

                    data[i++] = { name: element.id, value: element.value };
                }
                else {
                    $(element).addClass("error");
                    $(element).next().append(Checker[element.name].Message);
                    inputCorrect = false;
                }
            }
            catch (ex) {
            }
        });


        if (inputCorrect) {
            data[i] = { name: "seminarID", value: $("#register").find("input[name=date]").attr("value")};
            SendData(data);
        }
    }

    function SendData(data) {
        $("#disabledSubmit").show();
        $("#activeSubmit").hide();
        var now = new Date();
        $.getJSON(
            "Default.aspx?tick=" + now.getFullYear() + now.getMonth() + now.getHours() + now.getMinutes() + now.getSeconds() + now.getMilliseconds(),
            data,
            function(response) {
                if (response.state == 'ok') {
                    $("#registration").hide();
                    $("#notification").show();
                }
                else {
                    $("#registration").hide();
                    $("#errorNote").show();
                }
            }
        );
    }

    function ClearCheckerStyles(element) {
        $(element).removeClass("error");
        $(element).removeClass("success");
    }

    var Checker;
    $(
    function() {
        Checker = new Array();
        Checker["PolygonID"] = {
            Func: function(inp) {
                return true;
            },
            Message: "Field required"
        }
        
        Checker["Name"] = {
            Func: function(inp) {
                return /\s*\S+/.test(inp);
            },
            Message: "Field required"
        }

        Checker["Free"] = {
            Func: function(inp) {
                return /\S+/.test(inp);
            },
            Message: "Field required"
        }

        Checker["Number"] = {
            Func: function(inp) {
                return inp != '' && !isNaN(new Number(inp));
            },
            Message: "Field missing or you entered not a number"
        }

        Checker["Phone"] = {
            Func: function(inp) {
                return /\s*\S{3,}/.test(inp);
            },
            Message: "Field required"
        }

        Checker["Date"] = {
            Func: function(inp) {
                return /\s*\S{3,}/.test(inp);
            },
            Message: "Field missing or not a date"
        }

        Checker["Mail"] = {
            Func: function(inp) {
                return /\s*\S+@\S+/.test(inp);
            },
            Message: "Field missing or not a mail"
        }

        Checker["City"] = {
            Func: function(inp) {
                return /\s*\S{3,}/.test(inp);
            },
            Message: "Field required"
        }
    }
);

