$(document).ready(function() {
    if($.browser.msie && $.browser.version < 7){
        pngfix();
    }
	$('.date-jq').datePicker().bind(
			'click',
			function()
			{
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		).bind(
			'dateSelected',
			function(e, selectedDate, $td)
			{
				$('#date-pick-jq-span').text(selectedDate.asString());
				$('#date-pick-jq').val(selectedDate.asString());
				//console.log('You selected ' + selectedDate);
			}
		);;
    $(".delme").focus(function(){
        if ($(this).is('input')) {
            var orival = $(this).val();
            $(this).val("");
        }else{
            var orival = $(this).html();
            $(this).html("");
        }
        $(this).blur(function(){
            if($(this).is('input')){
                if ($(this).val() != "" && $(this).val() != orival) {
                    $(this).removeClass('delme').unbind('blur').unbind('focus');
                }else{
                    $(this).val(orival);
                }
            }else{
                if ($(this).html() != "" && $(this).html() != orival) {
                    $(this).removeClass('delme').unbind('blur').unbind('focus');
                }else if ($.browser.safari && $(this).val() != "" && $(this).val() != orival) {
                    $(this).removeClass('delme').unbind('blur').unbind('focus');
                }else{
                    $(this).html(orival);
                }
            }
        });
    });
    //$(".select .livsec").hide();
    $(".select .open-close, .select span").click(function(){
        $(this).siblings('.livsec').toggle().find('li').not('.noselect').click(function(){
            var select = $(this).parents('.select');
            //$('input', select).val($(this).html());
            $('input', select).val($(this).attr("id"));
            $('span', select).html($(this).html());
            $('.livsec', select).hide();
        });
        return false;
    });
    //Reveals & Hides ETC
    $(".extras").hide();
    $(".up-down.slow").toggle(function(){
        $(".extras").slideDown("slow");
        $(".up-down.slow").addClass("opened");
    }, function hideEtc(){
        $(".extras").slideUp("slow");
        $(".up-down.slow").removeClass("opened");
    });
	
	$('.content').height( (parseInt($('#central-container').height()) - 125) + 'px');

    //Spondor
    $(".sponsor-list").hide();
    $(".up-down.sp").toggle(function(){
        $(".sponsor-list").slideDown("slow");
        $(".up-down.sp").addClass("opened");
    }, function hideEtc(){
        $(".sponsor-list").slideUp("slow");
        $(".up-down.sp").removeClass("opened");
    });

    //$(".opener .sec").hide();
    $(".opener > a").click(function(){
        $(this).siblings(".sec").slideToggle("slow", function(){
			$('.content').height($('#central-container').height());
		});
        $(".opener > a").not(this).siblings(".sec").slideUp("fast");
	//$('.content').height($('#central-container').height());
        return false;
    });
    $("ul.sec li:last").addClass("last");

	var yourURL = "flickr.com";

	outLinks();

    //Open Close

    $(".places").addClass("closed");
	$(".places").first().removeClass("closed").addClass("open");
    $(".up-down,.text-name").click(function(){
        var place = $(this).parents(".places").find(".place");
        var places = $(this).parents(".places");
        if(places.hasClass("closed")){
            place.slideDown(300, function(){
                if(places.hasClass("closed")){
                    places.removeClass("closed").addClass("open");
                }
                for(var i in window) {
                	if(i.indexOf("map") >= 0)
                	{
                		google.maps.event.trigger(window[i], 'resize');
						//window[i].setCenter(window[i].getCenter());
						if(!window[i].panned)
						{
							window[i].panBy(-200,-200);
							window[i].panned = true;
						}
						//window[i].panTo( map.getCenter() );
                	}
                }
		/*if(google.maps.event.trigger && map)
		{
			//var divm = map.getDiv();
			//map.setZoom( map.getZoom() );
			//map.panTo( map.getCenter() );
			//google.maps.event.trigger(map, 'resize');
			//map.panBy(-200,-200);
		}*/
			$('.content').height('auto');
            });
        }else{
            place.slideUp(300, function(){
                if(places.hasClass("open")){
                    places.removeClass("open").addClass("closed");
					$('.content').height($('#central-container').height());
                }
            });
        }
        $(".places").not($(this)).each(function(){
        	closeOther(this);
        });
        
        return false;
    });
});

function outLinks() {
  var outLink;
  if (document.getElementsByTagName('a')) {
    for (var i = 0; (outLink = document.getElementsByTagName('a')[i]); i++) {
      if (outLink.href.indexOf("flickr.com") > 0) {
        outLink.setAttribute('target', '_blank');
      }
    }
  }
}

function closeOther(el)
{
	if($(el).hasClass("open")){
	    $(el).find(".place").slideUp(600, function(){
		if($(el).hasClass("open")){
		    $(el).removeClass("open").addClass("closed");
		}
	    });
	}
}

function oldselect()
{
    $(".select .open-close, .select span").click(function(){
        $(this).siblings('.livsec').toggle().find('li').click(function(){
            var select = $(this).parents('.select');
            $('input', select).val($(this).html());
            $('span', select).html($(this).html());
            $('.livsec', select).hide();
        });
        return false;
    });
}
