// JavaScript Document

var SALEX = {
	str_url: '',
	str_content_box_1_id: 'js_content_1',
	obj_content_box_1: null,
	str_content_box_2_id: 'js_content_2',
	obj_content_box_2: null,
	str_loading_box_id: 'loading_content',
	obj_loading_box: null,
	int_active_box: 1,
	str_active_content: '',
	obj_fade_effect: null,
	obj_appear_effect: null,
	obj_fade_loading: null,
	obj_appear_loading: null,
	
	init: function() {
		 SALEX.str_url = SALEX.getUrl();
		 SALEX.obj_content_box_1 = $(SALEX.str_content_box_1_id);
		 SALEX.obj_content_box_2 = $(SALEX.str_content_box_2_id);
		 SALEX.obj_loading_box = $(SALEX.str_loading_box_id);
	},
	
	getUrl: function() {
		var obj_loc = window.location;
		var str_url = obj_loc.protocol + '//' + obj_loc.host + obj_loc.pathname + obj_loc.search;
		if (str_url.substr(str_url.length-1, 1) != '/') {
			str_url += '/';	
		}
		
		return str_url;
	},
	
	showContentLoader: function() {
		if (SALEX.int_active_box == 1) {
			SALEX.obj_fade_effect = new Effect.Opacity(SALEX.obj_content_box_1, {duration: 0.6, to: 0.1});
		} else {
			SALEX.obj_fade_effect = new Effect.Opacity(SALEX.obj_content_box_2, {duration: 0.6, to: 0.1});
		}
		SALEX.obj_appear_loading = new Effect.Opacity(SALEX.obj_loading_box, {duration: 0.4, to: 1.0});
	},
	
	updateContent: function(_obj_ajax_response) {
		if (SALEX.int_active_box == 1) {
			SALEX.obj_content_box_2.style.height = 'auto';
			SALEX.obj_content_box_2.innerHTML = _obj_ajax_response.responseText;
			//SALEX.obj_fade_effect = new Effect.Opacity(SALEX.obj_content_box_1, {duration: 0.4, to: 0.0});
			SALEX.obj_content_box_2.show();
			SALEX.obj_content_box_1.hide();
			SALEX.obj_appear_effect = new Effect.Opacity(SALEX.obj_content_box_2, {duration: 0.6, to: 1.0});
			//SALEX.obj_content_box_1.morph('height:'+SALEX.obj_content_box_2.getHeight()+'px;');
			//setTimeout("SALEX.obj_content_box_1.hide();", 400);
			SALEX.int_active_box = 2;
		} else {
			SALEX.obj_content_box_1.style.height = 'auto';
			SALEX.obj_content_box_1.innerHTML = _obj_ajax_response.responseText;
			//SALEX.obj_fade_effect = new Effect.Opacity(SALEX.obj_content_box_2, {duration: 0.4, to: 0.0});
			SALEX.obj_content_box_1.show();
			SALEX.obj_content_box_2.hide();
			SALEX.obj_appear_effect = new Effect.Opacity(SALEX.obj_content_box_1, {duration: 0.6, to: 1.0});
			//SALEX.obj_content_box_2.morph('height:'+SALEX.obj_content_box_1.getHeight()+'px;');
			//setTimeout("SALEX.obj_content_box_2.hide();", 400);
			SALEX.int_active_box = 1;
		}
		SALEX.obj_fade_loading = new Effect.Opacity(SALEX.obj_loading_box, {duration: 0.4, to: 0.0});
		
		SALEX.updateCategory(_obj_ajax_response.request.url);
		SALEX.updateMenu(_obj_ajax_response.request.url);
		SALEX.updateLogout(_obj_ajax_response.request.url);
		SALEX.updateProductTextileAnime(_obj_ajax_response.request.url);
	},
	
	loadContentFailed: function() {
		alert('Wystapil blad podczas pobierania tresci.');
	},
	
	loadPage: function(_str_url) {
		SALEX.showContentLoader();
		var obj_ajax = new Ajax.Request(_str_url + '&ajax=1', {
			method: 'get',
			onSuccess: SALEX.updateContent,
			onFailure: SALEX.loadContentFailed
		});
	},
	
	updateCategory: function(_str_url) {
		var int_new_cat_id = 0;
		if (_str_url.indexOf('?p=products') > 0 || _str_url.indexOf('?p=product') > 0) {
			var int_pos = _str_url.indexOf('category_id=') + 12;
			var str_tmp = _str_url.substr(int_pos);
			var str_id = '';
			var int_l = str_tmp.length;
			for (var i = 0; i < int_l; i++) {
				if (isNaN(str_tmp[i])) {
					break;
				} else {
					str_id += str_tmp[i];
				}
			}
			int_new_cat_id = str_id;
		}
		
		var arr_categories = $('categories_menu').getElementsByTagName('a');
		var int_a_length = arr_categories.length;
		for (var i = 0; i < int_a_length; i++) {
			if (arr_categories[i].rel == int_new_cat_id) {
				if (!arr_categories[i].parentNode.hasClassName('active')) {
					arr_categories[i].parentNode.addClassName('active');
				}
			} else {
				if (arr_categories[i].parentNode.hasClassName('active')) {
					arr_categories[i].parentNode.removeClassName('active');
				}
			}
		}
	},
	
	updateMenu: function(_str_url) {
		var str_page = 0;
		if (_str_url.indexOf('?p=index') > 0) {
			str_page = 'index';
		}
		if (_str_url.indexOf('?p=textiles') > 0) {
			str_page = 'textiles';
		}
		if (_str_url.indexOf('?p=pricelist') > 0) {
			str_page = 'pricelist';
		}
		if (_str_url.indexOf('?p=contact') > 0) {
			str_page = 'contact';
		}
		
		var arr_categories = $('mainmenu').getElementsByTagName('a');
		var int_a_length = arr_categories.length;
		for (var i = 0; i < int_a_length; i++) {
			if (arr_categories[i].rel == str_page) {
				if (!arr_categories[i].parentNode.hasClassName('active')) {
					arr_categories[i].parentNode.addClassName('active');
				}
			} else {
				if (arr_categories[i].parentNode.hasClassName('active')) {
					arr_categories[i].parentNode.removeClassName('active');
				}
			}
		}
	},
	
	updateLogout: function(_str_url) {
		if (_str_url.indexOf('?p=logout') > 0) {
			if ($('logout_link')) {
				$('logout_link').remove();
			}
		}
	},
	
	changeProductPhoto: function(_obj_target) {
		var obj_main_img = $('product_main_img');
		var arr_tmp = _obj_target.getElementsByTagName('img');
		var obj_target_img = arr_tmp[0];
		
		obj_main_img.src = obj_target_img.src;
		obj_main_img.parentNode.href = _obj_target.href;
	},
	
	showProductPhoto: function() {
		var obj_main_img = $('product_main_img');
		var arr_photos = $('product_photos').getElementsByTagName('a');
		var int_l = arr_photos.length;
		
		for (var i = 0; i < int_l; i++) {
			if (arr_photos[i].hasClassName('hidden_lb_photo') && obj_main_img.parentNode.href == arr_photos[i].href) {
				window.lightbox.start(arr_photos[i]);
			}
		}
	},
	
	productsTextilesAnime: function(_bool_next) {
		var obj_textiles = $('product_textiles');
		if (obj_textiles) {
			var arr_textiles = obj_textiles.getElementsByTagName('a');
			if (arr_textiles.length > 4) {
				if (typeof _bool_next == 'undefined') _bool_next = false;
				var obj_first_textile = arr_textiles[0];
				if (_bool_next) {
					obj_first_textile.remove();
					obj_first_textile = arr_textiles[0];
				} else {
					clearTimeout(window.prod_text_anim_timer);
				}
				obj_textiles.insert(obj_first_textile.cloneNode(true));
				Effect.SlideUp(obj_first_textile);
				setTimeout("SALEX.productsTextilesAnime(true);", 4000);
			}
		}
	},
	
	updateProductTextileAnime: function(_str_url) {
		if (_str_url.indexOf('?p=product') > 0 && _str_url.indexOf('?p=products') <= 0) {
			window.prod_text_anim_timer = setTimeout("SALEX.productsTextilesAnime(false);", 4000);
		}
	}
	
};



document.observe('click', (function(event){
	var target = event.findElement('a');
	if (target) {
		if (target.href.indexOf('?p=') > 0 && target.href.indexOf(window.location.host) > 0) {
			event.stop();
			SALEX.loadPage(target.href)
		} else if (target.rel == 'salex_product_photo') {
			event.stop();
			SALEX.changeProductPhoto(target);
		} else if (target.rel == 'salex_product_main_photo') {
			event.stop();
			SALEX.showProductPhoto();
		}
	}
}));
