﻿window.$BTJ.PopUp = new function () {

	var $this = this;
	$this.init = function() {
		//Extended qTip Styles;
		
		$.fn.qtip.styles.standard = { // Last part is the name of the styleSheet
			background: '#ffffff',
			color: '#002c5c',
			textAlign: 'left',
			border: {
				width: 1,
				radius: 1,
				color: '#d3d8e0'
			}
		};
		
		$.fn.qtip.styles.inCart = { // Last part is the name of the styleSheet
			tip: { corner: 'bottomLeft', size: { x: 10, y: 10 } },
			background:'url(styles/images/bg_popup_incart.png)',
			"background-position": "bottom",
			"background-repeat": "repeat-x",
			name: "standard",
			width: { min: 250 }
		};
		
		$.fn.qtip.styles.inStore = { // Last part is the name of the styleSheet
			tip: { corner: 'bottomMiddle', size: { x: 10, y: 10 } },
			background:'url(styles/images/bg_popup_incart.png)',
			"background-position": "bottom",
			"background-repeat": "repeat-x",
			name: "standard",
			width: { min: 100 }
		};
		
		$.fn.qtip.styles.openCategory = { // Last part is the name of the styleSheet
			tip: { corner: 'rightMiddle', size: { x: 10, y: 10 } },
			width: 188,
			name: "standard",
			background: "#ffffff",
			background:'url(styles/images/bg_popup_incart.png)',
			"background-position": "bottom",
			"background-repeat": "repeat-x"
		};
			
		$.fn.qtip.styles.hafteContent = { // Last part is the name of the styleSheet
			tip: { corner: 'rightTop', size: { x: 10, y: 10 } },
			width: 188,
			name: "standard",
			background: "#ffffff",
			background:'url(styles/images/bg_popup_incart.png)',
			"background-position": "bottom",
			"background-repeat": "repeat-x"
			
		};
		
		$.fn.qtip.styles.quickFacts = { // Last part is the name of the styleSheet
			tip: false,
			width: { min: 917 },
			height:  "auto",
			name: "standard",
			background: "#ffffff",
			padding: '0px 0px',
			border: {
				width: 1,
				radius: 1,
				color: '#99abbe'
			}
						
		};
		
		$.fn.qtip.styles.moveCart = { // Last part is the name of the styleSheet
			tip: { corner: 'bottomRight', size: { x: 10, y: 10 } },
			width: { min: 380, max: 450 },
			name: "standard",
			background: "#ffffff",
			background:'url(/styles/images/bg_popup_movecart.png)',
			"background-position": "bottom",
			"background-repeat": "no-repeat",
			border: {
				width: 1,
				radius: 1,
				color: '#99abbe'
			}
		};
		
		// New popup for Kundnummer, tillagd av Nils
		
		$.fn.qtip.styles.KundNummer = { // Last part is the name of the styleSheet
			tip: { corner: 'topLeft', size: { x: 10, y: 10 } },
			width: { min: 300, max: 400 },
			name: "standard",
			background: "#ffffff",
			background:'url(/styles/images/bg_popup_movecart.png)',
			"background-position": "bottom",
			"background-repeat": "no-repeat",
			border: {
				width: 1,
				radius: 1,
				color: '#99abbe'
			}
		};
		
		// New popup for Kundnummer, tillagd av Nils
		
		if($("#Kundnr").length > 0) {
		
			$this.createPopUp("input.KundnummerOrderSok", "none", { 
				content: $('#Kundnr').html(), 
				show: 'focus', 
				hide: 'unfocus', 
				style: "KundNummer",
				position: {
					corner: {
						target: 'bottomRight',
						tooltip: 'topleft'
					}
				}
			});
		
		}
		
		$this.createPopUp(".InCartStatus .OpenPopUp", "ajax", { 
			content: 'loading...', 
			show: 'click', 
			hide: 'click', 
			style: "inCart",
			position: {
				corner: {
					target: 'topRight',
					tooltip: 'bottomLeft'
				}
			}
		});

		$this.createPopUp(".InStoreStatus .OpenPopUp", "ajax", { 
			content: 'loading..', 
			show: 'click', 
			hide: 'click', 
			style: "inStore",
			position: {
				corner: {
					target: 'topRight',
					tooltip: 'bottomMiddle'
				}
			}
		});
		
		$this.createPopUp(".OpenCategory", "ajax", { 
			content: 'loading...', 
			show: 'click', 
			hide: 'click', 
			style: "openCategory",
			position: {
				corner: {
					target: 'leftMiddle',
					tooltip: 'rightMiddle'
				}
			}
		});
	    
	    
		$this.createPopUp(".MoveBtn", "ajax", { 
			content: 'loading...', 
			show: 'click', 
			hide: 'click', 
			style: "moveCart",
			position: {
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomRight'
				}
			}
		});
	
		
		$this.createPopUp(
		  ".IntroBox .Hafte .jsDownloadAjax", "ajax", { 
			content: 'loading...',
			show: 'click', 
			hide: 'click', 
			style: {
				background:'url(/styles/images/bg_popup_movecart.png)',
				name: "hafteContent"
			},
			position: {
				corner: {
					target: 'leftMiddle',
					tooltip: 'rightTop'
				}
			}
		});
		
	}; // end init.
	

	$this.createPopUp = function(trigger, method, params) {

		params.content.prerender = false;
		
		params.hide = {when: {event: 'unfocus'}};
		
		//Hjälpfunktioner för att stoppa länkarna och gömma bubblan när man byter flik.
		$(trigger).live('click', function(event){
			event.preventDefault();
		});
	
		// Popup-funktionen. Hämtar innehåll olika beroende på hur "method" är satt.
		$(trigger).live('mouseover', function() {
			// If we have not already buit a pop for this element
			if($(this).data('popBuilt') != true) {	
				var button = $(this);
				
				if (method == "ajax") {
					url = $(this).attr('href');
					params.content = { text: params.content, url: url };
				} else if (method == "inline") {      
					content = $(this).attr('href');
					params.content = $(content);
				}    
				
				// If button has opened a move-to-cart-popup
				if(button.hasClass('MoveBtn')) {
					params.api = {
						onContentUpdate : function () {
							var self = this;
							// Configures the Cancel button
							this.elements.content.find('.CancelBtn').click(function(event) {
								// Just close the dialog
								event.preventDefault();
								self.hide();
							})
						}
					};
					$(this).qtip(params);
					
				
				} else if (button.hasClass('KundnummerOrderSok')) { // New popup for Kundnummer, tillagd av Nils
				
				params.api = {
				
					onRender : function () {
								var self = this;
								
								self.elements.content.find("input:checkbox").click(function()				
									{
									
										var checked_status = this.checked;
										
										if ($(this).hasClass("Alla")) {
										self.elements.content.find("input:checkbox").each(function()
										{
											this.checked = checked_status;
											if(checked_status) {
												$(this).attr('value', 'on');
											}
										}); 
										} else {
										self.elements.content.find(".Alla:checkbox").each(function()
										{
											this.checked = false;
											if(checked_status) {
												$(this).attr('value', 'off');
											}
											
										});
										}
									});	
									
									
								this.elements.content.find('.OkBtn').click(function(event) {
									
									button.attr("value", "");
									
									self.elements.content.find("input:checkbox:checked").each(function(i){
										
										var currentVal = button.attr("value");
										var newVal = $(this).attr("custnumber");
										
										var value = currentVal + newVal + ",";
										
										if (newVal=="Alla") {
										
											button.attr("value", "Alla");
											return false;
									
										} else {
											
											button.attr("value", value);
										
										}
										
									});
									
									var sValue = button.attr("value");
									sValue = sValue.replace(/\,$/,'');
									button.attr("value", sValue);
									button.addClass("FormHintActive");
									
									// Close the dialog
									event.preventDefault();
									self.hide();
								})
								// Configures the Cancel button
								this.elements.content.find('.CancelBtn').click(function(event) {
									// Just close the dialog
									event.preventDefault();
									self.hide();
								})
							}
				
				};
				
				$(this).qtip(params);
				
				} else {
					$(this).qtip(params);
				}
					
				// Lets indicate that the pop has been built
				$(this).data('popBuilt', true);				    
			}
	 });
	};
};

