$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});

function show_message(message, color) {

	$('#message_box').hide();

	$('#message_box').css({'color':color});

	$('#message_box').html(message);

	$('#message_box').fadeIn('slow');

	setTimeout("$('#message_box').fadeOut('slow');",8000);

}

function trim(str, chars) {

	return ltrim(rtrim(str, chars), chars);

}

 

function ltrim(str, chars) {

	chars = chars || "\\s";

	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");

}

 

function rtrim(str, chars) {

	chars = chars || "\\s";

	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");

}


$(document).bind('hashChange', function(e, newHash){								
		var loc = $.URLDecode(newHash);
		var type = loc.substr(1,4);
		loc = loc.substr(6,loc.length)			
		
		if (type=='menu') {
			$('#'+loc).click();					
		}
		
		if (type=='site') {			
//			alert(loc);
//			location = loc;		
			$('#content').html('Se incarca...');
		
			var locatie = loc;								
			
			$.get(loc,{},function(response){ 
		
				$('#content').html(response);								
				$('#bottom_p').html('');						
				$('#top_p').html('');						
				$('#left_p').hide();						
				$('#right_p').hide();
				
				window.location.hash = 'site_' + $.URLEncode(locatie);						
			})
		}
		
//		location = loc;	
});

$(document).ready(function(){	
	if (window.location.hash !='') {		
		var loc = $.URLDecode(window.location.hash);
	
		var type = loc.substr(1,4);
		loc = loc.substr(6,loc.length)				
		
		if (type=='menu') {
			$('#'+loc).click();			
		}
		
		if (type=='site') {		
			loc = 'http://' + loc.substr(7,loc.length);	
			//if (loc.substr(1,2)=='/') loc=loc.substr(2,loc.length);
			//alert(loc);
			window.location = loc + '&redirect=1';
		}
	}
		
	var last = $('#first');				   		
	
	$('#menu_content a').mouseover(function() {
		if (this.href!=last.href) {
	   		$(this).css('background-color','#000');
		}
	});
	
	$('#menu_content a').mouseout(function() {
		if (this.href!=last.href) {										   
			$(this).css('background','none');										  
		}
	});	
	
	$('#menu_content a').bind('click',function(event){
	
		event.preventDefault();				
		
		if (last!=null)
				$(last).css('background','none');
		last = this;


		$(this).css('background-color','#C00');
								
				
		$('#content').html('Se incarca...');
		
		var link = this.href;	
		if (this.id=="fifth") window.location = 'http://catalog.afaceri-press.ro';
		window.location.hash = 'menu_' + this.id;	
		
		
		$.post('../includes/ajax.php?action=load_submenu',{ 'link':link },function(data){										
					if (data.valid == 'true') {
						$.get('../submenu/' + data.file_name,{},function(response){ 																		
							$('#dmenu').html(response);					


							if (($('#dmenu a').length>0) && !($('#no_ajax a').length>0))
				
								$('#dmenu a').bind('click',function(event){
				
									event.preventDefault();
				
									$('#content').html('Se incarca...');
				
									var locatie = this.href;					
									
				//					$('#content').width(1000);
									
									$.get(this.href,{},function(response){ 
				
										$('#content').html(response);								
										$('#bottom_p').html('');						
										$('#top_p').html('');						
										$('#left_p').hide();						
										$('#right_p').hide();
					
										window.location.hash = 'site_' + $.URLEncode(locatie);
					
										if ($('#content_table_cell').length>0) {
				//							$('#content_table_cell').width(1000);	
									//		$('#content').width(1000);
										}
									})
				
								});		
							
							$('#dmenu').show();														
						});		
					} else {						
						$('#dmenu').html('');
						$('#dmenu').hide();	
					}
		},"json");
		
		$.get(this.href,{},function(response){ 

			$('#content').html(response);	

			if ($('#content').length>0) {				

				$.post('../includes/ajax.php?action=check_valid',{ 'link':link, 'identifier': 'top_p.php'},function(data){										
					if (data.valid == 'true') {
						$.get('../pages/top_p.php',{},function(response){ 
							$('#top_p').html(response);					
							$('#top_p').show();							
							
							//$('#sizert').html(response);
							//var blockheightt = $('#sizert').height();						
							//$('#top_p').height(eval(blockheightt-50));
							//alert($('#top_p').height());
						});		
					} else {						
						$('#top_p').html('');
					}
				},"json");
								
				$.post('../includes/ajax.php?action=check_valid',{ 'link':link, 'identifier': 'bottom_p.php'},function(data){																													  					if (data.valid == 'true') {
						$.get('../pages/bottom_p.php',{},function(response){ 
							$('#bottom_p').html(response);					
							$('#bottom_p').show();							
						});		
					} else {
						$('#bottom_p').html('');						
					}
				},"json");
				
				var blockwidthl = 0;	
				var blockwidthr = 0;
				
				$.post('../includes/ajax.php?action=check_valid',{ 'link':link, 'identifier': 'left_p.php'},function(data){																								
					if (data.valid == 'true') {
						$.get('../pages/left_p.php',{},function(response){ 
							$('#left_p').html(response);
							$('#left_p').show();
							
							$('#sizerl').html(response);
							blockwidthl = $('#sizerl').width();	
							//$('#left_p').width(blockwidthl);
							//alert(1000-blockwidthr-blockwidthl);
							if (1000-blockwidthl-blockwidthr==512)	{
//								$('#content').width(1000-blockwidthl-blockwidthr);
//								$('#content_table_cell').width(1000-blockwidthl-blockwidthr);														
							}
						});
					} else {
						$('#left_p').hide();
					}
				},"json");	
				
				$.post('../includes/ajax.php?action=check_valid',{ 'link':link, 'identifier': 'right_p.php'},function(data){																											  					if (data.valid == 'true') {
						$.get('../pages/right_p.php',{},function(response){ 
							$('#right_p').html(response);
							$('#right_p').show();			
							
							$('#sizerr').html(response);
							blockwidthr = $('#sizerr').width();	
							$('#right_p').width(blockwidthr);
							//alert(1000-blockwidthr-blockwidthl);							
							if (1000-blockwidthl-blockwidthr==512)	{
//								$('#content').width(1000-blockwidthl-blockwidthr);
//								$('#content_table_cell').width(1000-blockwidthl-blockwidthr);																						
							}
						});
					} else {
						$('#right_p').hide();					
					}
				},"json");									
			}

			
			if (($('#content a').length>0) && !($('#no_ajax a').length>0))

				$('#content a').bind('click',function(event){
					if ($(this).attr('rel')!='no_ajax') {
						event.preventDefault();
	
						$('#content').html('Se incarca...');
	
						var locatie = this.href;					
						
	//					$('#content').width(1000);
						
						$.get(this.href,{},function(response){ 
	
							$('#content').html(response);								
							$('#bottom_p').html('');						
							$('#top_p').html('');						
							$('#left_p').hide();						
							$('#right_p').hide();
		
							window.location.hash = 'site_' + $.URLEncode(locatie);
		
							if ($('#content_table_cell').length>0) {
	//							$('#content_table_cell').width(1000);	
						//		$('#content').width(1000);
							}
						})
					}
				});		
				

		})

	});


	$('#cautare_frm').ajaxForm({ 
		target:        '#content', 

        success:   function(data) {
			$('#bottom_p').html('');						
			$('#top_p').html('');					
			$('#left_p').hide();						
			$('#right_p').hide();
			
			if ($('#content_table_cell').length>0) {
//				$('#content_table_cell').width(1000);
//				$('#content').width(1000);
			}
				
			if ($('#content a').length>0)

				$('#content a').bind('click',function(event){

					event.preventDefault();	

					$('#content').html('Se incarca...');			
					window.location = this.href;
					/*$.get(this.href,{},function(response){ 
					//	alert(response);						
						$('#content').html(response);								
						$('#bottom_p').html('');						
						$('#top_p').html('');						
						$('#left_p').hide();						
						$('#right_p').hide();
						
						if ($('#content_table_cell').length>0) {
							$('#content_table_cell').width(1000);												
//          					$('#content').width(1000);
						}
					})*/

				});
		}
    }); 
	

	$('#subscr_form').ajaxForm({ 

        dataType:  'json',  

        success:   function(data) {

			show_message(data.message, data.color);

		}

    }); 			



	$('#subscr_email').click(function(event) {

		this.select();								  

	});

	

	if (trim($('#content').html()) == '') {

		$('#first').click();						

	}

});
