$.fn.search = function() {
	return this.focus(function() { if( this.value == this.defaultValue ) { this.value = ""; } }).blur(function() { if( !this.value.length ) { this.value = this.defaultValue; } });
};

$(document).ready(function() {
    $("#search").search();
    $(".searchbutton").hover(
		function() { $(this).attr("src","/img/common/search_button_over.gif"); },
		function() { $(this).attr("src","/img/common/search_button.gif"); }
	);

    $(".box_feature_design .box_more, .box_event .box_more").after("<div class='box_raquo'>&raquo;</div>");   
    $(".box_feature_design .box_more, .box_event .box_more").hover(
        function() {
            $(this).parent().find('.box_raquo').css('backgroundPosition', '0 -20px');
        },
        function() {
            $(this).parent().find('.box_raquo').css('backgroundPosition', '0 0');
        }
    );

   $(".box_feature_footer .box_more a").hover(
        function() {
            $(this).css('textDecoration', 'underline');
            $(this).parent().find('.box_raquo, .box_raquo_more, .box_raquo_less').css('backgroundPosition', '0 -20px');
        },
        function() {
            $(this).css('textDecoration', 'none');
            $(this).parent().find('.box_raquo, .box_raquo_more, .box_raquo_less').css('backgroundPosition', '0 0');
        }
   );

    $(".box_news .box_more").after("<div class='box_raquo_more'>&raquo;</div>");

    $(".box_feature_footer_swdf .box_raquo_more, .box_feature_footer .box_raquo_more, .box_feature_footer .box_raquo_less").hover(
        function() {
            $(this).parent().find('.box_more').css('textDecoration', 'underline');
            $(this).css('backgroundPosition', '0 -20px');
        },
        function() {
            $(this).parent().find('.box_more').css('textDecoration', 'none');
            $(this).css('backgroundPosition', '0 0');
        }
    );
    $(".box_feature_footer .box_more, .box_feature_footer_swdf .box_more").hover(
        function() {
            $(this).parent().find('.box_raquo_more').css('backgroundPosition', '0 -20px');
            $(this).parent().find('.box_raquo_less').css('backgroundPosition', '0 -20px');
        },
        function() {
            $(this).parent().find('.box_raquo_more').css('backgroundPosition', '0 0');
            $(this).parent().find('.box_raquo_less').css('backgroundPosition', '0 0');
        }
    );

    closeAnyOpenNews = function() {
        $('.box_news_open').find('.box_news_content_full').hide();
        $('.box_news_open').find('.box_news_content_precis').show();
        $('.box_news_open').parent().addClass('col1').removeClass('col2');
        $('.box_news_open').find('a').html("Read more");
        $('.box_news_open').find('.box_raquo_more').addClass('box_raquo_less').removeClass('box_raquo_more');
        $('.box_news_open').find('.box_news_share').hide();
    }

    $('.box_news .box_feature_footer').click(function(event) {
		event.preventDefault();
        this_state = $(this).find('a').html();
        if (this_state == "Read more") {
            closeAnyOpenNews();
            $(this).find('a').html("Read less");
            $(this).find('.box_raquo_more').addClass('box_raquo_less').removeClass('box_raquo_more');
            $(this).parent().find('.box_news_content_precis').hide();
            $(this).parent().find('.box_news_content_full').show();
            $(this).parent().parent().parent().addClass('col2').removeClass('col1');
            $(this).parent().parent().addClass('box_news_open').removeClass('box_news_closed');
            $(this).find('.box_news_share').show();
            $('.wrap').masonry();
        } else {
            $(this).find('a').html("Read more");
            $(this).find('.box_raquo_less').addClass('box_raquo_more').removeClass('box_raquo_less');
            $(this).parent().find('.box_news_content_precis').show();
            $(this).parent().find('.box_news_content_full').hide();
            $(this).parent().parent().parent().addClass('col1').removeClass('col2');
            $(this).parent().parent().addClass('box_news_closed').removeClass('box_news_open');
            $(this).find('.box_news_share').hide();
            $('.wrap').masonry();
        }
    });
	
	$('.box_news .box_feature_footer_swdf .box_raquo_more').click(function(event) {		
		//alert($(this).parent().find('.box_more a').attr('href'));
		window.open($(this).parent().find('.box_more a').attr('href'));
        return false;
	});

	$("#FirstName").search();
	$("#Surname").search();
	$("#JobTitle").search();
	$("#qqydy-qqydy").search();
	$("#Organisation").search();

	$("a.launch_video").fancybox({
		'overlayColor'		:	'#000',
		'overlayOpacity'	:	0.75
	});
	
	
	$('.past_event_video_slideshow_item').click(function(event) {
		event.preventDefault();
		$(this).parent().find('a').removeClass('selected');
		$(this).addClass('selected');
		var video_url = $(this).attr("href");
		var substr = video_url.split('/')
		$(this).parent().parent().find('.event_video_container').html('<iframe src="http://player.vimeo.com/video/' + substr[3] + '?title=0&amp;byline=0&amp;portrait=0&amp;color=ff601b" width="660" height="371" frameborder="0"></iframe>');
	});
	$('.past_event_image_slideshow_item').click(function(event) {
		event.preventDefault();
		$(this).parent().find('a').removeClass('selected');
		$(this).addClass('selected');
		image_url = $(this).attr("href");
		$(this).parent().parent().find('.event_images_container img').attr('src', image_url);		
	});
		
   if ($('.box_calendar_scroll').length != 0) {
		
		$(".box_event_detail_footer p.more_less a").click(function(event) {
			event.preventDefault();
			if ($(this).hasClass('less_more')) {
				$(this).removeClass('less_more');
				$(this).html('More');
				$(this).attr('href','#more');
				$(this).parent().parent().parent().find(".event_hide").hide();			
			} else {
				$(this).parent().parent().parent().find(".event_hide").show();
				$(this).addClass('less_more');
				
				if ($(this).parent().parent().parent().find(".google_map").length != 0) {				
					gmap = $(this).parent().parent().parent().find("#event_id");
					gmap_id = gmap.html();
					eval("initialize_" + gmap_id + "();");					
				}
				$(this).html('Less');
				$(this).attr('href','#less');
			}
			
		});
		
		
		var cal_width = $('.box_calendar_wrapper').width();
		
		function resizer() {
			this_box_number = $("#box_number").html();           	          	
			calendarWidth = $(".box_calendar_wrapper").width();
			contentWidth = (this_box_number * 101) - (7 + calendarWidth);
			dragWidth = $("#scroll_track").width() - 45;
		}
		
		
		function updateHandle() {
			currentPosition = $('.box_calendar').position().left;
			draggerPos = (dragWidth * currentPosition) / contentWidth;
			$("#scroll_handle").css('left', -draggerPos);
		}

		resizer();
	
		$('#scroll_track').click(function(event) {
			var x = event.pageX - $(this).offset().left;				
			if (x > dragWidth) { x = dragWidth; }
			var percent = x/dragWidth;
			var newPos = -percent * contentWidth;
			$(".box_calendar").animate({left:newPos},{duration:0,queue:false});
			updateHandle();
		  });
		

        $('#cal_left').click(function(event) {
           	event.preventDefault();
			currentPosition = $('.box_calendar').position().left;
			newPosition = currentPosition + (7 * 101);
			if ( newPosition > 0 ) {
				$('.box_calendar').css('left', 0);
			} else {
				$('.box_calendar').css('left', newPosition);
			}
			updateHandle();
        });
        $('#cal_right').click(function(event) {
          	event.preventDefault();
			currentPosition = $('.box_calendar').position().left;
			newPosition = currentPosition - (7 * 101);
			if ( newPosition < -contentWidth ) {
				$('.box_calendar').css('left', -contentWidth);
			} else {
				$('.box_calendar').css('left', newPosition);
			}
          	updateHandle();
        });

        $("#scroll_handle").draggable({
            axis: 'x',
            containment: 'parent',
            start: function(event, ui) {
                resizer();
            },
            drag: function(event, ui) {
	             var thisPosition = $(this).position().left - 273;
                var percent = thisPosition/dragWidth;
                var newPos = -percent * contentWidth;
                $(".box_calendar").animate({left:newPos},{duration:0,queue:false});
            }
         });
         
    }

});
