$j(document).ready(function(){
    $j("a#inline").fancybox({
        'hideOnContentClick': true
    });
    
    //this breaks datetimepicker :(
    /*
    $j("form [title]").tooltip({
        offset: [0, 350],
        effect: "fade",
        opacity: 0.9
    });
    */
    $j(".showToolTip [title]").tooltip({
        offset: [0, 350],
        effect: "fade",
        opacity: 0.9
    });

    $j(".showToolTipEditProfile [title]").tooltip({
        // place tooltip on the right edge
	position: "top right",
	// a little tweaking of the position
	offset: [60, 40],
        effect: "fade",
        opacity: 0.9,
        events: {
		input: 'click, blur',
		checkbox: 'mouseover click, mouseout',
		date: 'click, blur'
	},

	relative: false,

	onBeforeShow: function(){
            var thistrigger = this.getTrigger();
            var thistip = this.getTip();
            thistip.html('\
			<div class="callout">\
				<div class="content">\
					<p class="plus-padding">\
					' + thistrigger.data("title") + '\
					</p>\
				</div>\
			</div>');
            }
    });

    $j(".registerLoginForm[title]").tooltip({
        // place tooltip on the right edge
	position: "top right",
	// a little tweaking of the position
	offset: [60, 40],
        effect: "fade",
        opacity: 0.9,
        events: {
		input: 'click, blur',
		checkbox: 'mouseover click, mouseout',
		date: 'click, blur'
	},

	relative: false,

	onBeforeShow: function(){
            var thistrigger = this.getTrigger();
            var thistip = this.getTip();
            thistip.html('\
			<div class="callout">\
				<div class="content">\
					<p class="plus-padding">\
					' + thistrigger.data("title") + '\
					</p>\
				</div>\
			</div>');
            }
    });

    $j(".taggingInput[title]").tooltip({
        // place tooltip on the right edge
	position: "top right",
	// a little tweaking of the position
	offset: [130, 280],
        effect: "fade",
        opacity: 0.9,

	relative: false,

	onBeforeShow: function(){
            var thistrigger = this.getTrigger();
            var thistip = this.getTip();
            thistip.html('\
			<div class="callout">\
				<div class="content">\
					<p class="plus-padding">\
					' + thistrigger.data("title") + '\
					</p>\
				</div>\
			</div>');
            }
    });

    $j("#DefaultContentForm-subTitle").tooltip({
        // place tooltip on the right edge
	position: "top right",
	// a little tweaking of the position
	offset: [90, 30],
        effect: "fade",
        opacity: 0.9,
        events: {
		input: 'click, blur',
		checkbox: 'mouseover click, mouseout',
		date: 'click, blur'
	},

	relative: false,

	onBeforeShow: function(){
            var thistrigger = this.getTrigger();
            var thistip = this.getTip();
            thistip.html('\
			<div class="callout">\
				<div class="content">\
					<p>\
					' + thistrigger.data("title") + '\
					</p>\
				</div>\
			</div>');
            }
    });
    $j("#DefaultContentForm-title").tooltip({
        // place tooltip on the right edge
	position: "top right",
	// a little tweaking of the position
	offset: [150, 30],
        effect: "fade",
        opacity: 0.9,
        events: {
		input: 'click, blur',
		checkbox: 'mouseover click, mouseout',
		date: 'click, blur'
	},

	relative: false,

	onBeforeShow: function(){
            var thistrigger = this.getTrigger();
            var thistip = this.getTip();
            thistip.html('\
			<div class="callout">\
				<div class="content">\
					<p>\
					' + thistrigger.data("title") + '\
					</p>\
				</div>\
			</div>');
            }
    });
    $j("iframe#DefaultContentForm-mainText").tooltip({
        // place tooltip on the right edge
	position: "top right",
	// a little tweaking of the position
	offset: [136, 30],
        effect: "fade",
        opacity: 0.9,
        events: {
		input: 'click, blur',
		checkbox: 'mouseover click, mouseout',
		date: 'click, blur'
	},
	relative: false,
	onBeforeShow: function(){
            var thistrigger = this.getTrigger();
            var thistip = this.getTip();
            thistip.html('\
			<div class="callout">\
				<div class="content">\
					<p>\
					' + thistrigger.data("title") + '\
					</p>\
				</div>\
			</div>');
            }
    });
    $j(".showToolTipProductPage[title]").tooltip({
        // place tooltip on the right edge
	position: "top right",
	// a little tweaking of the position
	offset: [48, 5],
        effect: "fade",
        opacity: 0.9,
        events: {
		input: 'click, blur',
		checkbox: 'mouseover click, mouseout',
		date: 'click, blur'
	},

	relative: false,

	onBeforeShow: function(){
            var thistrigger = this.getTrigger();
            var thistip = this.getTip();
            thistip.html('\
			<div class="callout">\
				<div class="content">\
					<p>\
					' + thistrigger.data("title") + '\
					</p>\
				</div>\
			</div>');
            }
    });
});

