var tryFBpublish = false;
var FBpublishparams = null;

showLogin = function() {	
	
	/*
	var popup = new Boxy('<div style="width:500px"> <form id="login_form" action="/en/signin" method="post" style="float:left;border:1px solid #eeeeee; padding: 10px 20px 10px 10px; margin-bottom: 15px; width: 250px"> <table > <input type="hidden" name="signin[_csrf_token]" value="bc51467aeb3392b42f138de300c4e855" id="signin__csrf_token" /> <tr > <th> <label for="signin_username"><label for="signin_username">Username or email</label></label> </th> <td style="padding-top: 3px;"> <input type="text" name="signin[username]" id="signin_username" /> </td> </tr> <tr class="" title=\'\'> <th><label for="signin_password">Password</label></th> <td><input type="password" name="signin[password]" id="signin_password" /></td> </tr> <tr class="" title=\'\'> <th><label for="signin_remember">Remember</label></th> <td><input type="checkbox" name="signin[remember]" id="signin_remember" /></td> </tr> <tr> <td colspan="2" style="text-align:right"> <input type="submit" class="popup_button_vert" value="Login" /> </td> </tr> <tr> <td colspan="2" style="text-align:right"> <a class="forgot_password gray" href="/en/password">Forgot Password?</a> </td> </tr> </table> </form> <div style="float:left; width:200px; text-align: center; margin-top: 50px "> <div style="padding-top:15px; margin-left: 25px;float: left">or</div>&nbsp;&nbsp;&nbsp; <img id="fbconnect" style="padding-top: 10px" src="/images/fbconnect.gif" alt="Facebook Connect" border="0" width="107" height="25"/> </div> <div style="clear:both"></div> <div > Don\'t have an account yet ? <a class="green" href="/en/apply">Create an account</a> or <a class="green" href="/en/become-an-artist">Become an artist</a></div> </div> ', 
					{modal: true, 
					title: 'Login to Artsetter' , 
					closeable: true, 
					closeText:'X'});
	
	if(typeof(FB) != 'undefined')	FB.XFBML.parse();
	
	$(".tr_row_error input").tooltip({
		bodyHandler: function() {
			return $(this).parent().parent().attr('title');
		},
		delay: 0,
		showURL: false
	});
	*/
    $('#login_button').fancybox({
           
            'type'		: 'iframe',
            "hideOnContentClick": false,
			'height' : 250,
			'width' : 520
        }).trigger("click");
        
    
}

getEditArtworkPopup = function(id, gallery_id) {
	$.ajax({  
        type: "GET",  
        url: '/en/admin/editartwork/' + gallery_id + '/' + id,
        dataType: "html",  
        success: function(text) { 
		 	var popup = new Boxy(text, {modal: true, title: 'Edit Artwork' , fixed: false, closeable: false, closeText:'X', unloadOnHide: true, beforeUnload: function() { $(document).unbind("popuploaded") } });

			
			jQuery(document).trigger("popuploaded"); 
			
			$(".form_row_error input, .form_row_error select, .form_row_error textarea").tooltip({
				bodyHandler: function() {
					return $(this).parent().attr('title');
				},
				delay: 0,
				showURL: false
			});
			$(".tr_row_error input, .tr_row_error select, .tr_row_error textarea").tooltip({
				bodyHandler: function() {
					return $(this).parent().parent().attr('title');
				},
				delay: 0,
				showURL: false
			});
			
			
			$('#sizeTable').children().children('*:hidden').each(function() {
				if($(this).children('.form_row_error').length > 0) {
					$(this).show();
				} else if($(this).children().children('.input_artwork_size').val() != '' && $(this).children().children('.input_artwork_size').val() != undefined) {
					
					$(this).show();
				}
				
			});
			$('#sizeTable').children().children('*:not(:first)').each(function() {
				$(this).append('<div class="artwork_size_delete" style="line-height:38px;float:left;height:38px; padding-left:5px;"><label class="green" onclick="artworkSizeSetRowToDelete($(this))" >Delete</label></div>');
				
			});
			
			
        },  
        error: function(msg) {  
            alert("Something hasn't worked\n" + msg.d);  
        }  
  	}); 
	
}


getMakeAnOfferPopup = function(artwork_size_id) {
	$.ajax({  
        type: "GET",  
        url: '/buy/make-an-offer?id='  + artwork_size_id,
        dataType: "html",  
        success: function(text) { 
		 	var popup = new Boxy(text, {modal: true, title: 'Make an offer' , closeable: true, closeText:'X', unloadOnHide: true });

		},  
        error: function(msg) {  
            alert("Something hasn't worked\n" + msg.d);  
        }  
  	}); 
	
}


showNextAvailableSize = function(elem) {
	elem.children().children('*:hidden').first().show();

}
artworkSizeSetRowToDelete = function(elem) {
	elem.parent().siblings("[name*='to_delete']").val(1);
	elem.parent().parent().hide();
}


refreshHeader = function(elem) {
	 $.ajax({  
        type: "GET",  
        url: '/en/adminheader',
        dataType: "text",  
        success: function(text) { 
         	$('#adminheader').html(text)
        },  
        error: function(msg) {  
            alert("Something hasn't worked\n" + msg.d);  
        }  
  	  }); 
}

