/*
 * Ext JS Library 2.2.1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

var review_win;
var review_popup_rating;

Ext.onReady(function(){
	Ext.QuickTips.init();
    
    review_win = new Ext.Window({
		title: 'Review and Rate',
		cls: 'feedback',
		id: 'review_popup',
		border: false,
		bodyBorder: false,
		bodyStyle: 'padding-left: 13px; padding-right:13px;',
		shadow: false,
		resizable: false,
        width       : 364,
        height      : 284,
        closeAction :'hide',
		layout: 'fit',
		items: {
			xtype: 'form',
			cls: 'feedback-form',
			id: 'review_form',
			border: false,
			labelAlign: 'left',
			labelWidth:40,
			bodyBorder: false,
			autoHeight: true,
			defaults: {hideMode: 'offsets'},
			items: [{
		        xtype: 'hidden',
	            name: 're_bz_id',
	            id: 're_bz_id'
        	},{
		    	border: false,
        		html:'Rating: <div id="review_popup_rating"><input type="radio" id="review_popup_rating1" name="review_popup_rating" value="1" title="1" /><input type="radio" id="review_popup_rating2" name="review_popup_rating" value="2" title="2" /><input type="radio" id="review_popup_rating3" name="review_popup_rating" value="3" title="3" /><input type="radio" id="review_popup_rating4" name="review_popup_rating" value="4" title="4" /><input type="radio" id="review_popup_rating5" name="review_popup_rating" value="5" title="5" /></div>',
        		bodyStyle: 'margin-top:5px;margin-bottom:5px;background:transparent;'
        	},{
		    	border: false,
        		html:'Review:',
        		bodyStyle: 'margin-bottom:4px;padding:3px 3px 3px 0;'
        	},{
		        xtype: 'textarea',
	            hideLabel: true,
	            name: 're_review',
	            id: 're_review',
	            width: 320,
	            height: 135
	        }],
			buttonAlign: 'right',
			buttons: [{
				minWidth: 40,
				icon: g_params["static_media_domain"]+'/public/global/popup/btn-send-message.png',
				cls: "send-btn x-btn-text-icon",
				handler: function() {
					if (Ext.getCmp("re_review").getValue().length<10){
				  		Ext.MessageBox.alert('Warning','You must enter a review of at least 10 characters in addition to rating the business.');
					}
					else if (!review_popup_rating.getValue() || review_popup_rating.getValue()<1){
				  		Ext.MessageBox.alert('Warning','You must give a rating of at least 1 star.');
					}
					else {
						$('.x-panel-footer', $('#review_form')).html('<div class="loading-indicator">Sending...</div>');
						$('.x-panel-footer', $('#review_form')).hide();
						Ext.Ajax.request({
							url: g_params["base_domain"]+'/guides/&set_review',
							method: 'post',
							params:{
								're_bz_id':Ext.getCmp("re_bz_id").getValue(),
								're_review':Ext.getCmp("re_review").getValue(),
								're_rating':review_popup_rating.getValue()
							},
							callback: function(options, success, response){
								if (success==true){
									//$('.x-panel-footer', $('#review_form')).hide();
			                        review_win.hide();
									Ext.MessageBox.show({
					   					title:'Success',
					   					msg: 'Your review and rating were saved successfully.',
										buttons: Ext.MessageBox.OK,
									   width: 364,
									  cls:'feedbackm'
									});
									location = location.href;
								}
							}
						}); 
		        	}
		        	
		        }
            },{
				minWidth: 50,
				icon: g_params["static_media_domain"]+'/public/global/popup/btn-cancel.jpg',
				cls: "cancel-btn x-btn-text-icon",
                handler  : function(){
                    review_win.hide();
                }
            }]
		}
    });
	review_win.show();
	review_win.hide();
});

function review_popup(biz_id){
	if (fake_user)
		open_login_popup(location.href);
	else {
		review_win.show();
		review_win.center();
		Ext.Ajax.request({
			params:{'bz_id':biz_id},
			method: 'post',
			url: g_params["base_domain"]+'/guides/&get_review',
			callback: function(options, success, response){
				var review_results = eval('(' + response.responseText + ')');
				Ext.getCmp("re_bz_id").setValue(review_results.re_bz_id);
				Ext.getCmp("re_review").setValue(review_results.re_review);
				if (review_results.re_rating>0)
					document.getElementById("review_popup_rating"+review_results.re_rating).checked = true;
				review_popup_rating = new Ext.ux.Rating('review_popup_rating');
			}
		});
	}
}

function delete_review(biz_id){
	if (fake_user)
		open_login_popup(location.href);
	else {
		var x = window.confirm("Are you sure you want to delete this review?");
		if (x){
			Ext.Ajax.request({
				params:{'re_bz_id':biz_id},
				method: 'post',
				url: g_params["base_domain"]+'/guides/&delete_review',
				callback: function(options, success, response){
					location = location.href;
				}
			});
		}
	}
}