(function ($) {

Drupal.behaviors.artistsNews = {
  attach: function(context) {
    if( window.location.hash == "#news" ) {
      $(".exhibition-gallery").hide();
      $(".artist-news").show();
    };
    
    $('.news-show').click(function(){
      $(".exhibition-gallery").fadeOut('fast', function() {
        $(".artist-news").fadeIn();
      });
    });
    
    $('.works-show').click(function(){
      $(".artist-news").fadeOut('fast', function() {
        $(".exhibition-gallery").fadeIn();    
      });
    });  
      
  }
};

Drupal.behaviors.localTasksShortcuts = {
  attach: function(context) {
    $('section#section-content ul.tabs').prependTo('.toolbar-shortcuts ul.menu');
  }
};

Drupal.behaviors.expandText = {
  attach: function(context) {
    $('.expandable-text', context).find('.expand a').click(function() {
      var $expandable = $(this).parents('.expandable-text')
      if( $expandable.hasClass('expanded') ) {
        $expandable.find('.expandable-element').slideUp();      
        $expandable.removeClass('expanded').addClass('collapsed');
      } else {
        $expandable.find('.expandable-element').slideDown();
        $expandable.removeClass('collapsed').addClass('expanded');
      }
      return false;
    });
  }
};



Drupal.behaviors.artistsIndex = {
  attach: function(context) {
    var nid = 0;
    $('#region-content', context).find('.artist-teaser-item').hover(
      function () {
        nid = $(this).data('node-id');
        $('section.block-artists').find('li[data-node-id="'+nid+'"] a').addClass('active');
      }, 
      function () {      
        $('section.block-artists').find('li[data-node-id="'+nid+'"] a').removeClass('active');
      }
    );

    $('section.block-artists', context).find('li').hover(
      function () {
        nid = $(this).data('node-id');
        $('#region-content').find('figure[data-node-id="'+nid+'"]').addClass('hover');
      }, 
      function () {      
        $('#region-content').find('figure[data-node-id="'+nid+'"]').removeClass('hover');
      }
    );
  }
};

})(jQuery);;
(function ($) {

Drupal.behaviors.googleMap = {
  attach: function(context) {
      if( $('#google-map-canvas').length > 0 ) {
        googleMapInitialize();  
      }
  }
};

var map;
var fTolv;
var hellviK;
var ftolv = new google.maps.LatLng(59.328698,18.064753);
var hellvi = new google.maps.LatLng(57.756938,18.879619);
	  
function SthlmControl(SthlmCtrlDiv, map) {
  SthlmCtrlDiv.style.padding = '0 20px 20px';
  var SthlmCtrlUI = document.createElement('DIV');
  SthlmCtrlUI.style.color = '#FFF !important;';
  SthlmCtrlUI.style.fontSize = '16px';
  SthlmCtrlUI.style.fontWeight = '100 !important;';
  SthlmCtrlUI.style.fontFamily = '"GeorgiaItalic"';
  SthlmCtrlUI.style.cursor = 'pointer';
  SthlmCtrlUI.title = 'Click to set the map to Home';
  SthlmCtrlDiv.appendChild(SthlmCtrlUI);

  var SthlmCtrlText = document.createElement('DIV');
  SthlmCtrlText.innerHTML = '<b>Sthlm</b>';
  SthlmCtrlUI.appendChild(SthlmCtrlText);

  google.maps.event.addDomListener(SthlmCtrlUI, 'click', function() {
  		  map.setCenter(ftolv)
  });
}

function HellviControl(HellviCtrlDiv, map) {
  HellviCtrlDiv.style.padding = '0 20px 20px';

  var HellviCtrlUI = document.createElement('DIV');
  HellviCtrlUI.style.color = '#FFF !important;';
  HellviCtrlUI.style.fontSize = '16px';
  HellviCtrlUI.style.fontWeight = '100 !important;';
  HellviCtrlUI.style.fontFamily = '"GeorgiaItalic"';
  HellviCtrlUI.style.cursor = 'pointer';
  HellviCtrlUI.title = 'Click to set the map to Home';
  HellviCtrlDiv.appendChild(HellviCtrlUI);

   var HellviCtrlText = document.createElement('DIV');
  HellviCtrlText.innerHTML = '<b>Hellvi</b>';
  HellviCtrlUI.appendChild(HellviCtrlText);

  google.maps.event.addDomListener(HellviCtrlUI, 'click', function() {
  		  map.setCenter(hellvi)
  });
}

function googleMapInitialize() {
  var fTolv = {
    zoom: 17,
    center: ftolv,
    mapTypeId: google.maps.MapTypeId.SATELLITE,
    panControl: false,
    zoomControl: false,
    scaleControl: false,
    mapTypeControl: false,
    rotateControl: false,
    overviewMapControl: false
  };

  map = new google.maps.Map(document.getElementById('google-map-canvas'), fTolv);

	var populationOptions = {
	  strokeColor: "#FFFFFF",
	  strokeOpacity: 1,
	  strokeWeight: 4,
	  fillColor: "#FFFFFF",
	  fillOpacity: 0,
	  map: map,
	  center: ftolv,
	  radius: 15
	};
	var populationOptionsTwo = {
	  strokeColor: "#FFFFFF",
	  strokeOpacity: 1,
	  strokeWeight: 4,
	  fillColor: "#FFFFFF",
	  fillOpacity: 0,
	  map: map,
	  center: hellvi,
	  radius: 15
	};

	circle = new google.maps.Circle(populationOptions);
	circleTwo = new google.maps.Circle(populationOptionsTwo);
	
	google.maps.event.addDomListener(circle, 'click', function() {
		map.setCenter(ftolv);
		window.open('http://maps.google.se/maps?q=Fredsgatan+12,+Stockholm&hl=sv&sll=57.756893,18.880399&sspn=0.009742,0.015235&vpsrc=0&z=16');
	});
	
	google.maps.event.addDomListener(circleTwo, 'click', function() {
		map.setCenter(hellvi);
		window.open('http://maps.google.se/maps?q=Hellvi+K%C3%A4nnungs+207,+Gotland&hl=sv&ie=UTF8&sll=57.75103,18.873439&sspn=0.019488,0.03047&vpsrc=0&t=h&z=16');
	});
  	
	var sthlmCtrlDiv = document.createElement('DIV');
	    var sthlmCtrl = new SthlmControl(sthlmCtrlDiv, map);

	    sthlmCtrlDiv.index = 1;
	    map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(sthlmCtrlDiv);

	    var hellviCtrlDiv = document.createElement('DIV');
	    var hellviCtrl = new HellviControl(hellviCtrlDiv, map);

	    hellviCtrlDiv.index = 1;
      map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(hellviCtrlDiv);
}

})(jQuery);;
(function ($) {

Drupal.behaviors.newsletterSignup = {
  oWidth: 0,
  attach: function(context) {
    var $form = $('#main-menu .newsletter form', context);
    
    var oWidth = $form.outerWidth();
    oWidth -= parseInt($form.css("paddingRight"), 10);
    oWidth -= parseInt($form.css("paddingLeft"), 10);
    this.oWidth = oWidth;
    
    $form.submit(function() {
      if ($(this).hasClass('active')) {
        var valid = $form.validate({
              rules: { 
                  mail: {
                    email: true,
                    required: true
                  }
              },
              messages: {
                mail: ""
              }
              }).element('.form-text');
        if (valid) {
          $form.addClass('submitted transmitting');
          $.post('/ajax/newsletter', $form.serialize(), function(response) { Drupal.newsletterSignup.submit(response, $form) }, "json");
        }     
      }
      return false;
    });

    $form.find(':submit').click(function() {
      if (!$(this).parents('form').hasClass('active')) {      
        $(this).parents('form').stop().animate({ width: '225px'}, 'fast', function() {
          $(this).attr('value','Subscribe');
          $(this).siblings('.form-item').fadeIn('fast').find('input').focus();
        }).addClass('active');
      }
    });

    $form.mouseenter(
      function() {
        $(this).stop().animate({ width: '225px'}, 'fast', function() {
          $(this).find(':submit').attr('value','Subscribe');
          $(this).find('.form-item').fadeIn('fast').find('input').focus();
        }).addClass('active');
      }
    ).mouseleave(
      function() {
        if(!$(this).hasClass('submitted')) {
          $(this).find('.form-item').hide();
          $(this).stop().animate({ width: oWidth +'px'}).find(':submit').attr('value','Newsletter').removeClass('active');
        }
      }
    );
  }
};

Drupal.newsletterSignup = {
  submit: function(response, $form) {
    $form.removeClass('transmitting');
    $form.find('.form-item').hide();
    $form.find(':submit').hide(function() {
      $form.children('div').prepend(response.message);
    });
    
    setTimeout( function() { 
      $('li.newsletter').find('.form-response').fadeOut(function() {
        var oWidth = Drupal.behaviors.newsletterSignup.oWidth;
        $(this).parents('form').removeClass('submitted active').stop().animate({ width: oWidth +'px'}).find(':submit').attr('value','Newsletter');
        $(this).siblings('.form-submit').show();
        $(this).remove();
      });
    }, 2000 );
  }
}

})(jQuery);;