$(document).ready(function() {
	
	refreshHeader();
	
	$('.bouton_upload_become').live('click', function () {
		
		$.ajax({  
        	type: "GET",  
	        url: '/en/upload-artwork' ,
	        dataType: "html",  
	        success: function(text) { 
			 	var popup = new Boxy(text, {modal: true, title: 'Upload an Artwork' , closeable: true, closeText:'X', unloadOnHide: true });

	        },  
	        error: function(msg) {  
	            alert("Something hasn't worked\n" + msg.d);  
	        }  
	  	}); 
	});
	
	$('.makeanoffer').live('click', function () {
		
				var target = $(this).attr('rel2');
		getMakeAnOfferPopup(target);
			
	});
	
	
	$('.bouton_confirm_shipping').live('click', function () {
		
		var target = $(this).attr('rel');
		$.get('/en/admin/confirmshipping?id=' + target, function(data) {
 				window.location.reload();
		});
	});
	
	$('.bouton_refuse_shipping').live('click', function () {
		
		var target = $(this).attr('rel');
		Boxy.confirm("Are your sure you want to refuse this sale?", function() { 
			$.get('/en/admin/refuseshipping?id=' + target, function(data) {
  				window.location.reload();
			});
			
		}, {title: 'Refuse Shipping'}, "Confirm", "Cancel");

		
		
		
	});
	
	$('.bouton_refuse_offer').live('click', function () {
		
		var target = $(this).attr('rel');
		Boxy.confirm("Are your sure you want to refuse this offer?", function() { 
			$.get('/en/admin/refuseoffer?id=' + target, function(data) {
  				window.location.reload();
			});
			
		}, {title: "Refuse Offer"}, "Confirm", "Cancel");

		
		
		
	});
	
	$('.bouton_accept_offer').live('click', function () {
		
		var target = $(this).attr('rel');
		Boxy.confirm("Are your sure you want to accept this offer?", function() { 
			$.get('/en/admin/acceptoffer?id=' + target, function(data) {
  				window.location.reload();
			});
			
		}, {title: "Accept Offer"}, "Confirm", "Cancel");

		
		
		
	});
	
	
	$('.need_authentication').live('click', function () {
		
			
		var popupneed = new Boxy("<div >You need to be logged to process this action.<br />Please <a onclick=\"showLogin();\" class=\"green close\" style=\"cursor:pointer\">login</a> or <a href=\"/en/apply\" class=\"green\">register</a> first.</div>", {modal: true, title: 'Authentication Required' , closeable: true, closeText:'X', unloadOnHide: true });
	
	});
	$('.delete_artwork').live('click', function () {
		
		var target = $(this).attr('rel');
		var reload = false;
		if($(this).hasClass('popup_button_gris')) {
			reload = true;
		}
		
		Boxy.confirm("Are your sure you want to delete this artwork?", function() { 
			$.get('/en/admin/delete?id=' + target, function(data) {
  				
				if(reload) {
					window.location.reload();
					//location.href = '/en/admin/gallery';
				} else {	
					$('#artwork_edit_' + data).remove();
				}
			});
			
		}, {title: 'Artwork deletion'}, "Confirm", "Cancel");

		
		
		
	});
	
	
	$('.delete_address').live('click', function () {
		
		var target = $(this).attr('rel');
		Boxy.confirm("Are your sure you want to delete this address?", function() { 
			$.get(target, function(data) {
				window.location.reload();
  				//$('#address_' + data).remove();
			});
			
		}, {title: "Address deletion"}, "Confirm", "Cancel");

		
		
		
	});
	$('.edit_artwork_link').live("click" , function() {
  		getEditArtworkPopup($(this).attr('rel'), $(this).attr('rel2'));
  	});

	$('img.captify').click(function() {
		location.href = $(this).attr('title');
	});
	$('img.captify').captify({
		// all of these options are... optional
		// ---
		// speed of the mouseover effect
		speedOver: 'fast',
		// speed of the mouseout effect
		speedOut: 'normal',
		// how long to delay the hiding of the caption after mouseout (ms)
		hideDelay: 500,	
		// 'fade', 'slide', 'always-on'
		animation: 'slide',		
		// text/html to be placed at the beginning of every caption
		prefix: '',		
		// opacity of the caption on mouse over
		opacity: '1',					
		// the name of the CSS class to apply to the caption box
		className: 'slide',	
		// position of the caption (top or bottom)
		position: 'bottom',
		// caption span % of the image
		spanWidth: '100%'
	});

  		

	$(".form_row_error input, .form_row_error select, .form_row_error textarea").tooltip({
				bodyHandler: function() {
					return $(this).parent().attr('title');
				},
				delay: 0,
				showURL: false
			});
	$(".tr_row_error input, .tr_row_error select, .tr_row_error textarea").tooltip({
				bodyHandler: function() {
					return $(this).parent().parent().attr('title');
				},
				delay: 0,
				showURL: false
			});
	
		
});

