function showProposalForm() {
	document.getElementById('proposalContainer').style.display = "block";
	if (!Inviko._proposalPanel) {
		Inviko._proposalPanel = new YAHOO.widget.Panel('proposalContainer', {
			width : "480px",
			fixedcenter : true,
			visible : false,
			constraintoviewport : true
		});
		Inviko._proposalPanel.render();
	}
	Inviko._proposalPanel.show();
}

var dialog;
$(function(){
	dialog = $('a[rel]').overlay({expose: { 
        color: '#333', 
        loadSpeed: 200, 
        opacity: 0.9 
    }, 
    
    onLoad: function()
    {
    	$('#message').focus();
    },
    
    closeOnClick: false, api: true });
	
	selectProposalFromLocation();
	
	$('div.scrollable').scrollable({size: 1});
	
	// select the thumbnails and make them trigger our overlay
	$("#triggers a").overlay({
	 
		// each trigger uses the same overlay with the id "gallery"
		target: '#gallery',
	 
		// optional exposing effect
		expose: '#f1f1f1'
	 
	// let the gallery plugin do its magic!
	}).gallery({
	 
		// the plugin accepts its own set of configuration options
		speed: 800
	});
	
});

function goForIt() {
	if (confirm("Confirm that your proposal is right and you wish to share your name, city and email address with the deal owner!")) {
		var callback = {
			success : function(o) {
				dialog.close();
				if (o !== undefined) {
					var response = YAHOO.lang.JSON.parse(o.responseText);
					if (response.responseType === '[__Success]') {
						document.getElementById('btnGo').style.display = "none";
					}
					
					alert(response.message);
				}
			},
			failure : function(response) {
				alert("Oops! Something wrong happened. Please try again!");
			}
		};

		var postData = "id=" + Inviko._dealId + "&proposal="
				+ document.getElementById("proposal").value;
		var transaction = YAHOO.util.Connect.asyncRequest("POST",
				webroot + 'deals/goForDeal/', callback, postData);
	}
}

function sendMessage(){
	var button = $('#btnSendMessage') 
	button.replaceWith('<img id="workingSprite" src="' + webroot + 'img/working.gif" alt="Working please wait..."></img>');
	$.getJSON(webroot + "messages/newMessage", {
		deal_id : Inviko._dealId,
		message : $('#message').val(),
		deal_owner_id : $('#dealOwnerId').val()
	},function(data){
		$('#workingSprite').replaceWith(button);
		dialog.close();
		if(data.message){
			alert(data.message);
		}
		else{
			alert(data);
		}
	});
}

function showProposals(callback){	
	var b = $('#btnGo');
	if(!b.hasClass('current-button')){
		$('#proposalsContainer').append('<img id="workingSprite" src="' + webroot + 'img/working.gif" alt="Working please wait..."></img>');
		$.getJSON(webroot + 'proposals/getProposalViews/' + Inviko._dealId, function(data){
			
			var size = data.length;
			
			if(size == 0){
				alert('Sorry but no proposals have been made yet!');
				return false;
			}
			$('#workingSprite').remove();
			
			var offset = b.offset();
			var height = b.height();
			var width = b.innerWidth();
			
			b.addClass('hasArrow');
			
			var arrowHalfWidth = 13 + 6;
					
			$('#btnGo').addClass('current-button');//.parent().append('<span class="green-arrow-down" style="display:none;" id="downArrow"></span>');
			
			$('#downArrow').slideDown('slow');
			$('#proposalsContainer').slideDown('slow');
			
			debug = data;
			
			var tabHeader = $('#tabHeader');
			var id = 5;
			var content = $('#tabContent');
			
			
			
			var cssClass = 'class="rgb"'; 
			
			for(var i = 0; i < size; i++){
				var item = data[i];
				var id = item.ProposalView.id;			
			
				tabHeader.append('<li ' + (i != (size - 1) ? cssClass : '') + '><a href="#proposal_' + id + '">' + 
						item.Profile.first_name + ' from ' + item.Profile.city + '</a><li>');
				
				content.append('<div id="proposal_' + id + '" style="width: 100%;"><label class="pretty-date inv-title-small" title="' + 
						Inviko.formatToISODate(item.ProposalView.created) + '">Submited </label><br></br><label>' + 
						item.ProposalView.proposal + '</label><div><div class="fr"><a class="inv-nice-button green ' + 
						'small sr">Review ' + item.Profile.first_name + '\'s Profile </a></div></div></div>');
				renderPaypalButton(id, item.Deal, item.Profile);
			}
			
			$('.pretty-date').prettyDate();
			
			tabs = $("ul.inv-tabs").tabs("div.inv-tab-content > div", {
				current: 'inv-tab-current', 
				api: true, 
				initialIndex : 0, 
				effect: 'fade'		
			});
			
			if(callback){
				callback.call();
			}
		});		
	}
}

function acceptProposal(proposalId){
	
}

function selectProposalFromLocation(){
	var proposalId = document.location.href.split('#')[1];
	if(proposalId){
		showProposals(function(){$('a[href="' + proposalId +'"]').click();});		
	}
}

function renderPaypalButton(proposalId, deal, profile){
	$('#proposal_' + proposalId).append('<div class="fr sr"><form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">' +
		'<input type="hidden" name="cmd" value="_xclick">' +
		'<input type="hidden" name="hosted_button_id" value="11157088">' +
		'<input type="hidden" name="notify_url" value="http://' + httpHost + webroot + 'payments/paymentCallback"></input>' +
		'<input type="hidden" name="amount" value="' + deal.commission + '"></input>' +
		'<input type="hidden" name="item_name" value="Commission on escrow for '+ deal.title + '"></input>' +
		'<input type="hidden" name="item_number" value="' + deal.id + '"></input>' +
		'<input type="hidden" name="currency_code" value="USD"></input>' +
		'<!-- <input type="hidden" name="image_url" value="http://inviko.com/img/logo.png"></input> -->' +
		'<input type="hidden" name="no_shipping" value="1"></input>' +
		'<input type="hidden" name="return" value="http://' + httpHost + webroot + 'payments/paymentCallback"></input>' +
		'<input type="hidden" name="rm" value="2"></input>' +
		'<input type="hidden" name="cbt" value="Return to Inviko"></input>' +
		'<input type="hidden" name="cancel_return" value="http://' + httpHost + webroot + 'payments/cancelPayment"></input>' +
		'<input type="hidden" name="custom" value="' + userId + '"></input>' +
		'<input type="hidden" name="charset" value="utf-8"></input>' +
		'<input type="hidden" name="business" value="vidal_1263475438_biz@inviko.com"></input>' +
		'<a class="inv-nice-button small green" onclick="if(confirm(\'In order for us to give you the personal information of ' + 
		profile.first_name + ' you need to put the commission on escrow. This amount will be saved until the deal is closed and then delivered to ' + profile.first_name + 
		'. If the deal is not closed, you can always request your money back\')){ this.parentNode.submit(); }else{ return false; }">Accept and Engage</a>' +
	'</form></div>');
}