(function($) {

	$(function() {
		
		var isIE =  $('html').hasClass('ie');
		var isIE6 =  $('html').hasClass('ie6');
		
		$('#keyword').focus(function(){
			var currentVal = $(this).val();									// Read initial field value
            if (currentVal == 'search')
			$(this).val('');														// Clear initial value
		});

		$('#keyword').blur(function(){
            var obj = $(this);
            if (obj.val().length == 0) {
                obj.val('search');
            }
        });

        $('.do_search').click(function(){
            $('#search_form').submit();
        });

		// fix PNG for IE6
		//if ( $.browser.msie && $.browser.version == '6.0' )
			//DD_belatedPNG.fix('div, a, img, li');
		
		// remove any borders on last LI element
		$("ul").each(function(){
			 $(this).children("li:last").css({borderRight:"0",borderBottom:"0",background:"none",paddingRight:"0",marginRight:"0"});
		});

		// smooth animation of internal links
		$('a[href*=#]').click(function() {
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target	|| $('[name=' + this.hash.slice(1) +']');

				if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body').animate({scrollTop: targetOffset}, 1000);
					return false;
				}
			}
		});

		if ( $('#content ul.exhibitions').length ){
			$('#content ul.exhibitions li:nth-child(4n)').addClass('endOfLine');
		}

		$('a.triggerSpecialEvents').click(function(){
		   $('div#moreSpecialEvent').fadeIn(500);

			return false;
		});

		$('a#close').click(function(){
			$('div#moreSpecialEvent').fadeOut(250);

			return false;
		});

		$('#content ul.exhibitions li a').hover(
			function(){
				var description = $(this).children('div').children('p.description');
				$(this).children('div').stop().animate({ height:'80px' },250,function(){
					description.fadeIn(150);
				});
			},
			function(){
				var description = $(this).children('div').children('p.description');
				description.fadeOut(150,function(){
					$(this).parent('div').stop().animate({ height:'30px' },250);
				});
			}
		);

		$('#topExhibitions ul.exhibitions li a').hover(
			function(){
				$(this).children('div').stop().animate({ height:'84px' },250,function(){
					$(this).children('p.description').fadeIn(150);
				});
			},
			function(){
				$(this).children('div').children('p.description').fadeOut(150,function(){
					$(this).parent('div').stop().animate({ height:'36px' },150);
				});
			}
		);

		$('a#signup').click(function(){
			$('form#subscribe').slideDown('slow',function(){
				$('form#subscribe input#name').focus();
			});

			return false;
		});

		$('a#close').click(function(){
			$('form#subscribe').slideUp('fast');

			return false;
		});

		if ( $('#intro, #tourIntro, div#tourDescription div.desc').length ){
			$('#intro p:last-child, #tourIntro p:last-child, div#tourDescription div.desc p:last-child').css({ marginBottom:0 });
		}

        //news

        $('a#location_link').click(function(){
            var o = $(this);
            o.attr('href');
            var link = "/base/cms/map/filter/location/" + o.attr('href') + "/media/" + $('a#media_selected.selected').attr('href');
            $.get(link, null, function(data){
                $('div#map').html('<h1 class="pageTitle">' + data.media + '</h1><p>' + data.location +'</p>');
            });
            return false;
        });

        //end of news


		if ( $('#tour').length ){
			// http://www.mayzes.org/googlemaps.jquery.html
            
/*
			$('#tour').googleMaps({
				latitude: 	51.515833,
				longitude: -0.070456,
				markers:[{
					latitude: 51.515833,
					longitude: -0.070456,
					info:{
						layer:'#starter'
					}
				},{
					latitude: 51.517833,
					longitude: -0.074456,
					info:{
						layer:'#starter'
					}
				},{
					latitude: 51.522833,
					longitude: -0.069456,
					info:{
						layer:'#middle'
					}
				}],
                polyline: {
                    path: [
                        new google.maps.LatLng(51.515833, -0.070456),
                        new google.maps.LatLng(51.517833, -0.074456),
                        new google.maps.LatLng(51.522833, -0.069456)
                    ],
                    color: 			'#ff0000',
                    pixels: 		4
                },
				depth:   16
			});*/
		}

		if ( $("div#twitter").length ){
			$("div#twitter").tweet({
	//            username: "firstthursdays",
				query: "#firstthursdays",
				join_text: "auto",
				avatar_size: 0,
				count: 1,
				//template: "<label>{user}</label> {text}",
				template: "{text}",
				loading_text: "loading tweets...",
				refresh_interval: 180
			});
		}

		var slider = function(){
			$('#slider dl').css({display: 'block'});
			$('#slider').easyAccordion({
				autoStart: true,
				slideInterval: 4000,
				slideNum: false,
				animationSpeed:500
			})
		}

		if ( $('#slider').length ){
			$('#slider dl').css({display: 'none', height: '402px'});
			setTimeout(slider, 2000);
		}

	});
})(jQuery);
	
// Define indexOf for IE
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};