function findDuplicates(data, callbackConfirm, callbackCancel){
	$j.post("/backend/customer/find-duplicate/format/json", data, function(r){
								if (undefined !== r.ret[0]) {
			var matches = r.ret;
			//match found
			var dialogDiv = $j("#dialog-customer-matches");
			if  (dialogDiv.length == 0) {
				var dialogDiv = $j('<div id="dialog-customer-matches"></div>');
				$j("body").append(dialogDiv);
			}
			var html = "";
			for (i in matches) {
				if ('function' !== typeof matches[i]) {
					var billing = matches[i]['billingAddress1'] + ", " + matches[i]['billingAddress2'] + ", " + matches[i]['billingAddress3'] + ", " + matches[i]['billingAddress4'] + ", " + matches[i]['billingPostCode'];     
					var shipping = matches[i]['shippingAddress1'] + ", " + matches[i]['shippingAddress2'] + ", " + matches[i]['shippingAddress3'] + ", " + matches[i]['shippingAddress4'] + ", " + matches[i]['shippingPostCode'];
					displayName = "<span>"  + matches[i]['firstName'] + " " + matches[i]['lastName'] + "<br />Billing:<br />" + billing + "<br />Shipping:<br />" + shipping + "</span>";
					html += "<a class='chooseCustomerLink' customerId='"+ matches[i]['id'] + "' id='match_" + matches[i]['id']+"' href='#'>" + displayName + "</a> <br /><br />";
				}
			}

			if (html !=''){
				$j("#dialog-customer-matches").html(html);
			}
			
			$j("#dialog-customer-matches").dialog({
	    		title: "Possible duplicates found", 
	    		buttons: { 
	    					"None of above": function() { 
	    						$j(this).dialog("close"); 
	    						if ('function' == typeof (callbackCancel)) {
	    							callbackCancel();
		    					}
	    						return false;
	    					}
	    		},
	    		resizable: false,
	    		modal: true
	    	});

	    	$j(".chooseCustomerLink").click(function(){
	    		var customerId = $j(this).attr("customerId");
	    		if ('function' == typeof (callbackConfirm)) {
					callbackConfirm(customerId);
				} else {
					document.location.href = "/backend/customer/edit/id/" + customerId;	
				}
		    });
	    	return false;
		}
	});
};

function loginFormPopup(callback){
	$j.getScript("/js/zend-form.validator.js");
	
	var dialogDiv = $j("#dialog-login");
	if  (dialogDiv.length == 0) {
		var dialogDiv = $j('<div id="dialog-login"></div>');
		$j("body").append(dialogDiv);
	}
	
	$j.get("/login/form/format/json", function(r){
		$j("#dialog-login").html(r);
		$j("#FrontendLoginForm-emailAddress").focus();
	});
	
	$j("#dialog-login").dialog({
		title: "Please login", 
		width: 400,
		height: 230,
		buttons: { 
					"Login": function() {
						var form = $j("#login-form");
						zendForm.validate(form, function(){
							var data = {};
							data.emailAddress = $j("#FrontendLoginForm-emailAddress").val();
							data.password = $j("#FrontendLoginForm-password").val();
							data.hashLoginForm = $j("#FrontendLoginForm-hashLoginForm").val();
							$j.post("/login/login-ajax/format/json", data, function(r){
								if(r.ret === true) {
									$j.jGrowl("You have been logged in successfuly");
									$j("#dialog-login").dialog("close");
									if (typeof callback == 'function') {
										callback();
									}
								} else {
									$j.jGrowl("Wrong username or password");
								}
								return false;
								
							});
						});
						return false; 
					},
					"Cancel": function() { 
						$j(this).dialog("close"); 
						return false;
					}
		},
		resizable: false,
		modal: true
	});
};
