var refresh_page = false;

jQuery.shareDialog = function(type,data,options){
	var input_options = options.input; // allow: 1=usernames, 2=emails, 3=both
	
	var group_playlist = function() {
		this.link = DOMAIN_PLAYLIST+'/'+data.enid+'?invite='+data.invite_code;
		this.type = 'group_playlist';
		this.title = 'Invite friends to this group playlist';
		// Message preview (actual sent message is in views/emails/share_group_playlist_html.tpl)
		var action_word = (data.is_owner?'created':'joined');
		this.message = 'I just '+action_word+' a group playlist called <a href="'+this.link+'">'+data.playlist_title+'</a>, '+
					   'and I need your help in making it even better. It\'s a special kind of playlist, one that lets me and '+
					   'you and all of our friends add songs to it until it\'s perfect and then share it with the world '+
					   '(and each other, of course). Everyone in the group can comment on each other\'s picks, so make sure they\'re good!';
		this.success = 'Success! You invited friends to your group playlist.';
		showBoxy();
	}
 
	var song = function() {
		var link = 'http://playlist.com/playlist/additem/'+data.enid;
		this.type = 'song';
		this.title = 'Send '+data.song+' by '+data.artist+' to some friends';
		this.subject = 'Check out this song!';
		this.message = 'Hey, I found a song I think you\'d like. Check it out! <br/><a href="'+link+'">'+link+'</a>';
		this.success = 'Success! You sent a song to some friends.';
		showBoxy();
	}

	var playlist = function() {
		var link = 'http://playlist.com/playlist/'+data.enid;
		this.type = 'playlist';
		this.title = 'Send '+data.playlist_name+' to some friends';
		this.subject = 'Check out this playlist!';
		this.message = 'Hey, I found a music playlist I think you\'d like. Check it out! <br/><a href="'+link+'">'+link+'</a>';
		this.success = 'Success! You sent a playlist to some friends.';
		showBoxy();
	}

	var showBoxy = function(){
		var input_prompt = 'Type a';
		if(input_options==1||input_options==3) input_prompt += ' username';
		if(input_options==3) input_prompt += ' or';
		if(input_options==2||input_options==3) input_prompt += ' email address';
		input_prompt += ', then hit return.';
		
		var content = '<div class="boxy_content">'+
					 	'<div class="title">' + this.title + '</div>'+
					  	'<div class="noresults">To select users you must follow them first. Your not following anyone!</div>'+
						'<div class="success_inline" style="display:none; margin:0 0 4px 0; padding:4px; background: #95C210; color:#fff;"></div>'+
						'<div class="invalid_users" style="display:none; margin:0 0 6px 0; padding:4px; background: #DF4729; color:#fff;"></div>'+
					  	'<form id="shareForm" accept-charset="utf-8">'+
					  		'<ol><li id="share-list" class="input-text">'+
					  		'<input type="text" value="" id="share-input" />'+
					  		'<div id="share-auto">'+
					  			'<div class="default">' + input_prompt + '</div>'+
					  			'<div class="search_message">Searching friends...</div>'+
					  			'<ul id="feed"></ul>'+
							'</div></li></ol>'+
							'<div class="message">' + this.message + '</div>'+
							'<input type="hidden" name="enid" value="' + data.enid + '"/>'+
							'<input type="hidden" name="invite_code" value="' + data.invite_code + '"/>'+
							'<input id="invite_submit" type="submit" value="Send"/>'+
			                '<input type="hidden" name="enable_group" value="' + (data.enable_group ? 1 : 0) + '" />' +
							'<span class="invite_field_label">or share this invite link</span>'+
							'<input class="invite_field" type="text" value="'+this.link+'" />';
						'</form>'+
					  '</div>';

		jQuery('.boxy-wrapper').remove(); // Remove all existing lightboxes
		var lightbox = new Boxy(content,{
			//title: this.title,
			closeable: true, 
			closeText: 'X',
			center: true,
			fixed: false,
			modal:true,
			hideFade: true,
			hideShrink: false,
			unloadOnHide:true,
			afterShow: function () {
				setupShareDialog(this);
				return false;
			}
		});
	}

	var setupShareDialog = function(lightbox){
		var success = this.success;
		var users_selected = false;
		
	   	jQuery.facebooklist(lightbox,'#share-input', '#preadded', '#share-auto',{url:'/async/buddylist/fetch?show=json',cache:1}, 6, {userfilter:1,casesensitive:0},0,input_options);

		// Select share url when input is in focus
		$('.invite_field').focus(function(){
			this.select();
		});
	}

	switch(type){
    	case 'group_playlist': group_playlist(); break;
    	case 'playlist': playlist(); break;
    	case 'song': song(); break;
    }

}

shareDialog = function(type,data,options){
	jQuery.shareDialog(type,data,options);
}




