var path = document.location.pathname;
if (path.charAt(path.length-1) != '/') 
	path = path+'/';

var site = {
	fonts: {
		init: function() {
			Cufon.replace($(".Myriad_Pro"), {fontFamily: 'Myriad Pro'});
			Cufon.replace($(".HeliosCond"), {fontFamily: 'HeliosCond'});
			Cufon.now();
		}
	},
	global: {
		init: function() {			
			site.tree.init();
			
			$("input.with-label").click(function() {
				if ($(this).val() == $(this).attr('rel'))
					$(this).val('');
			}).blur(function() {
				if ($(this).val() == '') {
					$(this).val($(this).attr('rel'));
				}
			});

			$("form.ajax").submit(function() {
				var $form = $(this);
				
				if ( !site.utils.checkForm($form) ) return false;
				
				$.ajax({
					type: $form.attr("config:method"),
					dataType: "json",
					url: $form.attr("config:url"),
					data: $form.serialize(),
					beforeSend: function() {
					},
					success: function(data) {
						var callback = '';
						switch (data.result) {
							case 'done':
								callback = $form.attr("id").replace('-form', '_success');
								break;
							case 'error':
								callback = $form.attr("id").replace('-form', '_error');
								break;						
						}
						
						site.callbacks[callback](data, $form);
					},
					error: function() {
					}
				});
		
				return false;
			});
			
			$("a.sh-block").toggle(function(){
				$("#"+$(this).attr("rel")).slideDown();
			}, function(){
				$("#"+$(this).attr("rel")).slideUp();
			});
		}
	},
	index: {
		init: function() {
			$(".catalogue-menu li").hover(function(){
				$(this).css('z-index', 100);
				$(this).find("div.submenu").show();
				$(this).find("a.level0").addClass("current");
			}, function(){
				$(this).find("div.submenu").hide();
				$(this).find("a.level0").removeClass("current");
				$(this).css('z-index', 1);
			});
		}
	},
	tree: {
		init: function() {
			$(".ajax-tree a").live("click", site.tree.onAjaxTreeNodeClick);
			$(".ajax-tree a").each(function(){
				var href = $(this).attr("href").replace('http://'+document.domain,'');							
				if ( href.charAt(href.length-1) != '/' ) href = href+'/';

				if (path.indexOf(href) == 0)
					$(this).parent().addClass("selected");
					
				if (document.location.pathname == '/')					
					return;				
				
				if (path.indexOf(href) == 0)					
					$(this).click();

			});
		},
		onAjaxTreeNodeClick: function(e) {
			var id = $(this).attr("rel");
			
			if ( !id ) return;
			
			var ajax_tree = $(this).parents(".ajax-tree");
			
			if ( ajax_tree.data("state-"+id) != null ) {
				if (ajax_tree.data("state-" + id) == true) {
					$(this).removeClass('current');
					$("#ajax-tree-node-" + id).slideUp();
				}
				else {
					$(this).addClass('current');
					$("#ajax-tree-node-" + id).slideDown();
				}
					
				ajax_tree.data("state-"+id, !ajax_tree.data("state-"+id));
				return false;
			}

			$(this).addClass('current');
			$('<ul id="ajax-tree-node-'+id+'"><li class="last">Загрузка...</li></ul>').insertAfter($(this));
			
			$.get(ajax_tree.attr("tree:url")+id,{}, function(data) {				
				$("#ajax-tree-node-"+id+" li").remove();
				for( i in data.nodes ) {
					$("#ajax-tree-node-"+id).append('<li><a class="level'+data.nodes[i].level+'" href="'+data.nodes[i].path+'"'+(parseInt(data.nodes[i].have_childs) > 0 ? ' rel="'+data.nodes[i].id+'"':'')+'>'+data.nodes[i].title+'</a></li>')
				}
				
				$("#ajax-tree-node-"+id+" a").each(function () {
					var href = $(this).attr("href").replace('http://'+document.domain,'');
					
					if ( $(this).attr("rel") > 0 ) {				
						if ( path.indexOf(href) == 0 )
							$(this).click();
					}
					else if ( path.indexOf(href) == 0 )
						$(this).addClass("current");
					}
				);

				$("#ajax-tree-node-"+id+" li:last").addClass("last");
				
				ajax_tree.data("state-"+id, !ajax_tree.data("state-"+id));
			},"json");
			
			return false;
		},
		closeAjaxTreeNode: function(e) {
			return false;
		}
	},
	
	photos: {
		init: function() {
			$(".photos .photos-preview").click(function(){
				var $photos = $(this).parents(".photos");
				$photos.find(".photos-preview img").attr("src","/images/frames/photos-middle.png");
				$(this).find("img").attr("src","/images/frames/photos-middle-current.png");
				
				$photos.find(".photos-main").attr("href", $(this).attr("href").replace('preview_', ''));
				$photos.find(".photos-main img").css("background-image", 'url("'+$(this).attr("href")+'")');
				
				return false;
			});
			
			$(".photos .photos-preview:eq(0)").click();
		}
	},
	scroller: {
		init: function() {
			
			$(".scroller").each(function(){
				var $scroller = $(this);
				var icons_count = $scroller.find(".scroller-photo-preview").length;
				
				$scroller.data('hidden-left',0);
				$scroller.data('hidden-right',0);
				if ( icons_count > $scroller.attr("config:visible") ) {
					$scroller.find("#arrow-right").attr('href','#').addClass('enabled');
					$scroller.data('hidden-right',icons_count-$scroller.attr("config:visible"));			
				}
			});
			
			$(".scroller #arrow-left, .scroller #arrow-right").click(function(){
				if (!$(this).attr('href'))
					return false;
					
				var $scroller = $(this).parent();
				var step = $scroller.attr("config:step");
				var direction = $(this).hasClass('arrow-left') ? 1 : -1;
		
				$scroller.data('hidden-left', $scroller.data('hidden-left')-direction);
				$scroller.data('hidden-right', $scroller.data('hidden-right')+direction);
				
				if ( $scroller.data('hidden-right') == 0 ) {
					$scroller.find("#arrow-right").removeAttr('href').removeClass('enabled');
				}
				else {
					$scroller.find("#arrow-right").addClass('enabled').attr('href','#');
				}
				
				if ( $scroller.data('hidden-left') == 0 ) {
					$scroller.find("#arrow-left").removeAttr('href').removeClass('enabled');
				}
				else {
					$scroller.find("#arrow-left").addClass('enabled').attr('href','#');
				}
				
				$scroller.find(".scroller-content").animate({
					'left': parseInt($scroller.find(".scroller-content").css('left'))+direction*step
				});
				
				return false;
			});
			
			$(".scroller .scroller-photo-preview").click(function(){
				var $scroller = $(this).parents(".scroller");
				$scroller.find(".scroller-photo-preview img").attr("src","/images/frames/project-middle.png");
				$(this).find("img").attr("src","/images/frames/project-middle-current.png");
				
				$(".scroller-photo-main").attr("href", $(this).attr("href").replace('preview_', ''));
				$(".scroller-photo-main img").css("background-image", 'url("'+$(this).attr("href")+'")');
				return false;
			});
			
			$(".scroller .scroller-photo-preview:eq(0)").click();
		}
	},
	callbacks: {
		feedback_success: function(data, form) {
			form.hide();
			$("#feedback-done").show();
			
			setTimeout(function() {
				form.show();
				form.find(":input").val('');
				$("#feedback-done").hide();
			}, 5000);
		},
		question_success: function(data, form) {
			form.hide();
			$("#question-done").show();
			
			setTimeout(function() {
				form.show();
				form.find(":input").val('');
				$("#question-done").hide();
			}, 5000);
		},
		order_success: function(data, form) {
			form.hide();
			$("#order-done").show();
			
			setTimeout(function() {
				form.show();
				form.find(":input").val('');
				$("#order-done").hide();
			}, 5000);
		}
	},
	utils: {
		checkForm: function(form) {
			var res = true;
			var current_res = true;
			$(":input.required", form).each(function() {
				current_res = true;
				if ( $(this).is('select') && (!$("option:selected", $(this)).length || $("option:selected", $(this)).val() == 0) ) {
					res = false;
					current_res = false;
					$("label[for='"+$(this).attr("name")+"']", form).addClass("required-error");
				}
				else if ( $(this).hasClass('with-label') && $(this).val() == $(this).attr('rel') ) {
					res = false;
					current_res = false;
				}
				else {
					if ( $(this).val() == '' ) {				
						res = false;
						current_res = false;
					}
					else if ( $(this).hasClass("email") ) {
						var emailTest = '^[_\\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z_-]+\\.)+[a-z]{2,4}$';
						var regex = new RegExp(emailTest);
						if ( $(this).val() != '' && !regex.test($(this).val()) ) {
							res = false;
							current_res = false;
						}
					}
				}
				
				if ( !current_res ) {
					if ( $(this).hasClass('with-label') )
						$(this).addClass("required-error");
					else
						$("label[for='"+$(this).attr("name")+"']", form).addClass("required-error");
				}
			});
			
			$(":checkbox.required", form).each(function(){
				if ( !$(this).is(":checked") ) {
					res = false;
					$("label[for='"+$(this).attr("name")+"']", form).addClass("required-error");
				}
			});
			
			$("label.radio.required", form).each(function(){

				var radio_name = $(this).attr("for");
				if (!form.find("input[name="+radio_name+"]:radio:checked").length) {
					$(this).addClass("required-error")
				}
			});
			
			setTimeout(function() { 
				$('.required-error', form).removeClass("required-error");
			}, 5000);
			
			return res;
		}
	}
};

$(document).ready(site.global.init);
