// Namespace dla modułu CMS
EDU.Cms = {
		
		_temp: '',
		_temp2: '',
		_temp3: '',
		
		objectTypeIntToString: function(type) {
			return type == 1 ? 'article' : (type == 2 ? 'product' : (type == 3 ? 'media' : type));
		},
		
		objectTypeStringToInt: function(type) {
			return type == 'article' ? 1 : (type == 'product' ? 2 : (type == 'media' ? 3 : type));
		},
		
		/**
		 * Podgląd modelu produktu
		 */
		modelPreview: function(id, permit) {
			EDU.notify('Ładowanie...', false, EDU.NO_BUTTON, true);
			
			new Ajax.Updater('model_preview',
				'/store/product-model/preview/id/' + id,
				{
					method: 'get',
					evalScripts: true,
					onComplete: function() 	{
												new EDU.notifyHide();
												EDU.showAjaxLoginForm('model_preview');
											}
				}
			);
		},
		
		
		/* 
		 * Wczytuje drzewko uprawnień dla grup
		 */		
		groupPreview: function(id, edit) {
			EDU.notify('Ładowanie...', false, EDU.NO_BUTTON, true);
			var url = '/cms/permit/index/group/' + id;
			if (edit == false) {
				url += '/nonedit/1';
			}
			new Ajax.Updater('group_preview',
				url,
				{
					method: 'get',
					evalScripts: true,
					onComplete: function() 	{
												new EDU.notifyHide();
												EDU.showAjaxLoginForm('group_preview');
											}
				}
			);
		},	
		
		
		/* 
		 * Wczytuje drzewko uprawnień dla strefy klienta
		 */		
		areaPreview: function(id) {
			EDU.notify('Ładowanie...', false, EDU.NO_BUTTON, true);
			new Ajax.Updater('area_preview',
				'/cms/permit-area/index/area/' + id,
				{
					method: 'get',
					evalScripts: true,
					onComplete: function() 	{
												new EDU.notifyHide();
												EDU.showAjaxLoginForm('area_preview');
											}
				}
			);
		},		
		
		
		/**
		 * Podgląd strony w widoku z drzewkiem
		 */
		pagePreview: function(id, permit, objectType, offset) {
			EDU.notify('Ładowanie...', false, EDU.NO_BUTTON, true);
			new Ajax.Updater('sidebar_tree_preview',
				'/cms/page/preview/id/' + id + (objectType != null ? '/object_type/' + objectType : '') + (offset != null ? '/offset/' + offset : ''),
				{
					method: 'get',
					evalScripts: true,
					onComplete: function() 	{
												new EDU.notifyHide();
												EDU.showAjaxLoginForm('sidebar_tree_preview');
											}
				}
			);
		},
			
			
		pagePreviewClose: function() {
			EDU.notify('Ładowanie...', false, EDU.NO_BUTTON, true);
			new Ajax.Updater('sidebar_tree_preview',
				'/cms/log/last',
				{
					method: 'get',
					evalScripts: true,
					onComplete: function() {
												new EDU.notifyHide();
												EDU.showAjaxLoginForm('sidebar_tree_preview');
											}
				}
			);
		},
		
		
		pagePreviewCheckProduct: function(pageId, productId) {
			EDU.notify('Aktualizacja...', false, EDU.NO_BUTTON, true);
			
			_temp = productId;
			
			new Ajax.Request(
				'/cms/product/editassign',
				{
					method:		'post',
					parameters: 'page_id=' + pageId + '&product_id=' + productId,
					onSuccess: 	function(originalRequest) {
										if (originalRequest.responseText == '1') {
										   $('product_desc_' + _temp).addClassName('product_starred');
										} else {
										   $('product_desc_' + _temp).removeClassName('product_starred');
										}
										EDU.notifyHide();
										EDU.showAjaxLoginForm(null, originalRequest.responseText);
									}
				}
			);
		},
				
		moveToPosition: function(itemId, baseId, baseName, containerName) {
			
			_temp = baseId;
			
			new Ajax.Request(
				'/cms/page/item-page-list/name/' + baseName +'/id/' + baseId +'/item/' + itemId + '/modelname/'+ containerName,
				{
					method:		'get',
					onSuccess: 	function(originalRequest) {
							EDU.notify(originalRequest.responseText, false, EDU.NO_BUTTON, false);
									}
				}
			);
		},
		
		saveItemOnPosition: function(itemId, baseId, baseName, position, objectType, containerName) {
			
			_temp = baseId;
			_temp2 = objectType;
			_temp3 =containerName;
			
			EDU.notify('Aktualizacja...', false, EDU.NO_BUTTON, true);
			
			new Ajax.Request(
				'/cms/'+ baseName +'/move-to-position/id/' + baseId +'/item/' + itemId +'/position/' + position + '/container/' + containerName,
				{
					method:		'get',
					onComplete: 	function(originalRequest) {
									if (_temp3 != 'page') {
										EDU.Cms.refreshAssignment(_temp3, _temp, _temp2);
									}
									else {
										EDU.Cms.pagePreview(_temp, '', _temp2)
									}
							
									}
				}
			);
		},
		/**
		 * Zapisuje kolejność elementów po zmianie przez drag'n'drop
		
		reorder: function(sortableId, controller, baseId, baseType) {
			var order = Sortable.serialize(sortableId);
			new Ajax.Request('/cms/' + controller + '/reorder',
				{
					method:'post',
					parameters: order + '&base_id=' + baseId + '&base_type=' + baseType
				}
			);
		},
		*/
		
		
		/**
		 * Aktualizuje przypisanie obiektu do strony
		 */
		reassign: function(baseName, controller, baseId, objectId, assign) {
			//alert('baseNAme:' + baseName + ' baseId: ' + baseId + ' controller:' + controller);
			parameters = {
				'objectId'	: objectId,
				'assign'	: assign
			};
			parameters[baseName] = baseId;
			
			new Ajax.Request('/cms/' + controller + '/assign',
				{
					method: 	'post',
					parameters: parameters,
					onSuccess: function(originalRequest){
															//alert(originalRequest.responseText)
															EDU.showAjaxLoginForm(null, originalRequest.responseText);
														}
				}
			);
		},
		
		
		articlePreview: function() {
			$('preview_title').innerHTML 	= $F('title');
			$('preview_lead').innerHTML 	= tinyMCE.getInstanceById('lead').getBody().innerHTML;
			$('preview_content').innerHTML 	= tinyMCE.getInstanceById('content').getBody().innerHTML;
		},
		
		
		productPreview: function() {
			$('preview_name').innerHTML 	= $F('name');
			$('preview_short_description').innerHTML 	= tinyMCE.getInstanceById('short_description').getBody().innerHTML;
			$('preview_long_description').innerHTML 	= tinyMCE.getInstanceById('long_description').getBody().innerHTML;
			$('preview_code').innerHTML 	= $F('code');
			$('preview_price').innerHTML 	= $F('price');
			$('preview_price_old').innerHTML 	= $F('price_old');
		},
		
		
		pageEditPreview: function() {
			$('preview_name').innerHTML 	= $F('name');
			$('preview_content').innerHTML 	= tinyMCE.getInstanceById('content').getBody().innerHTML;
		},
		
		
		getProductAttributeForm: function(productId, modelId, objectId) {
			EDU.notify('Aktualizacja...', false, EDU.NO_BUTTON, true);
			
			parameters = {
				'product_id'	: productId,
				'model_id'		: modelId
			};
			
			new Ajax.Request('/cms/product/get-product-attribute-form',
				{
					method: 	'post',
					parameters: parameters,
					onSuccess:	function(originalRequest) {
									$(objectId).innerHTML = originalRequest.responseText;
									EDU.notifyHide();
									EDU.showAjaxLoginForm(objectId);
								}
				}
			);
		},
		
	
		/**
		* Wybór istniejących obiektow
		*/	
		showAssignObjectGrid: function(type, baseId, baseName)
		{
		    win = new Window({
                className: "alphacube",
                title: "Powiązania", 
                width: 940,
                url: '/cms/' + type + '/assign/baseName/' + baseName + '/baseId/' + baseId, 
                height: 595, 
                destroyOnClose: true, 
                recenterAuto: false,
                showEffect: Element.show,
                hideEffect: Element.hide,
                wiredDrag: true
            }); 
            
            win.setCloseCallback(function(){
                    if (baseName == 'page') {
        				try {
        					if ($('assignment').id == 'assignment'){
        						window.parent.EDU.Cms.refreshAssignment(baseName, baseId, type);
        					}
        				}catch(e) {
        					window.parent.EDU.Cms.pagePreview(baseId, true, type == 'article' ? 1 : (type == 'product' ? 2 : 3));
        				}
        			} else {
        				window.parent.EDU.Cms.refreshAssignment(baseName, baseId, type);
        			}
        			return true;
                });
			
            win.showCenter(true, 140); 
                    
            /*EDU.notify('Ładowanie...', false, EDU.NO_BUTTON, true);
		
			var url = '/cms/' + type + '/assign/baseName/' + baseName + '/baseId/' + baseId;
		
			if ($('existing_' + type) == null) {
				var existing = document.createElement('div');
				existing.setAttribute('id', 'existing_' + type);	
				existing.setAttribute('class', 'existing_frame_div');
				
				var frame = document.createElement('iframe');
				frame.setAttribute('name', 'frame');				
				frame.setAttribute('id', 'existing_frame');		
				frame.setAttribute('src', url);
				
				existing.appendChild(frame);
				
				$('page_container').appendChild(existing);
			} else {
				var existing = $('existing_' + type);
				$('existing_' + type).setStyle({'display' : 'block'});
			}
			
			EDU.notifyHide();
			*/
		},
		
		closeAssignObjectGrid: function(baseName, baseId, type) {
			// $('existing_' + type).setStyle({'display' : 'none'});
			
			if (baseName == 'page') {
				EDU.Cms.pagePreview(baseId, true, objectTypeStringToInt(type));
			} else {
				EDU.Cms.refreshAssignment(baseName, baseId, type);
			}

		},
		
		refreshAssignment: function(baseName, baseId, type, offset) {
			EDU.notify('Aktualizacja...', false, EDU.NO_BUTTON, true);
			
			new Ajax.Request('/cms/' + baseName + '/assignment/' + baseName + '/' + baseId  + (offset != null ? '/object_type/' + type + '/offset/' + offset : ''),
				{
					method: 	'post',
					onSuccess:	function(originalRequest) {
					                //alert(originalRequest.responseText);
									$('assignment').innerHTML = originalRequest.responseText;
									$('tabber_' + EDU.Cms.objectTypeIntToString(type)).addClassName('tabbertabdefault');
									
									var itemsTabber = new tabberObj();
									
									itemsTabber.init(document.getElementById('itemstabber'));
									EDU.initSortables('articles', 'cms', 'article', baseId, baseName);
									EDU.initSortables('products', 'cms', 'product', baseId, baseName);
									EDU.initSortables('media', 'cms', 'media', baseId, baseName);
									EDU.initEditablesMediaDesc(baseId, baseName);
									
									EDU.notifyHide();
									EDU.showAjaxLoginForm('assignment');
								}
				}
			);
		},
		
		unassign: function(baseName, baseId, itemName, itemId) {
			EDU.notify('Usuwanie...', false, EDU.NO_BUTTON, true);
			_temp 	= baseName;
			_temp2 	= baseId;
			_temp3  = itemName;
			new Ajax.Request('/cms/' + itemName + '/unassign/id/' + itemId + '/' + baseName + '/' + baseId,
				{
					method: 	'post',
					onSuccess:	function(originalRequest) {
									EDU.notifyHide();
									if (_temp == 'page') {
										try {
				        					if ($('assignment').id == 'assignment'){
				        						EDU.Cms.refreshAssignment(_temp, _temp2, _temp3);
				        					}
				        				}catch(e) {
				        					EDU.Cms.pagePreview(_temp2, true, _temp3 == 'article' ? 1 : (_temp3 == 'product' ? 2 : 3));
				        				}
									} else {
										EDU.Cms.refreshAssignment(_temp, _temp2, _temp3);
									}
								}
			});
		},
		
		showLogDescription: function(id) {
			win = new Window({
                className: "alphacube",
                title: "Opis", 
                width: 740, 
                height: 400, 
                destroyOnClose: true, 
                recenterAuto: false,
                showEffect: Element.show,
                hideEffect: Element.hide,
                wiredDrag: true
            }); 
            win.getContent().innerHTML = $('description_' + id).innerHTML;
            //win.setContent('description_' + id, true, true);
            			
            win.showCenter(true, 140); 
		}

};