jQuery.confirmDialog = function(type,data,options) {
	var purchase = function() {
		this.type = 'confirm_purchase';
		this.title = '';
		if(data.item_name)
			item_name = '"' + data.item_name + '"';
		else
			item_name = "this track";
		
		var content = '<form id="confirm"><div class="container"><img src="/img/ecom/exclamation.png" /><div class="prompt"><h3>Are you sure you want to buy and download ' + item_name + '?</h3>'
		+ '<div class="text">Your credit card will be charged for this purchase and purchase will begin to download immediately.</div>'
		+ '<div class="buttonContainer"><button type="button" id="confirm_cancel" class="grey"><span>Cancel</span></button>'
		+ '<button type="submit" class="orange" id="confirm_continue" style="margin-left: 10px;"><span>Continue</span></button></div></div></div></form>';


		jQuery('.boxy-wrapper').remove(); // Remove all existing lightboxes
		var lightbox = new Boxy(content,{
			//title: this.title,
			closeable: true, 
			closeText: 'X',
			center: true,
			fixed: false,
			modal:true,
			hideFade: true,
			hideShrink: false,
			unloadOnHide:true,
			behaviours: function(c) {
				c.find('#confirm_continue').click(function () {
					js_purchase({
						user: data.user,
						track_enid: data.track_enid,
						tracklink_enid: data.tracklink_enid,
						price: data.price,
						sig: data.sig,
						timestamp: data.timestamp
					}),
					Boxy.get(this).hide();
					return false;
				})
				c.find('#confirm_cancel').click(function () {
					Boxy.get(this).hide();
					return false;
				})

			}
		});
	}

	var playlist_purchase = function() {
		this.type = "confirm_purchase";
		this.title = '';

		var content = '<form id="confirm_playlist"><div class="container"><div class="prompt"><h3>Songs available for purchase in "' + unescape(data.item_name) + '"</h3>'
		+ '<div id="tracklist" style="display: block;"></div>'
		+ '<div class="buttonContainer"><button type="button" id="confirm_cancel" class="grey"><span>Cancel</span></button>'
		+ '<button type="submit" class="orange" id="confirm_continue" style="margin-left: 10px;"><span>Purchase</span></button></div></div></div></form>';

		jQuery('.boxy-wrapper').remove(); // Remove all existing lightboxes
		var lightbox = new Boxy(content,{
			//title: this.title,
			closeable: true, 
			closeText: 'X',
			center: true,
			fixed: false,
			modal:true,
			hideFade: true,
			hideShrink: false,
			unloadOnHide:true,
			behaviours: function(c) {
				c.find('#confirm_continue').click(function () {
					tracks = [];
					i = 0;
					checked_items = jQuery("#confirm_playlist :checked");
					for(i=0;i < checked_items.length;i++ ) {
						tracks.push(checked_items[i].value);
					}

					js_purchase_playlist({user: data.user,
						playlist_id: data.playlist_id,
						tracklink_enid: tracks,
						item_name: data.item_name,
						price: document.getElementById("total_price") * 100,
						user: data.user,
						sig: document.getElementById("sig").value,
						timestamp: document.getElementById("timestamp").value}
					);
						
					Boxy.get(this).hide();
					return false;
				})
				c.find('#confirm_cancel').click(function () {
					
					Boxy.get(this).hide();
					return false;
				})

			}
		});
	}

	var showBoxy = function(){
		var content = '<form id="confirm"><div class="container"><img src="/img/ecom/exclamation.png" /><div class="prompt"><h3>Are you sure you want to buy and download "' + data.item_name + '"?</h3>'
		+ '<div class="text">Your credit card will be charged for this purchase and purchase will begin to download immediately.</div>'
		+ '<div class="buttonContainer"><button type="button" id="confirm_cancel" class="grey"><span>Cancel</span></button>'
		+ '<button type="submit" class="orange" id="confirm_continue" style="margin-left: 10px;"><span>Continue</span></button></div></div></div></form>';


		jQuery('.boxy-wrapper').remove(); // Remove all existing lightboxes
		var lightbox = new Boxy(content,{
			//title: this.title,
			closeable: true, 
			closeText: 'X',
			center: true,
			fixed: false,
			modal:true,
			hideFade: true,
			hideShrink: false,
			unloadOnHide:true,
			behaviours: function(c) {
				c.find('#confirm_continue').click(function () {
					js_purchase(data.user, data.item_enid, data.price, data.sig, data.timestamp);
					Boxy.get(this).hide();
					return false;
				})
				c.find('#confirm_cancel').click(function () {
					Boxy.get(this).hide();
					return false;
				})

			}
		});
	}


	switch(type){
    	case 'purchase': purchase(); break;
	case 'playlist_purchase': playlist_purchase(); break;
	}

}

confirmDialog = function(type, data, options) {
	jQuery.confirmDialog(type, data, options);
}
