﻿/* Popup
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function tmSizedPopup(url,winName,w,h) {	
    var useScroll = arguments[4] == undefined?0:arguments[4];
	popWin = window.open(url,winName,'fullscreen=0,width='+w+',height='+h+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+useScroll+',resizable=1');
}


/* Email-protection
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function ShowEmail(i_where, i_beforeAt, i_afterAt, i_afterDot, i_additional, i_middle) {
    if (i_additional == null)
        i_additional = "";
    builtStr = i_beforeAt + "@" + i_afterAt + "." + i_afterDot;
    if (i_middle != null) {
        if (i_middle != "")
            firstName = i_middle;
        else
            firstName = builtStr;
    }
    else {
        splitByDot = i_beforeAt.split('.');
        firstLetter = splitByDot[0].toUpperCase().charAt(0);
        firstName = firstLetter + splitByDot[0].substring(1);
        lastName = "";
        if (splitByDot.length > 1) {
            firstLetter = splitByDot[1].toUpperCase().charAt(0);
            lastName = firstLetter + splitByDot[1].substring(1);
            firstName += " " + lastName;
        }
    }

    $("#" + i_where).append(i_additional + '<a href="mailto:' + builtStr + '">' + firstName + '</a>');
}


/* Login
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
var OneClickLogin = {
    _keyPressCount: 0,
	init : function()
	{
		$(document).bind("keyup", OneClickLogin.eventHandler);
	}, 
	
	eventHandler : function(ev)
	{
		ev = (typeof(ev)== "undefined") ? window.event : ev;
		if(ev.type == "keyup")
			return OneClickLogin.keyHandler(ev);
	}, 
	
	keyHandler : function(ev)
	{
	    var kc = ev.keyCode || ev.which;
	    
	    if($("#ocAdminLogin").length > 0 && kc==27)
	        $("#ocAdminLogin").remove();

	        
		if(kc == 17 || kc == 32)
			this._keyPressCount++;

		else
			this._keyPressCount = 0;
			
		if(this._keyPressCount > 2)
		{
			window.scrollTo(0,0);			
			this.addLoginBox();
			this._keyPressCount = 0;
			this.cancelEvListener(ev);	
			return false;
		}
	}, 
	
	cancelEvListener : function(ev)
	{
		if(typeof(ev.preventDefault) != "undefined")
		{
			ev.preventDefault();
		}
		ev.returnValue = false;
		ev.cancelBubble = true;
	}, 
	
	addLoginBox : function()
	{
		if($("#ocAdminLogin").length != 0 || $("#ocAdminToolbar").length != 0)
			return;
		
		var $loginContainer = $('<div />').attr('id','ocAdminLogin');
		var $loginIframe = $('<iframe />').attr({
			scrolling:'no',
			frameborder:'no',
			src: this.loginUrl+'?redirect='+this.scriptName()+ "&random=" + new Date().getTime()
		});
        $("body").prepend($loginContainer);
        $loginContainer.append($loginIframe);
	},
	
	scriptName:function()
	{
		var l = location.href;
		if(l.indexOf('http://') == 0)
			l = l.substring(7);
		l = l.substring(l.indexOf("/"));
		return l;		
	}

}

function CalendarSetDate(i_date, i_id) {
    if (calendar != null && calendar != "")
        calendar.jSelect(i_date.replace("-", "/").replace("-", "/"));
    $(".tmpl-evenemang").addClass("loading").load("/ajax/Calendar.aspx", { date: i_date, id: i_id }, function (data) {
        $(this).removeClass("loading");
        if (i_id != null && i_id != "") {
            title = $("#event-" + i_id + " .post-head h2 a").html();
            content = "";
            $element = $("#event-" + i_id + " .post-content");
            if ($element != null)
            {
                content = $element.html();
            }
            $("#ctl00_CmsTitle").attr("content", title + " | Evenemang | Campus Skellefteå");
            $("html head title").html(title + " | Evenemang | Campus Skellefteå");
            $("#ctl00_CmsDescription").attr("content", content);
        }
    });
}

function nohtml(iInput) {
    return iInput.replace(new RegExp("<[^>]*>", "gi"), "");
}

OneClickLogin.loginUrl = "/admin/OneClick_loginbox/OneClick_login.aspx";
OneClickLogin.init();
var calendar = "";


/* Add widget-content
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function FetchID(i_selectorParent, i_value) {
    var i_type = i_selectorParent.attr("id");
    //alert("fetching type: " + i_type + " value: " + i_value);
    $.post("/ajax/WidgetAjax.aspx", { id: i_value, type: i_type }, function (data) {
        if (i_selectorParent.find(".rss-content") != null)
            i_selectorParent.find(".rss-content").remove();
        if (i_selectorParent.find(".widget-content") != null)
            i_selectorParent.find(".widget-content").remove();
        if (i_selectorParent.find(".badge") != null)
            i_selectorParent.find(".badge").remove();
        i_selectorParent.find(".external").remove();
        i_selectorParent.find(".custom-select-wrapper").after(data);
        //window.location.hash = $widget.attr("id");
    });
}

/* Easing equations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend(jQuery.easing,
{
  def: 'easeOutQuad',
  swing: function (x, t, b, c, d) {
    //alert(jQuery.easing.default);
    return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
  },
  easeInQuad: function (x, t, b, c, d) {
    return c * (t /= d) * t + b;
  },
  easeOutQuad: function (x, t, b, c, d) {
    return -c * (t /= d) * (t - 2) + b;
  },
  easeInOutQuad: function (x, t, b, c, d) {
    if ((t /= d / 2) < 1) return c / 2 * t * t + b;
    return -c / 2 * ((--t) * (t - 2) - 1) + b;
  },
  easeInCubic: function (x, t, b, c, d) {
    return c * (t /= d) * t * t + b;
  },
  easeOutCubic: function (x, t, b, c, d) {
    return c * ((t = t / d - 1) * t * t + 1) + b;
  },
  easeInOutCubic: function (x, t, b, c, d) {
    if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
    return c / 2 * ((t -= 2) * t * t + 2) + b;
  },
  easeInQuart: function (x, t, b, c, d) {
    return c * (t /= d) * t * t * t + b;
  },
  easeOutQuart: function (x, t, b, c, d) {
    return -c * ((t = t / d - 1) * t * t * t - 1) + b;
  },
  easeInOutQuart: function (x, t, b, c, d) {
    if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
    return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
  },
  easeInQuint: function (x, t, b, c, d) {
    return c * (t /= d) * t * t * t * t + b;
  },
  easeOutQuint: function (x, t, b, c, d) {
    return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
  },
  easeInOutQuint: function (x, t, b, c, d) {
    if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
    return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
  },
  easeInSine: function (x, t, b, c, d) {
    return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
  },
  easeOutSine: function (x, t, b, c, d) {
    return c * Math.sin(t / d * (Math.PI / 2)) + b;
  },
  easeInOutSine: function (x, t, b, c, d) {
    return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
  },
  easeInExpo: function (x, t, b, c, d) {
    return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
  },
  easeOutExpo: function (x, t, b, c, d) {
    return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
  },
  easeInOutExpo: function (x, t, b, c, d) {
    if (t == 0) return b;
    if (t == d) return b + c;
    if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
    return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
  },
  easeInCirc: function (x, t, b, c, d) {
    return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
  },
  easeOutCirc: function (x, t, b, c, d) {
    return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
  },
  easeInOutCirc: function (x, t, b, c, d) {
    if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
    return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
  },
  easeInElastic: function (x, t, b, c, d) {
    var s = 1.70158; var p = 0; var a = c;
    if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3;
    if (a < Math.abs(c)) { a = c; var s = p / 4; }
    else var s = p / (2 * Math.PI) * Math.asin(c / a);
    return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
  },
  easeOutElastic: function (x, t, b, c, d) {
    var s = 1.70158; var p = 0; var a = c;
    if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3;
    if (a < Math.abs(c)) { a = c; var s = p / 4; }
    else var s = p / (2 * Math.PI) * Math.asin(c / a);
    return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
  },
  easeInOutElastic: function (x, t, b, c, d) {
    var s = 1.70158; var p = 0; var a = c;
    if (t == 0) return b; if ((t /= d / 2) == 2) return b + c; if (!p) p = d * (.3 * 1.5);
    if (a < Math.abs(c)) { a = c; var s = p / 4; }
    else var s = p / (2 * Math.PI) * Math.asin(c / a);
    if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
    return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
  },
  easeInBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c * (t /= d) * t * ((s + 1) * t - s) + b;
  },
  easeOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
  },
  easeInOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
    return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
  },
  easeInBounce: function (x, t, b, c, d) {
    return c - jQuery.easing.easeOutBounce(x, d - t, 0, c, d) + b;
  },
  easeOutBounce: function (x, t, b, c, d) {
    if ((t /= d) < (1 / 2.75)) {
      return c * (7.5625 * t * t) + b;
    } else if (t < (2 / 2.75)) {
      return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
    } else if (t < (2.5 / 2.75)) {
      return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
    } else {
      return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
    }
  },
  easeInOutBounce: function (x, t, b, c, d) {
    if (t < d / 2) return jQuery.easing.easeInBounce(x, t * 2, 0, c, d) * .5 + b;
    return jQuery.easing.easeOutBounce(x, t * 2 - d, 0, c, d) * .5 + c * .5 + b;
  }
});


/* DOCUMENT READY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
$(function () {

  $(".custom-select-wrapper select").live("change", function () {
    var $selectorParent = $(this).parents(".widget");
    var value = $(this).val();
    FetchID($selectorParent, value);

    var date = new Date();
    date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
    //var $widget = $selectorParent.parent();
    var theId = $selectorParent.attr("id").split('-')[1];
    $.cookie("widget-" + theId, value, { path: '/', expires: date });
    //alert("saving cookie:" + "widget-" + theId + " value: " + value);

    return false;
  });

  $(".custom-select-wrapper input").bind('click', function () { return false; });

  $(".widget-wrapper .widget").each(function () {
    var $this = $(this),
			theId = $this.attr("id").split('-')[1];

    if (theId != null) {
      //alert("loading cookie: " + "widget-" + theId + " value " + $.cookie("widget-" + theId));
      var val = $.cookie("widget-" + theId);

      if (val != null) {
        var $selector = $this.find(".custom-select-wrapper select")
        $selectorParent = $selector.parents(".widget");

        $selector.val(val); // set val inside selector
        FetchID($selectorParent, val);
      }
    }
  });


  /* Bind event to paging on events page */
  /*
  $(".tmpl-evenemang .prev, .tmpl-evenemang .next").live("click", function () {
  var splitByAnd = $(this).attr("href").split('&');
  var date = splitByAnd[0].split('=')[1];
  var id = "";
  if (splitByAnd.length > 1)
  id = splitByAnd[1].split('=')[1];
  CalendarSetDate(date, id);
  return false;
  });*/

  /* Add class to body if user has js enabled
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  $('body').addClass('js-enabled');


  /* Remove margin from top text if it is empty
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  if ($('.header > .text').length && $('.header > .text').html().length < 2) {
    $('.header > .text').addClass('no-margin');
  }

  /* Add margin to top if image is empty
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  if ($('.header > .image').length && $('.header > .image').html().length < 2) {
    $('.header > .text').addClass('add-margin');
  }


  /* MAP WIDGET
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  $('#map-nav').find('.category ul a').bind('mouseover', function () {
    var _this = $(this),
        text = _this.text(),
        location = _this.attr('data-location') || false,
        hotspots = $('#hotspots');

    if (location) {
      _this.removeAttr('title');

      if ($.support.opacity) {
        hotspots.find('li').fadeOut();
        hotspots.find('.' + location).find('span').text(text).end().stop(true, true).fadeIn();
      }
      else {
        hotspots.find('li').hide();
        hotspots.find('.' + location).find('span').text(text).end().show();
      }
    }
  }).bind('mouseout', function () {
    if ($.support.opacity) {
      $('#hotspots').find('li').stop(true, true).fadeOut();
    }
    else {
      $('#hotspots').find('li').stop(true, true).hide();
    }
  });

  /* Style selectbox
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  $('.custom-select-wrapper select').each(function () {
    var $this = $(this),
			$mask = $('<div class="overlay" />')
						.text($this.find(':selected').text())
						.prependTo($this.parent());

    $this.bind('change', function () {
      $mask.text($this.find(':selected').text());
    });

    $this.css('opacity', '0').addClass('custom-js-styled-select');
  });

  /* Widget switch
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  /*
  $(".custom-select-wrapper select").live("change", function () {
  var widgetSelect = $(this);
        
  var widgetWrapper = widgetSelect.parents(".widget");
  var widgetType = widgetWrapper.attr('class').split('-')[1];
  var widgetId = widgetWrapper.attr("id").split('-')[1];
  $(widgetWrapper).addClass("loading").load("/ajax/Widget.aspx?widgetId=" + widgetId + "&widgetType=" + widgetType + "&select=" + widgetSelect.val() + " fieldset", function (data) {
  //alert(data);
  $(this).removeClass("loading");
  });
  });*/

  var href = $(".s-facebook").attr("href");
  //newHref = href.replace("&t=", "%23" + window.location.hash + "&t=");
  var hash = window.location.hash;
  hash = hash.replace("#", "%23");
  $(".s-facebook").attr("href", href.replace("&t=", hash + "&t="));

  /* Apple-like widget sidebar
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  /*
  var widget_wrapper = $('.widget-wrapper'),
  widget_wrapper_height = $('.widget-wrapper').outerHeight(),
  widget_wrapper_start_offset = widget_wrapper.offset().top,
  footer_offset = $('#footer').offset().top,
  calendar_height = 0,
  map_height = 0;
		
  if ( ($.browser.mozilla || $.browser.msie) && $('#map').length > 0 ) {
  map_height = 260;
  widget_wrapper_start_offset += map_height - 18;
  footer_offset += map_height;
  }
		
  if ( ($.browser.mozilla || $.browser.msie) && $('#calendar').length > 0 ) {
  calendar_height = 249;
  widget_wrapper_start_offset += calendar_height - 18;
  footer_offset += calendar_height;
  }

  function floating_sidebar() {

  if ($(window).height() > widget_wrapper_height + 36) {
  if ($('#primary-content').outerHeight() > $('#sidebar').outerHeight()) {

  var window_offset = $(window).scrollTop();

  if (((window_offset + widget_wrapper_height) <= (footer_offset - 20)) && widget_wrapper.hasClass('pinned-bottom')) {
  widget_wrapper.removeClass('pinned-bottom');
  }
  else if ((window_offset <= widget_wrapper_start_offset) && widget_wrapper.hasClass('pinned-top')) {
  widget_wrapper.removeClass('pinned-top');
  }
  else if ((window_offset > widget_wrapper_start_offset) && !widget_wrapper.hasClass('pinned-top')) {
  widget_wrapper.addClass('pinned-top');
  }
  else if (((window_offset + (widget_wrapper_height)) > footer_offset) && !widget_wrapper.hasClass('pinned-bottom')) {
  widget_wrapper.addClass('pinned-bottom');
  }
  }
  }
  }

  $(window).bind('scroll', floating_sidebar);
  floating_sidebar();
  */

  /* Enhance admin UI
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  if (!$.browser.msie) {
    $('.oc-controls').css({ display: 'none' });

    $('.logged-in').bind('mouseenter', function () {
      $(this).addClass('admin-hover');
      $(this).find('.oc-controls').css({ display: 'block' });
    });
    $('.logged-in').bind('mouseleave', function () {
      $(this).removeClass('admin-hover');
      $(this).find('.oc-controls').css({ display: 'none' });
    });
  }
});
