/**
 * General GUI implementation
 * 
 * DEPENDENCIES:
 * - jquery-1.4.2.min.js            // Jquery core
 * - jquery-ui-1.8.4.custom.min.js  // Jquery UI components
 * - jquery.form.js                 // Jquery plugin for Ajax management of forms
 * 
 * @author Johan.Weitner
 * 
 */

var BLTSO_GUI = {
  
  BOOT_TIME: new Date(),
  
  DEV_MODE: false, // Set to false to disable logging to local console
  DEFAULT_LOGIN_TITLE: "Logga in",
  MY_PAGE_URL: "min_sida/",
  LOGIN_REDIRECT_HOME: false,
  AUTOBROWSE_SPEED: 7,
  
  IS_IE: $.browser.msie,
  IE_VER: $.browser.version.substring(0,1),
  
 
  /**
   * Initialize GUI when DOM is ready...
   */
  init: function() {
      BLTSO_GUI.log("DOM ready - begin GUI init calls");
      
	  BLTSO_GUI.markBodyJsReady();
	  BLTSO_GUI.initFacebook();
	  BLTSO_GUI.initTopBanner();
	  BLTSO_GUI.applyIEFixes();
	  BLTSO_GUI.initQuickSearch();
      BLTSO_GUI.initLoginDialog();
      BLTSO_GUI.initUserMenu();
	  BLTSO_GUI.initReadMenu();
	  BLTSO_GUI.initQuickSearchForm();
	  BLTSO_GUI.initAddThis();
	  BLTSO_GUI.initTeaserGalleries();
	  BLTSO_GUI.initBorderedBoxContainers();
	  BLTSO_GUI.fixHalfWidthTeasers();
	  BLTSO_GUI.initMarkOnMapLink();
	  BLTSO_GUI.initInputGoogleMap();	
	  BLTSO_GUI.initAdvancedSearch();
	  BLTSO_GUI.initTenMoreLink();
	  BLTSO_GUI.initSlideshowBoxes();
	  BLTSO_GUI.initDateSpanWidgets();
	  BLTSO_GUI.initCancelDialog();
	  BLTSO_GUI.initImagePopup();
	  
      BLTSO_GUI.log("Done with GUI init calls");
    
      BLTSO_GUI.log("**************************************************************");
      BLTSO_GUI.log("\tGUI initialized in " + (new Date() - BLTSO_GUI.BOOT_TIME) + " milliseconds.");
      BLTSO_GUI.log("**************************************************************");
    
    /*
    $.ajaxSetup({
      url: 'ping.php'
    });
    */
    
  },
  
  initDateSpanWidgets: function() {
		// Exit if neither fromDate nor toDate exists
		if($("#fromDate").length < 1 && $("#toDate").length < 1) {
		  return;
		}

		var returnUnknown = $('.dateSpan').find('#returnUnknown');
		var startDateInput = $('.dateSpan').find('#fromDate');	
		var endDateInput = $('.dateSpan').find('#toDate');	
		var dateFormat = 'yy-mm-dd';
		var options = {
			dateFormat: dateFormat, 
			constrainInput: true,
			dayNames: ['Söndag', 'Måndag', 'Tisdag', 'Onsdag', 'Torsdag', 'Fredag', 'Lördag'],
			dayNamesShort: ['Sön', 'Mån', 'Tis', 'Ons', 'Tor', 'Fre', 'Lör'],				
			dayNamesMin: ['Sö', 'Må', 'Ti', 'On', 'To', 'Fr', 'Lö'],
			firstDay: 1,
			monthNames: ['Januari','Februari','Mars','April','Maj','Juni','Juli','Augusti','September','Oktober','November','December'],
			showOn: "button",
			buttonText: "Välj datum",
			defaultDate: "+1w",
			changeMonth: true,
			monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],
			currentText: "Idag",
			nextText: "Nästa",
			prevText: "Föregående",
			buttonImage: (publicationUrl + "template/ver2-0/images/datepicker.png"),
			buttonImageOnly: true
		};
		var disableEndDate = function() {
			endDateInput.val('');
			endDateInput.css('background-color', '#F8F8F8');
			endDateInput.attr('disabled', 'disabled');
			endDateInput.datepicker('disable');
			startDateInput.datepicker( 'option', 'minDate', '' );
			startDateInput.datepicker( 'option', 'maxDate', '' );
		};
		var enableEndDate = function() {
			endDateInput.css('background-color', '');
			endDateInput.removeAttr('disabled');
			endDateInput.datepicker('enable');
		};
	  
		// If only fromDate exists
		if($("#fromDate").length > 0 && $("#toDate").length < 1) {
	      $( "#fromDate" ).datepicker(options);
	    }
	  
		// If both fromDate and toDate exists - create a date span widget pair
		if($("#fromDate").length > 0 && $("#toDate").length > 0) {
			options.onSelect = function( selectedDate ) {
				var option = this.id == "fromDate" ? "minDate" : "maxDate",
				instance = $( this ).data( "datepicker" ),
				date = $.datepicker.parseDate(
				  instance.settings.dateFormat ||
				  $.datepicker._defaults.dateFormat,
				  selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
	        };
			var dates = $( "#fromDate, #toDate" ).datepicker(options);
		}
	  
		//Disable end-date picker on checkbox click
		returnUnknown.click( function(event) {
			if (endDateInput.is(':disabled')) { 
				enableEndDate();  
			} else {	    	  
				disableEndDate();
			}
		});
	  
		//Disable end-date picker when permamnent move is selected
		$('.radioButtonContainer #permanent').click( function(event) {
			disableEndDate();
			returnUnknown.attr('disabled', 'disabled');
			returnUnknown.removeAttr('checked');
		});
		$('.radioButtonContainer #temporary').click( function(event) {
			enableEndDate();
			returnUnknown.removeAttr('disabled');
			returnUnknown.removeAttr('checked');
		});	
	  
		//Start and end dates for pause in delivery can not be less than 7 days and more than 3 months apart
		if ( $('.dateSpan.pause').length > 0 ) {
			var onSelect = function(dateText, inst) {
				var currentDate = $(this).datepicker( 'getDate');
				var minDate = new Date(1900 + currentDate.getYear(), currentDate.getMonth(), currentDate.getDate() + 7);
				var maxDate = new Date(1900 + currentDate.getYear(), currentDate.getMonth() + 3, currentDate.getDate());
				$( '.dateSpan.pause #toDate' ).datepicker( 'option', 'minDate', minDate );
				$( '.dateSpan.pause #toDate' ).datepicker( 'option', 'maxDate', maxDate );
			};
			$( '.dateSpan.pause #fromDate' ).datepicker('option', 'onSelect', onSelect);
		}
		
		//If returnUnknown is initially selected, disable end-date picker
		if(returnUnknown.is(':checked')){ 
			disableEndDate();
		}
	},
  
  
  
  /**
   * Initialize the quicksearch widget in page head
   */
 initQuickSearch: function() {
 	$("#quicksearch .quicksearchfield input").click(function(){
		$("#quicksearch .quicksearchfield label").hide();
	});
	
	$("#quicksearch .quicksearchfield label").click(function(){
		$("#quicksearch .quicksearchfield label").hide();
		$("#quicksearch .quicksearchfield input").focus();
	});
	
	$("#quicksearch .quicksearchfield input").blur(function(){
		if($("#quicksearch .quicksearchfield input").val().length == 0) {
			$("#quicksearch .quicksearchfield label").show();
		}
	});
 },
  
  
  
  initTopBanner: function() {
    BLTSO_GUI.log("Initializing top banner");
  	$("#topBanner").css("height","auto");
  	
  	var h = $("#topBanner").height();
  	var diff = h - 120;
  	if(diff != 0) {
  		var css = (145 + diff) + "px 0 0 10px";
  	  	$("header").css("margin", css);
  	  	
  	  	//$("body").css("background-position", "0 " + (diff + 35) + "px");
  	  	$("body").css("background-position", "0 " + (diff - 100) + "px");
  	  	
  	  	$("#mainNav").css("top", (216 + diff) + "px");
  	  	$("#readMenu").css("top", (253 + diff) + "px");
  	}
  	
  	
  	// Set up fixed banner
  	if($("body.lockedTopBanner".length > 0)) {
  		BLTSO_GUI.log("Setting up fixed top banner...");
		var banner = $("#topBanner");
  		$(banner).css("position", "fixed");

		// add shadow element
		if ( $.browser.msie  &&  $.browser.version < 9.0 ) {
			$(banner).find('.rel').append('<div class="shadow"></div>');
			var $shadow = $(banner).find('div.shadow').hide();
			//$shadow.hide();
		}
		
		var fixed = true;
		var doc = $(document);
		$(window).scroll(function() {

			var pos = doc.scrollTop();
			if(fixed && pos > 2000) {
				$(banner).css({
					position : "absolute",
					top : (pos + "px")
				});
				fixed = false;
				BLTSO_GUI.log("Switching fixed top banner mode to absolute");
			} else if(!fixed && pos < 2000) {
				$(banner).css({
					position : "fixed",
					top : ("0px")
				});
				fixed = true;
				BLTSO_GUI.log("Switching back fixed top banner mode to fixed");
			}
			
			if(fixed && pos > 10 && pos < 2000) {
				$(banner).css({
					backgroundColor : "#fff",
					"box-shadow": "-4px 3px 7px rgba(0, 0, 0, 0.3) "
				});
				if ( $.browser.msie  &&  $.browser.version < 9.0 ) {
					$shadow.show();
				}
				
			} else {
				$(banner).css({
					backgroundColor : "transparent",
					"box-shadow": "0px 0px 0px #FFF"
				});
				if ( $.browser.msie  &&  $.browser.version < 9.0 ) {
					$shadow.hide();
				}
			}
		});
	}
  },
  
  
  
  fixHalfWidthTeasers: function() {
  	BLTSO_GUI.log("Adding CSS classes to halfwidth teasers");
  	if($("article.halfwidth").length != 0) {
		BLTSO_GUI.log("Found half-width teasers - adding classes");
		$("article.halfwidth").each(function(i){
			if(i % 2 == 0) {
				$(this).addClass("odd");
			} else {
				$(this).addClass("even");
			}
		});
	}
  },
  
  
  
  applyIEFixes: function() {
  	if(!(BLTSO_GUI.IS_IE && BLTSO_GUI.IE_VER < 9)) {
		return;
	}
	
	if($(".dayNav").length != 0) {
		$(".dayNav li:first-child").addClass("firstChild");
		if($(".dayNav li").length > 1) {
			$(".dayNav li:last-child").addClass("lastChild");	
		}
	}
	
	if($(".newspicContainer").length != 0) {
		/*$(".newspicContainer").each(function() {
			var img = $(this).find("img");
			var w = $(img).attr("width");
			var diff = 227 - w;
			
			BLTSO_GUI.log("Image width attr: " + w);
			BLTSO_GUI.log("Difference to container: " + diff);
			
			if(diff > 1) {
				$(img).parent("a").css("left", (Math.round(diff/2)) + "px");
			}
		});*/
		
		$(".newsinpixrow figure").each(function() {
			$(this).mouseover(function(){
				$(this).find("figcaption").show();
			});
			$(this).mouseout(function(){
				$(this).find("figcaption").hide();
			});
		});
	}
	
	
  },
  
  
  
  initQuickSearchForm: function() {
  	BLTSO_GUI.log("Initializing quick search form");
	jQuery(".overlaidSearchBtn a").click(function(){
		try {
			BLTSO_GUI.log("Submitting quick search form...");
			jQuery(this).parent("form").submit();
		} catch(e) {
			BLTSO_GUI.log("Could not submit quick search form: " + e.message);
		}
		
	});
  },
  
  
  
  initAdvancedSearch: function() {
  	if($("p.advancedSearchLink").length < 1) {
		return;
	}
	
	BLTSO_GUI.log("Initializing advanced search form...");
	
	try {
		// Init onload states
		var initPubChoice = $("select#category").val();
		if(initPubChoice != "all") {
			var activePubId = "#search-section-" + initPubChoice;
			var activeWriterId = "#search-author-" + initPubChoice;
			$(activePubId).show();
			$(activeWriterId).show();
		} else {
			$("p.departmentSelection").parent(".formFieldGroupSection").hide();
		}
		
		// Add eventhandler to "show advanced search form" link
		$("p.advancedSearchLink a").click(function(ev) {
			ev.preventDefault();
			//$("section.advancedSearchFormContainer").show();
			$("section.advancedSearchFormContainer").toggle();
		});
		
		// Handle choice of publication
		$("select#category").change(function(){
			var pubChoice = $(this).val();
			if(pubChoice == "all") {
				$("p.departmentSelection").parent(".formFieldGroupSection").hide();
			} else {
				$("p.departmentSelection select, p.writerSelection select").each(function(){
					$(this).hide();
				});
				var activePubId = "#search-section-" + pubChoice;
				var activeWriterId = "#search-author-" + pubChoice;
				$(activePubId).show();
				$(activeWriterId).show();
				$("p.departmentSelection").parent(".formFieldGroupSection").show();
			}
		});
	} catch(e) {
		BLTSO_GUI.log("Could not initialize advanced search form: " + e.message);
	}
	
	BLTSO_GUI.log("Done initializing advanced search form");
  },
  
  
  
  // Initialize ten more link
  initTenMoreLink: function() {
  	if($("a.tenMoreLink").length < 1) {
		return;
	}
	
	BLTSO_GUI.log("Initializing ten more link");
	
	$("a.tenMoreLink").unbind("click");
	$("a.tenMoreLink").click(function(ev){
		ev.preventDefault();
		
		var offset = ev.target.href.match("offset=(\\d*)")[1];
		var section = ev.target.href.match(publicationUrl + "(.*/)")[1];
		//would do ev.target.href + '&service=ajax&type=readersChoice' but if parameters are not kept in service-type-offset order the encoding breaks ?!!
		var url = publicationUrl + section + "?service=ajax&type=readersChoice&offset=" + offset;
		
		$.get(url, function(data, textStatus, req) {
		  var tmp = $("<div id='tmp'></div>");
		  $(tmp).append(data);
		  var tenMoreBox = $(tmp).find("section.tenMorePlease p");
		  $("#newsAreaLeft ol.orderedArticleList").append($(tmp).find("ol.orderedArticleList li"));
		  
		  if($(tenMoreBox).length != 0) {
		  	BLTSO_GUI.log("Ten more exists - adding new ten more link");
			$("section.tenMorePlease").html(tenMoreBox);
			$("section.tenMorePlease").show();
			BLTSO_GUI.initTenMoreLink();
		  } else {
		  	BLTSO_GUI.log("This is the end - hiding ten more link");
			$("section.tenMorePlease").hide();
		  }	
		  
		  return false;
		});
		
	});
	$(".tenMorePlease p").show();
	BLTSO_GUI.log("Done initializing ten more link");
  },
  
  
  
  /**
   * Initialize browser teaser boxes
   */
  initSlideshowBoxes: function() {
  	BLTSO_GUI.log("Initializing slideshow boxes...");
	
	var minHeight = 10;
	if(BLTSO_GUI.IS_IE && BLTSO_GUI.IE_VER < 9) {
		minHeight = 30;
	}
	
	$(".slideshowBox").each(function(){
		
		// Look for tallest image and set fixed height for all image containers
		/*var imgH = 0;
		$(this).find("figure img").each(function(){
			var h = $(this).attr("height");
			if(h > imgH) {
				imgH = h;
			}
		});
		$(this).find("figure").each(function(){
			$(this).height(imgH);
		});*/
		
		// Look for the tallest item and set fixed parent container height accordingly
		var h = 0;
		$(this).find("section").each(function(){
			if($(this).height() > h) {
				h = $(this).height();
			}
		});
		$(this).height(h + minHeight);
		
		// Center containers vertically
		$(this).find("section").each(function(i){
      if($(this).height() < h && $(this).find("h3").length == 0) {
        var diff = h - $(this).height();
        var tm = Math.round(diff/2);
        BLTSO_GUI.log("Container # " + i + " is shorter than max height (" + h + "), height = " + $(this).height() + " - diff = " + diff + " - setting top margin = " + tm);
        $(this).css("position", "relative");
        $(this).css("top", tm + "px");
      } else {
        var diff = h - $(this).height();
        var diff = h - $(this).height();
        var tm = Math.round(diff/2);
        BLTSO_GUI.log("Container # " + i + " is NOT shorter than max height (" + h + "), height = " + $(this).height() + " - diff = " + diff + " - setting top margin = " + tm);
      }
    });
		
		// Setup
		$(this).data("active", 0);
		var len = $(this).find("section").length;
		$(this).data("num", len);
		
		// Next
		$(this).find(".browsertool .next").click(function(ev){
			ev.preventDefault();
			$(this).find("a").blur();
			var curr = $(this).parents(".slideshowBox").data("active");
			if(curr == (len - 1)) {
				BLTSO_GUI.gotoSlideshowStart(this);
				return;
			}
			var nxt = curr + 1;
			$(this).parents(".slideshowBox").find("section.active").removeClass("active").next("section").addClass("active");
			$(this).parent(".browsertool").find("li.on").removeClass("on").next("li").addClass("on");
			
			$(this).parents(".slideshowBox").data("active", nxt);
		});
		
		// Previous
		$(this).find(".browsertool .prev").click(function(ev){
			ev.preventDefault();
			$(this).find("a").blur();
			var curr = $(this).parents(".slideshowBox").data("active");
			if(curr == (0)) {
				BLTSO_GUI.gotoSlideshowEnd(this);
				return;
			}
			var nxt = curr - 1;
			$(this).parents(".slideshowBox").find("section.active").removeClass("active").prev("section").addClass("active");
			$(this).parent(".browsertool").find("li.on").removeClass("on").prev("li").addClass("on");
			
			$(this).parents(".slideshowBox").data("active", nxt);
		});
		
	});
	
	// Start auto scroll
	if (thisAutoScroll) {
		clearInterval(thisAutoScroll);
	}
	var thisAutoScroll = setInterval("BLTSO_GUI.gotoNextSlide()", 1000 * BLTSO_GUI.AUTOBROWSE_SPEED);
	BLTSO_GUI.log("Done initializing slideshow boxes");
  },
  
  
  
  gotoNextSlide: function() {
	$(".slideshowBox").each(function() {
		$(this).find(".browsertool .next").click();
	});
  },
  
  
  
  gotoSlideshowStart: function(obj) {
  	$(obj).parents(".slideshowBox").data("active", 0);
	$(obj).parents(".slideshowBox").find("section.active").removeClass("active");
	$(obj).parent(".browsertool").find("li.on").removeClass("on");
	
	$(obj).parents(".slideshowBox").find("section").first().addClass("active");
	$(obj).parent(".browsertool").find("li").first().next().addClass("on");
  },
  
  
  
  gotoSlideshowEnd: function(obj) {
	var num = $(obj).parents(".slideshowBox").data("num");
	$(obj).parents(".slideshowBox").data("active", (num - 1));
	$(obj).parents(".slideshowBox").find("section.active").removeClass("active");
	$(obj).parent(".browsertool").find("li.on").removeClass("on");
	
	$(obj).parents(".slideshowBox").find("section").last().addClass("active");
	$(obj).parent(".browsertool").find("li").last().prev().addClass("on");
  },
  
  
  
  /**
   * Mark body class="js" to enable styles relevant to script-enabled browsers
   */
  markBodyJsReady: function() {
  	jQuery(document.body).addClass("js");
  },
  
  

  /**
   * Initialize modal dialog for login form.
   * Depends on jQuery UI.
   */
  initLoginDialog: function() {
    BLTSO_GUI.log("initLoginDialog invoked");
    // Instantiate modal dialog
    try {
      jQuery("#loginDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 710,
        height: 330,
        zIndex: 2000,
        modal: true,
        draggable: false,
        resizable: false,
        closeOnEscape: true,
        dialogClass: "loginDialog",
    		open: function(event, ui) {
    			$(".ui-widget-overlay").unbind("click");
    			$(".ui-widget-overlay").click(function(){
    				$('#loginDialog').dialog('close');
    			});
    		}
      });
      
      BLTSO_GUI.initLoginLinks();
	    BLTSO_GUI.initLoginHomeLink();
      //BLTSO_GUI.initLoginFormLabels();
      BLTSO_GUI.initFieldHighlighter();
      BLTSO_GUI.initTabbedCustomContentBox();
      BLTSO_GUI.initLoginFormAction();
      
    } catch(e) {
      BLTSO_GUI.log("Could not initialize login dialog: \n" + e.message);
    }
     
  },
  
  /**
   * Initialize modal dialog for 
   * subscription cancellation confirmation.
   * Depends on jQuery UI.
   */
  initCancelDialog: function() {
	    BLTSO_GUI.log("initCancelDialog invoked");
	    // Instantiate modal dialog
	    try {
	      jQuery("#cancelDialog").dialog({
	        bgiframe: true,
	        autoOpen: false,
	        width: 370,
	        height: 330,
	        zIndex: 2000,
	        modal: true,
	        draggable: false,
	        resizable: false,
	        closeOnEscape: true,
	        dialogClass: "loginDialog",
			open: function(event, ui) {
				$(".ui-widget-overlay").unbind("click");
				$(".ui-widget-overlay").click(function(){
					$('#cancelDialog').dialog('close');
				});
			}
	      });
	      
	      BLTSO_GUI.initCancelLinks();
	      
	    } catch(e) {
	      BLTSO_GUI.log("Could not initialize login dialog: \n" + e.message);
	    }
	     
  },
  
  /**
   * Add event handlers to login links
   * Depends on jQuery UI.
   */
  initLoginLinks: function() {
    BLTSO_GUI.log("initLoginLinks invoked");
    jQuery('.loginlink').click(function() {
      BLTSO_GUI.LOGIN_REDIRECT_HOME = false;
	  jQuery('#loginDialog').dialog('open');
      return false;
    });
  },
  
  /**
   * Add event handler to top-most login button 
   * - redirecting to "Min sida" post login success
   * Depends on jQuery UI.
   */
  initLoginHomeLink: function() {
    BLTSO_GUI.log("initLoginHomeLink invoked");
    jQuery('.loginhomelink').click(function() {
      BLTSO_GUI.LOGIN_REDIRECT_HOME = true;
	  jQuery('#loginDialog').dialog('open');
      return false;
    });
  },
  
  /**
   * Add event handler to the subscription cancellation button.
   * Depends on jQuery UI.
   */
  initCancelLinks: function() {
	    BLTSO_GUI.log("initCancelLinks invoked");
	    jQuery('#cancelButton').click(function() {
	      BLTSO_GUI.LOGIN_REDIRECT_HOME = false;
		  jQuery('#cancelDialog').dialog('open');
	      return false;
	    });
  },
  
  /**
   * Hilite chosen field container
   */
  initFieldHighlighter: function() {
    BLTSO_GUI.log("initFieldHighlighter invoked");
    jQuery(".buyformfield input").click(function() {
      jQuery(".buyformfield.active").removeClass("active");
      jQuery(this).parent().addClass("active");
    });
  },
  
  
  
  /**
   * Open login dialog with custom dialog title
   * Depends on jQuery UI.
   */
  openLoginDialog: function(dialogTitle) {
      BLTSO_GUI.log("openLoginDialog invoked");
      if(dialogTitle) {
        jQuery( "#loginDialog" ).dialog({ title: dialogTitle });
      } else {
        jQuery( "#loginDialog" ).dialog({ title: BLTSO_GUI.DEFAULT_LOGIN_TITLE });
      }
      jQuery('#loginDialog').dialog('open');
  },
  
  
  
  /**
   * Show/hide login field labels on focus/blur
   */
  initLoginFormLabels: function() {
    BLTSO_GUI.log("initLoginFormLabels invoked");
    jQuery(".loginformfield label").not(".logincheckbox label").click(function() { jQuery(this).hide(); });
    jQuery(".loginformfield input").not(".logincheckbox input").focus(function() { jQuery(this).siblings("label").hide(); });
    jQuery(".loginformfield input").not(".logincheckbox input").blur(function() {
      if(jQuery(this).val().length == 0) {
        jQuery(this).siblings("label").show();  
      }
    });
  },
  
  
  
  /**
   * Initialize user menu
   */
  initUserMenu: function() {
    BLTSO_GUI.log("initUserMenu invoked");
    jQuery("#userMenu h2 a").click(function(){
      jQuery("#userMenu").toggleClass("open");
      jQuery("#userMenu h2 a").blur();
    });
  },
  
  
  
  initReadMenu: function() {
  	BLTSO_GUI.log("initReadMenu invoked");
	jQuery("#readMenuHeader a").click(function(ev){
		ev.preventDefault();
		
		if($("body").hasClass("showReadMenuOnStart")) {
			$("body").removeClass("showReadMenuOnStart")
		} else {
			$("body").toggleClass("openReadMenu");
		}
		
		jQuery(this).blur();
		
		// Persist readMenu state in session
		var showReadMenu = $("body").hasClass("openReadMenu") ? "true" : "false";
		var url = publicationUrl + "?service=ajax&type=toggleReadMenu&showReadMenu=" + showReadMenu;
		$.get(url, function(data) {
			BLTSO_GUI.log(data);
		});
		
		return false;
	});
  },
  
  
  
  /**
   * Create image captions for "News in pictures" section
   */
  initNewsInPics: function() {
    BLTSO_GUI.log("initNewsInPics invoked");
	if(!jQuery("#newsinpix")) {
        return;
    }
	try {
		jQuery(".newsinpixrow a").each(function(){
	      jQuery(this).append("<figcaption>" + jQuery(this).attr("title") + "</figcaption>");
	      jQuery(this).removeAttr("title");
	    });
	} catch(e) {
		BLTSO_GUI.log("Could not initialize news image captions: " + e.message);
	}
  },
  
  

  /**
   * Initialize modal ad
   */
  initModalAd: function() {
    BLTSO_GUI.log("initModalAd invoked");
    // Instantiate modal ad
    try {
      jQuery("#modalAd").dialog({
        bgiframe: true,
        autoOpen: true,
        width: 670,
        height: 330,
        zIndex: 999999,
        modal: true,
        draggable: false,
        resizable: false,
        closeOnEscape: true,
        dialogClass: "modalAdDialog"
      });
	  
	  jQuery(".closeModalAdBtn").click(function() {
	  	jQuery("#modalAd").dialog('close');
	  });
	  
    } catch(e) {
      BLTSO_GUI.log("Could not initialize modal ad dialog: \n" + e.message);
    }
     
  },
  
  
  initTeaserGalleries: function() {
  	$(".gallery").each(function() {
		var items = $(this).find("figure");
		
		if (items.length > 1) {
			//$(this).parent(".gallerycontainer").addClass("armedGalleryContainer");
			
			var w = $(this).parent().parent().width(); // Viewport width
			var maxH = 500;
			var num = 0;
			
			$(items).each(function(){
				$(this).width(w);
				if ($(this).height() > maxH) {
					if($(this).width() > $(this).height()) {
						maxH = $(this).height();
						BLTSO_GUI.log("Set max height to: " + maxH);
					} else {
						$(this).addClass("portrait");
						BLTSO_GUI.log("Image is has portrait orientation = not eligible for maxH - skipping");
					}
					
					
				}
				num++;
			});
			BLTSO_GUI.log("Max height: " + maxH);
			$(this).parent(".gallerycontainer").find(".gallerynav").css("top", (maxH - 50) + "px");
			
			// Set heights
			$(items).each(function(){
				var img = $(this).find("img");
				var offs = $(img).next("figcaption").outerHeight();
				$(this).height(maxH + offs);
				
				// Resize portrait images
				if($(this).hasClass("portrait")) {
					var factor = $(img).attr("height") / maxH;
					var w = Math.round($(img).attr("width") / factor);
					var h = Math.round($(img).attr("height") / factor);
					$(img).attr("height", h);
					$(img).attr("width", w);
				}
				
				var imgH = $(img).height();
				if (imgH < maxH) { // Img height is less than max height - position it at vertical center
					$(img).css("margin-top", Math.round((maxH - imgH) / 2) + "px");
				}
			});
			
			if (num < 2) {
				return;
			}
			
			var maxForward = (num - 1) * w; // End position
			var gallery = jQuery(this);
			var nav = jQuery(this).next(".gallerynav");
			
			jQuery(nav).show();
			
				var nextBtn = jQuery(nav).find(".next a");
				var prevBtn = jQuery(nav).find(".prev a");
				
				nextBtn.click(function(){
					$(this).hide();
					prevBtn.show();
					var currLeft = parseInt(jQuery(gallery).css("left"));
					jQuery(this).blur();
					
					
					if (currLeft + maxForward == 0) {
						//alert('shit');
						BLTSO_GUI.log("Reached end - cancelling scroll");
						jQuery(this).blur();
						return false;
					}
					
					jQuery(gallery).animate({
						left: ('-=' + w)
					}, 250, function(){
						nextBtn.show();	
						BLTSO_GUI.log("Animation complete");
						
					});	
					return false;
					
				});
			
			
			prevBtn.click(function(){
				$(this).hide();
				nextBtn.show();
				var currLeft = parseInt(jQuery(gallery).css("left"));
				jQuery(this).blur();
				
				if (currLeft == 0) {
					BLTSO_GUI.log("Reached beginning - cancelling scroll");
					jQuery(this).blur();
					return false;
				}
				
				jQuery(gallery).animate({
					left: ('+=' + w)
				}, 250, function(){
					prevBtn.show();
					BLTSO_GUI.log("Animation complete");
				});
				
				return false;
			});
			
		} // if more than 1 img
		//$(".gallery").css("height","auto");
		//$(".gallery figure").css("display","block");
	});
  },


  /**
   * Add event handlers to tabbedCustomContentBox
   */
  initTabbedCustomContentBox: function() {
	  BLTSO_GUI.log("initTabbedCustomContentBox invoked");
	  try {
		  jQuery(".tabbedCustomContentBox menu li a").click(function(ev){
			  ev.preventDefault();
			  var articleId = ev.target.href.match("popularListId=(\\d*)");
			  var element = this;
			  jQuery.get(publicationUrl + "?service=ajax&type=statList&articleId=" + articleId[1], function(data) {
				jQuery(element).parents("menu.minitabs").find("li.on").removeClass("on");
			  	jQuery(element).parent().addClass("on");
				jQuery(element).parents("section.customContentBox").find("section.tabbedboxbody").html(data);
			  });
		  });
	  } catch(e) {
	  	BLTSO_GUI.log("Could not initialize tabbedCustomBox: " + e.message);
	  }
	  
	  try {
		  jQuery(".listCollection menu li a").click(function(ev){
			  ev.preventDefault();
			  var articleId = ev.target.href.match("popularListId=(\\d*)");
			  var element = this;
			  jQuery.get(publicationUrl + "?service=ajax&type=listCollection&articleId=" + articleId[1], function(data) {
				jQuery(element).parents("menu.minitabs").find("li.on").removeClass("on");
			  	jQuery(element).parent().addClass("on");
				jQuery(element).parents("section.listCollection").find("section.boxbody").html(data);
			  });
		  });
		  
	  } catch(e) {
	  	BLTSO_GUI.log("Could not initialize tabbed listCollection: " + e.message);
	  }	
	  
  },
  
  
  
  /**
   * Initialize bordered box containers, adding markup for drop shadow
   */
  initBorderedBoxContainers: function() {
  	BLTSO_GUI.log("initBorderedBoxContainers invoked");
  	if(jQuery(".borderedBoxContainer, .featureBoxFoot").length == 0) {
		return;
	}
	
	try {
		jQuery(".borderedBoxContainer, .featureBoxFoot").each(function(){
			if(!(jQuery(this).next().hasClass("bottomDropShadow"))) {
				jQuery(this).after("<div class='bottomDropShadow'></div>");				
			}
		});
	} catch(e) {
		BLTSO_GUI.log("Could not intialize borderedBoxContainers: " + e.message);
	}
  },
  
  
  /**
   * Initialize Google Map link
   */
  initMarkOnMapLink: function() {
  	jQuery(".markOnMapLink a").click(function(ev) {
		jQuery(".inputMap").toggleClass("inputMapOpen", 250);
		ev.preventDefault();
		return false;
	});
  },
  
  
  /**
   * Initialize Facebook integration
   */
  initFacebook: function() {
  	BLTSO_GUI.log("initFacebook invoked");
	
	try {
		FB.init({appId: facebookAppId, status:true, cookie:true, xfbml: true});	
		FB.Event.subscribe("auth.sessionChange",function(response) {
			if ( !response.session ) {
				// TODO: handle facebook disconnect
			}
		});
		
	} catch(e) {
		BLTSO_GUI.log("Could not initialize Facebook integration: " + e.message);
	}
	
	
  },
  
  
  
  initAddThis: function() {
  	BLTSO_GUI.log("Initializing article sharing functionality");
	var success;
	try {
		
		if ( jQuery(".addthis_toolbox").length>0 ) {
			BLTSO_GUI.log("Found AddThis widget - adding event listener");
			var guid = jQuery.Guid.New();
			var url = location.href;
			var articleId = location.href.match("\\d*(?=\\)\\.gm)");
			
			BLTSO_GUI.log("articleId: [" + articleId + "]");
			
			if ( location.href.indexOf("?")==-1 ) {
				url += "?r=" + guid;
			} else {
				url += "&r=" + guid;
			}
			
			BLTSO_GUI.log("URL: " + url);
			
			// Append recommendation guid to current url
			jQuery(".addthis_toolbox a").attr("addthis:url",url);
			
			// Create direct URL link and related field
			$(".directUrlLink a").click(function(ev){
				ev.preventDefault();
				createRecommendationEntry(articleId[0], guid, true, url);
			});
			
			// Add event listener to addThis widget
			addthis.addEventListener('addthis.menu.share',function() {
				createRecommendationEntry(articleId[0], guid);
			});
			
		} else {
			BLTSO_GUI.log("Found no AddThis widget in page");
		}
		
	} catch(e) {
		BLTSO_GUI.log("Could not initialize AddThis event listener: " + e.message);
	}
	
	
	
	// Create recommendation entry when shared
	function createRecommendationEntry(artId, guid, directURL, url) {
		$.post(publicationUrl + "auth/ajax/createRecommendation.do", { articleId: artId , guid: guid }, function(data) {
			if(data.success != "true"){
				BLTSO_GUI.log("POST to auth/ajax/createRecommendation.do failed");
				BLTSO_GUI.log("data: " + data);
				BLTSO_GUI.log("data.success: " + data.success);
				$("#addThisRecommendationMessage").html("Kunde inte skapa rekommendation");
			} else {
				logRecommendation(articleId[0], guid);
				if(directURL) {
					$("#directUrl").val(url);
					$(".directUrlField").toggle();
					$("#directUrl").focus();
					$("#directUrl").select();
				}
			}
		}, "json");
	}
	
	// Log recommendation
	function logRecommendation(artId, guid) {
  		$.get(publicationUrl + "?service=ajax&type=eaeRecommendationLogger",{articleId: artId}, function(data) {
			$("#addThisRecommendationMessage").html(data);	
		});
  	}
	
  },
  
  
  
  
  
  /**
   * Arm login form for Ajax
   */
  initLoginFormAction: function() {
    BLTSO_GUI.log("initLoginFormAction invoked");
	
	// Submit form
    $("#loginpageform").submit(function(ev) {
    	BLTSO_GUI.log("Login form submit intercepted - showing Ajax spinner and hiding error messages...");
        
        $("#loadingOverlay").show(); 	  	   
      });
  },
  
  
  /**
   * Generic Ajax form
   * @param {Object} formId:                      The html id attribute of the form
   * @param {Object} sourceSelector [optional]:   If specified, this selector will be extracted from and replace the response 
   * @param {Object} targetSelector [optional]:   Target selector to paste the response into. (Default selector = ".formPostResponse").
   * 
   * Depends on jquery.form.js
   */
  postForm: function(formId, sourceSelector, targetSelector, redirectURL, hideSelector) {
	var queryString = "";
	var url = jQuery(formId).attr("action");       // Get form action URL
	var redirectURL = window.location;
	if(BLTSO_GUI.LOGIN_REDIRECT_HOME) {
		redirectURL = (publicationUrl + BLTSO_GUI.MY_PAGE_URL);
	}
	
	BLTSO_GUI.log("Redirect home post login: " + BLTSO_GUI.LOGIN_REDIRECT_HOME);
	BLTSO_GUI.log("RedirectURL: " + redirectURL);
	
	try {
		queryString = jQuery(formId).formSerialize();  // Get form fields as URL query string	
	} catch(e) {
		BLTSO_GUI.log("Failed to serialize login form locally: " + e.message);
	}
    
	BLTSO_GUI.log("QueryString = " + queryString);
	BLTSO_GUI.log("URL = " + url);
	
	BLTSO_GUI.log("formId = " + formId);
	BLTSO_GUI.log("sourceSelector = " + sourceSelector);
	BLTSO_GUI.log("targetSelector = " + targetSelector);
	
    // targetSelector fallback
    if (!targetSelector) {
      targetSelector = formId + " .formPostResponse";
    }
    
    // Do the form submission with Ajax
    jQuery.post(url, queryString, function(data) {
	  var purgedData = data;
	  if(sourceSelector) { // If sourceSelector, extract it from response DOM
        purgedData = jQuery(sourceSelector, jQuery(data));
      }
	  
	  if(purgedData.html()) { // Response contains error message - show it in popup
		jQuery(targetSelector).html(purgedData);
		jQuery(targetSelector).show();
		if(hideSelector)
			jQuery(hideSelector).hide();
	  } else { // No error message - redirect to redirectURL
	  	window.location = redirectURL;
	  }
    }, "html"); 

    return false;
    
  },
  
  
  /**
   * Initialize Google Map implementation for input map, the map used in article input form
   */
  initInputGoogleMap: function () {
		if(!jQuery("#map_canvas")) {
			return;
		}
		
		var point;
		var gMark;

		var bounds;
		var southWest;
		var northEast;

		var lngSpanGmap = "59.32198054010197";
		var latSpanGmap = "18.072509765625";
		var lngSpan;
		var latSpan;
		
		try {
			
			var map = new GMap2(document.getElementById('map_canvas'));
			var burnsvilleMN = new GLatLng(lngSpanGmap, latSpanGmap);
			var geocoder = new GClientGeocoder();
			
	      	var zoom = jQuery("input[name='gmapZoomLevel']").val(); 
			if(jQuery("input[name='gmapZoomLevel']").val() == "" || jQuery("input[name='gmapZoomLevel']").val() == null){
				zoom = 7; 
			}
	
			map.setCenter(burnsvilleMN,parseInt(zoom));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
		
		    function addAddressToMap(response){
		     	map.clearOverlays();
		     	if (!response || response.Status.code != 200) {
		     		
		     	 removeGmapInput();
		     	}
		     	else
		     	{
					place = response.Placemark[0];
			   		point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
		   			marker = new GMarker(point);
		   			map.addOverlay(marker);
		   	 		jQuery("input[name='gmapLatittude']").val(place.Point.coordinates[1]);
					jQuery("input[name='gmapLongitud']").val(place.Point.coordinates[0]);
					jQuery("input[name='gmapZoomLevel']").val(map.getZoom());
		
					 marker.openInfoWindowHtml(place.address);
		     	}
		    }
		    
		    function settGMarkerPoint(){
		
		    	//alert(jQuery("input[name='gmapLatittude']").val());
				if(jQuery("input[name='gmapZoomLevel']").val() == "" || jQuery("input[name='gmapZoomLevel']").val() == null){
				} else {
					lngSpanGmap = jQuery("input[name='gmapLongitud']").val();
					latSpanGmap = jQuery("input[name='gmapLatittude']").val();	
		    	}
				// alert(lngSpanGmap+"--"+latSpanGmap); 
		    	var point = new GLatLng(lngSpanGmap, latSpanGmap);
		
		    	map.setCenter((point), parseInt(zoom));
		    	gMark = new GMarker(point);
				map.addOverlay(gMark);
		
		
		    }
		
			  	GEvent.addListener(map, "click", function(overlay,latlng) {
		      		if (overlay) {
		        		return;
		      		}
		
			        map.clearOverlays();
		
					var tileCoordinate = new GPoint();
					var tilePoint = new GPoint();
					var currentProjection = G_NORMAL_MAP.getProjection();
					tilePoint = currentProjection.fromLatLngToPixel(latlng, map.getZoom());
					tileCoordinate.x = Math.floor(tilePoint.x / 256);
					tileCoordinate.y = Math.floor(tilePoint.y / 256);
		
					bounds = map.getBounds();
					southWest = bounds.getSouthWest();
					northEast = bounds.getNorthEast();
					lngSpan = northEast.lng() - southWest.lng();
					latSpan = northEast.lat() - southWest.lat();
		
					point = new GLatLng(latlng.lat(),latlng.lng());
					gMark = new GMarker(point);
		
					map.addOverlay(gMark);
		
			 		jQuery("input[name='gmapLatittude']").val(latlng.lat());
			 		jQuery("input[name='gmapLongitud']").val(latlng.lng());
					jQuery("input[name='gmapZoomLevel']").val(""+map.getZoom());
					
			     });
				
				/*
		   		jQuery("#map").hide();
		   		jQuery("#mapDel").hide();
		   		jQuery(".GMapLink").hide();
		   		*/
		
			    var str = "";
			    var mytool_array;
		
					 jQuery("#communeeditDiv").change(function () {
						if(jQuery("#GID").val() == 0){
						      jQuery("#communeeditDiv option:selected").each(function () {  
						          str = jQuery(this).attr('name') + " ";
							   }); 
					          if(str != "Hela landet "){
					   		   map.setZoom(7);
							   removeGmapInput();
					   		   geocoder.getLocations(str+",sweden", addAddressToMap);
							  } else{
							     map.setCenter(burnsvilleMN,4);
							     map.clearOverlays();
							  }
						} else {
					 		jQuery("#GID").val("0");
					 	}
				    }).change();
		 		
				    jQuery("#commune").change(function () {
				          jQuery("#commune option:selected").each(function () {
				          str = jQuery(this).attr('name') + " ";
				          jQuery('#mapDel').toggle(0);
		    			  jQuery('#mapDel').toggle(0);
				           });
				           if(str != "Hela landet "){
				           	 map.setZoom(7);
						     geocoder.getLocations(str+",sweden", addAddressToMap);
						     }
						   else{
						     map.setCenter(burnsvilleMN,4);
						     map.clearOverlays();
							 removeGmapInput();
						   }
				    }).change();
		
				    jQuery("#category").change(function () {
				       var str2;
				          jQuery("#category option:selected").each(function () {
				          str2 = jQuery(this).attr('name');
		
				          	if(str2 == "The Leif Show"){
		          		   		jQuery("#commune").hide();
		          		      jQuery(".commune").hide();
		          		   		jQuery("#map").hide();
		          		   		jQuery(".GMapLink").hide();
		          		   		jQuery("#mapDel").hide();
				          	} else if(str2 == "Utrikes"){
				          		jQuery("#commune").hide();
				          	   jQuery(".commune").hide();
		          		   		jQuery(".GMapLink").show();
		          		   	// map.clearOverlays();
		          		   		//removeGmapInput();
				          	} else {
				          	   jQuery(".commune").show();
				        	  	jQuery("commune").show();
				        	  	jQuery("#commune").show();
		          		   		jQuery(".GMapLink").show();
				          	}
		
				           });
				        }).change();
		
			function removeGmapInput(){
				jQuery("input[name='gmapLatittude']").val("");
			 	jQuery("input[name='gmapLongitud']").val("");
				jQuery("input[name='gmapZoomLevel']").val("");
			}
		
		     jQuery("#uploadForm\\:file").change(function () {
		     	 jQuery('#addAnother').show();
		     });
		
		   jQuery('#fileUplod').hide();
		   jQuery('#map').hide();
		   jQuery('#mapDel').hide();
		   jQuery("#commune").hide();
		   jQuery(".commune").hide();
		   
		   jQuery('a.GMapLink').click(function() {
			
			   settGMarkerPoint(); 
		
			   jQuery('#youtubeVideoPree').hide();
			   jQuery('#youtube').hide(400); 
			   jQuery('#map').toggle(400);
			   return false; 
		    });
		   
		   jQuery('a.GMapLink').click(function() { jQuery('#mapDel').toggle(400); return false; });
		   jQuery("a#mapDel").click(function() {    map.clearOverlays();  removeGmapInput();  });
			
		} catch(e) {
			BLTSO_GUI.log("Google Maps threw exception: " + e.message);
		}
		
	   
	},
  
  initImagePopup: function() {
	  var overlayDiv = $(
			'<div class="overlayDiv">' +
			'<span class="close"></span>' +
			'<img src="" alt="" />' +
			'<p class="desc">Image has no description</p>' + 
		'</div>');
			
	  $('a[rel="popupDiv"]').click(function(e){
			e.preventDefault();
			var self = $(this);
			var position = self.position();
			var axisX = position.left;
			var axisY = position.top;
			
			overlayDiv.css({
				'left' : axisX,
				'top' : axisY + 100
			});	
			
			var src = self.attr('href');			
			overlayDiv.find('img').attr('src', src);
			var desc = self.next('figcaption').text();
			
			overlayDiv.find('.desc').text(desc);
			
			//show overlay div
			overlayDiv.appendTo('body');
			
			$(overlayDiv).find('.close').click(function(){
				overlayDiv.remove();
				overlayDiv.find('img').attr('src', '');
			});
		});	
  },
  
  log: function(msg, isError) {
    // If not in dev mode, disable clientside logging
    if (!BLTSO_GUI.DEV_MODE) { 
      return;
    }
    
    BLTSO_GUI.ERRORS += "\n\n";
    BLTSO_GUI.ERRORS += msg;
    
    try {
      //alert(msg);
      console.log(msg);     
    } catch(e) {
      //alert(msg);
    }
  }
  
  
  
};


/**
 * vjustify - justify heights of elements in given JQuery selector
 * by: Michael Futreal, see: http://michael.futreal.com/jquery/vjustify
 */
jQuery.fn.vjustify = function() {
    BLTSO_GUI.log("VJustify invoked");
  var maxHeight=0;
    this.each(function(){
        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
    });
  BLTSO_GUI.log("Max height found: " + maxHeight);
    this.each(function(){
        BLTSO_GUI.log("Setting max height for element");
    jQuery(this).height(maxHeight + "px");
        if (this.offsetHeight>maxHeight) {
            jQuery(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
        }
    });
};


// Init GUI
jQuery(document).ready(function() {
  BLTSO_GUI.init();
});
