//-- slide
var cid = 1;
var dir = 'prev';  
var html ='';

 Request.HTML.implement({
  
    processHTML: function(text){
        var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
        text = (match) ? match[1] : text;
       
        var container = new Element('div');
       
        return $try(function(){
            var root = '<root>' + text + '</root>', doc;
            if (Browser.Engine.trident){
                doc = new ActiveXObject('Microsoft.XMLDOM');
                doc.async = false;
                doc.loadXML(root);
            } else {
                doc = new DOMParser().parseFromString(root, 'text/html');
            }
            root = doc.getElementsByTagName('root')[0];
            for (var i = 0, k = root.childNodes.length; i < k; i++){
                var child = Element.clone(root.childNodes[i], true, true);
                if (child) container.grab(child);
            }
            return container;
        }) || container.set('html', text);
    }
 });

function AddEvents(){

// slide
   var state = 0;
   var FXslideLeft = new Fx.Slide('clBody', {mode: 'horizontal'});
   FXslideLeft.addEvent('complete', function() {
		$('content').set('text', '');  
		$('content').adopt(html);
		AddEvents();
	   	FXslideLeft.show();

   });
  
   var FXslideRight = new Fx.Tween('clBody', {property: 'left', duration: 500 });
   FXslideRight.addEvent('complete', function() {
			$('content').set('text', '');  
			$('content').adopt(html);
			AddEvents();
			FXslideLeft.show();
	
	   });
  
//request   
   var req = new Request.HTML({
		onSuccess: function(response){		
				if (response == '') return;
				else html = response;
				if (dir == 'prev') {
					if (Browser.Engine.webkit || Browser.Engine.trident)  FXslideLeft.slideOut();
					else FXslideRight.start(state);
				}
				else {
					 FXslideLeft.slideOut();
				}
		},
		onFailure: function(){
			$('content').set('text', 'You request cannot be processed at this time.');
			$('content').set('left', 0);

		}
	});

//onClick
   $('slideLeft').addEvent('click', function(e){
			e.stop();	

			dir = 'next';            		
		    if ( cid > 1 ) {
				//cid--;
				window.location.hash = ((cid > 1)?(cid-1):cid);
	  		    //req.send({url: '/?req=ajax&cid=' + cid +'&dir=' + dir});
			}
		});
	
   $('slideRight').addEvent('click', function(e){
		    e.stop();
		    state = (state == 0 ? 740 : 0);
			dir = 'prev';  
			//req.send({url: '/?req=ajax&cid=' + cid +'&dir=' + dir});
			//cid ++;	
			window.location.hash = (cid+1);
        });		   
		
	$('bookmark').addEvent('click', function() {  	 
	      title = document.title; 
		  url = pathMAIN;
				
				if (window.sidebar) { // Mozilla Firefox Bookmark		
						window.sidebar.addPanel(title, url,"");	}
				else if( window.external ) { // IE Favorite		
						window.external.AddFavorite( url, title); }	
				else if(window.opera && window.print) { // Opera Hotlist		
						return true; }
	});
		
		
}	  

// browsing
function listenHash() {
    var new_cid = window.location.hash.substr(1,window.location.hash.length);
    if ( new_cid != '' && parseInt(new_cid) !=  cid  ) {
		
		 cid = parseInt(new_cid);
		 var req = new Request.HTML({
		  		url: '/?req=ajax&cid=' + (cid-1) +'&dir=prev',
				onRequest: function(){
					$('clBody').fade(0.2);
					//$('content').set('text', ''); 
					
				},
				onSuccess: function(response){		
					$('content').set('text', '');  
					$('content').adopt(response);  
					$('clBody').fade('in');
					AddEvents();
				},
				onFailure: function(){
					$('content').set('text', 'You request cannot be processed at this time.');
				}
			}).send();
    }
}


// INI
Listener = setInterval( "listenHash()", 50 );
window.addEvent('domready', AddEvents);		  

