function stripslashes (str) {   
    return (str + '').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
        case '\\':
            return '\\';
        case '0':
            return '\u0000';
        case '':
            return '';
        default:
            return n1;
        }
    });
}

function addResToSidebar(res_id, res_name, res_guid) {
	var html = [];
	html.push('<li>');
	html.push('<a href="/restaurant/', res_guid, '" title="', res_name, '">', res_name ,'</a>');
	html.push('<a href="', res_id, '" title="Delete" class="del-btn" /></li>');
	$(html.join('')).appendTo('#restaurant-cart');
}

function loadResInfo(res_id) {
	//$('#waiting-panel').height( $('.inner-top3 .rt .left').height() );
	$('#waiting-panel').show();
	$.ajax({
		url: '/resultpage-ajax.php',
		type: 'post',
		dataType: 'json',
		data: 'id=' + res_id,
		success: function(res) {
			if(res.error)
				alert(res.code);
			else {
				//run picture slides				
				runSlide( res.data.pictures );

				//set head bar
				var html =[];
				html.push('<h2>', res.data.name,'<span>|</span>   <span class="noclass-city">', res.data.city,'</span></h2>');
				html.push('<p><em class="noclass-baseline">', res.data.baseline, '</em></p>');
				$('#res-basicinfo').html(html.join(''));				
				$('#res-id').val(res.data.id);
				$('#res-name').val(res.data.name);
				$('#res-guid').val(res.data.guid);

				$('#share-button').attr('href', 'http://www.facebook.com/sharer.php?s=100&p[title]=' + encodeURIComponent(res.data.name) + '&p[summary]=' + encodeURIComponent(res.data.baseline) + '&p[url]=' + encodeURIComponent('http://majestictech.co.in.cp-18.webhostbox.net/restaurant/' + res.data.guid));
				$('#pdf-button').attr('href', '/pdf.php?template=restaurant&id=' + res.data.id);
				$('#print-button').attr('href', '/print.php?template=restaurant&id=' + res.data.id);
				
				//fill restaurant tab
				$('#tab-restaurant').html('<div class="info">' + res.data.general_info + '</div>');	
				
				//fill Praktisch tab
				var html =[];
				html.push('<div class="left-part">');
				html.push('<p>', res.data.chief_name,'</p>');
				html.push('<p>', res.data.address,'</p>');
				html.push('<p>', res.data.postal_code, ' ', res.data.city, '</p>');
				html.push('<p>&nbsp;</p>');
				if( res.data.tel != '' )
					html.push('<p>Tel: ', res.data.tel, '</p>');
				if( res.data.mobile != '' )
					html.push('<p>GSM: ', res.data.mobile, '</p>');
				if( res.data.fax != '' )
					html.push('<p>Fax: ', res.data.fax,'</p>');
					
				html.push('<p>&nbsp;</p>');
				if( res.data.site_url != '' )
					html.push('<p><u>', res.data.site_url,'</u></p>');
				if( res.data.email != '' )
					html.push('<p><u>', res.data.email, '</u></p>');
				html.push('</div>');
				html.push('<div class="right-part">', res.data.more_info, '</div>');				
				$('#tab-prak').html(html.join(''));						




				//fill Contact tab
				var html =[];
				html.push('<div class="success" style="display: none;">Bedankt voor uw contact.</div>');
				html.push('<div class="left-part">');
				html.push('<p>', res.data.chief_name,'</p>');
				html.push('<p>', res.data.address,'</p>');
				html.push('<p>', res.data.postal_code, ' ', res.data.city,'</p>');
				html.push('<p>&nbsp;</p>');
				if( res.data.tel != '' )
					html.push('<p>Tel: ', res.data.tel, '</p>');
				if( res.data.mobile != '' )
					html.push('<p>GSM: ', res.data.mobile, '</p>');
				if( res.data.fax != '' )
					html.push('<p>Fax: ', res.data.fax,'</p>');
				html.push('<p>&nbsp;</p>');
				if( res.data.site_url != '' )
					html.push('<p><u>', res.data.site_url,'</u></p>');
				if( res.data.email != '' )
					html.push('<p><u>', res.data.email, '</u></p>');

				html.push('</div>');
				html.push('<div class="right-part">');
				
				if( res.data.email != '' )
				{
					html.push('</div>');
					html.push('<form id="form1" name="form2" method="post" style="width:400px;" class="input-form">');
					html.push('<input type="hidden" name="action" value="contact" />');
					html.push('<div class="input">');
					html.push('<label for="name">Naam</label>');
					html.push('<div><span class="input_span"><input type="text" name="name" class="text" /></div>');
					html.push('<div class="clear"></div>');
					html.push('</div>');
					html.push('<div class="input" >');
					html.push('<label for="tel">Tel</label>');
					html.push('<div><span class="input_span"><input type="text" name="tel" class="text" /></div>');  
					html.push('</div>');
					html.push('<div class="input">');
					html.push('<label for="email">Email</label>');
					html.push('<div><span class="input_span"><input type="text" name="email" class="text" /></div>');  
					html.push('</div>');
					html.push('<div class="input">');
					html.push('<label for="report">Bericht</label>');
					html.push('<div><textarea name="report" class="textarea"></textarea></div>');  
					html.push('</div>');									
					html.push('<div class="input"><input type="submit" name="Submit" value="verzend" class="submit2"/></div>');
					html.push('<div class="clear"></div>');		
					html.push('</form>');
				}

				html.push('</div>');
				html.push('<div class="clear"></div>');

				$('#tab-contact').html(html.join(''));						



				//active the restaurant tab
				$('.tab-active').removeClass('tab-active');
				$('#tab-restaurant').addClass('tab-active');
				//adjust the right-toolbar height
				$('.result-rightbar').height($('#tab-contents').height() + 40);//including padding
			}
			$('#waiting-panel').hide();
		},
		error: function(res) {
			alert('Sorry! Error occurred while requesting.');
			$('#waiting-panel').hide();
		}
	});
	return false;
}

function searchMore() {
	if( $('#has-more').val() == 0 )
		return false;
	//increase page number
	$('#page').val( parseInt( $('#page').val() ) + 1 );
	//query 
	$.ajax({
		url : '/search-ajax.php',
		type : 'POST',
		dataType: 'json',
		data : $('#form1').serialize() + '&page=' + $('#page').val(),
		success: function(res) {
			if(res.count > 0) {
				var html = [];
				for( var i = 0; i < res.count; i++ ) {
					//strip slashes
					res.data[i].name = stripslashes(res.data[i].name);
					html.push('<li>');
					html.push('<a href="', res.data[i].id, '" class="res-item" title="', res.data[i].name, '">', res.data[i].name);
					html.push('<br/><em>', res.data[i].city, '</em></a>');
					html.push('</li>');
				}
				$('#searched-res').html(html.join(''));
				$('#searched-res li:odd').addClass('nobg');			
			}
			if(res.count < 15)
				$('#has-more').val(0);
		},
		error: function(res) {
			alert('Sorry! Error occurred while requesting.');
		}
	});
	return false;
}

$(function() {
	$('.tab-menu').click( function() {		
		$('.tab-active').removeClass('tab-active');
		$( $(this).attr('href') ).addClass('tab-active');
		$('.result-rightbar').height($('#tab-contents').height() + 40);//including padding
		return false;
	});
	
	$('#restaurant-cart .del-btn').live('click', function() {
		removeResCartItem( $(this).attr('href') );
		$(this).parent().fadeTo("slow", 0, function() {
			$(this).remove();		
		});
		return false;
	});	
	
	//save restaurant
	$('#add-rescart').click(function() {
		var res_id = $('#res-id').val();		
		if( checkDuplication(res_id) != -1 ) {
			//alert('dit restaurant is al toegevoegd aan uw lijst met favoriete restaurants');
			document.getElementById('look-out-box').style.display = 'block';
			return false;
		}
		
		var res_name = $('#res-name').val();		
		var res_guid = $('#res-guid').val();
		//add the restaurant to the right sidebar
		addResToSidebar(res_id, res_name, res_guid);		
		//add the restaurant to the cookie
		addResCartItem( res_id, res_name, res_guid, $('.noclass-city').html(), $('.noclass-baseline').html() );		
		return false;
	});
	$('.res-item, .res-cartitem').live('click', function() {		
		loadResInfo( $(this).attr('href') );
		//show selected
		$('li.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		return false;
	});
	
	//read restaurants from cookie
	var cart_list = getResCartList();		
	for( var i = 0; i < cart_list.length; i++ ) {	
		if( cart_list[i] > 0 ) {
			var info = getResCartItem( cart_list[i] );
			addResToSidebar( cart_list[i], info[0], info[3] );
		}
	}
	
	//contact form
	$('#tab-contact .submit2').click(function() {
		$('#waiting-panel').height( $('.inner-top3 .rt .left').height() );
		$('#waiting-panel').show();
		$.ajax({
			url : '/resultpage-ajax.php',
			type : 'POST',
			dataType: 'json',
			data : $('#tab-contact form').serialize() + "&res_name=" + $('#res-name').val() + "&res_email=" + $('#res-email').val(),
			success: function(res) {
				$('#waiting-panel').hide();
				if(res.error > 0) {
					alert(res.code);
				} else {
					$('#tab-contact .success').show();
				}				
			},
			error: function(res) {
				$('#waiting-panel').hide();
				alert('Sorry! Error occurred while requesting.');
			}
		});
		return false;
	});		
	
	//adjust the right-toolbar height
	$('.result-rightbar').height($('#tab-contents').height() + 40);//including padding
});
