$(document).ready(function(){
	initArchive();
	initSearch();
	initPrint();
	initRSS();
	initResize();
	doResize();
});

var firsttime = true;

function initArchive(){
	var $item = $('#block-views-archive-block .view-content ul li');
	$item.each(function(){
		var $text = $(this).text();
		var $link = $(this).find('a');
		$link.text($text);
		$(this).html($link);
	});
};

function initSearch(){
	var searchHTML = '<div id="search-popup"><form class="search-form" id="search-form-popup" method="post" accept-charset="UTF-8" action="'+Drupal.settings.basePath+'search/node">'
					+'\n<div class="search-form-popup">'
					+'\n<input type="text" class="form-text" value="" size="40" id="edit-keys" name="keys" maxlength="255" value="Find it here..." />'
					+'\n<input type="submit" class="form-submit" value="Search" id="edit-submit" name="op"/>'
					+'\n</div>'
					+'\n</form></div>';
	$formPopup = $(searchHTML);
	$('div#page .header:first').css('position','relative');
	$formPopup.appendTo('div#page .header:first');
	$formPopup.css({'position':'absolute','right':0,'top':'50px'});
	
	$formPopup.hide();
	
	$('.header ul.primary-links li.menu-416 a:first').css('outline','none');
	$('.header ul.primary-links li.menu-416 a:first').click(function(e){
		e.preventDefault();
		$formPopup.toggle();
	});
	$('#edit-keys')[0].defaultValue = 'Find it here...';
	$('#edit-keys')[0].value = 'Find it here...';
	$('#edit-keys').click(function(e){
		if (this.value == this.defaultValue) this.value = '';
	});
	$('#edit-keys').blur(function(e){
		if (this.value == '') this.value = this.defaultValue;
	});
	
/*	
	var $searchPhrase = $('li.hidden-search-phrase:first').text();
	$searchPhrase = $searchPhrase.toLowerCase(); 
	$('strong.search-phrase-span').text('"'+$searchPhrase+'"');
*/	
	$('div#alpha .clear-block #search-form').css('display','none');
	$text = $('div#tabs-wrapper h2.with-tabs:first ').text();
	if ($text == 'Search') $('div#tabs-wrapper').css('display','none');
};

function initPrint(){
	$('a.print-page').click(function(e){
		e.preventDefault();
		window.print();
	});
};

function initRSS(){
	$rss = $('link[type=application/rss+xml]');
	var title;
	var link;
	$rss.each(function(){
		if ($(this).attr('href').indexOf('/crss/') != -1){
			link = $(this).attr('href');
		}
	});
	if (typeof(link) != 'undefined' ){
		$link = $('<div class="rss-block"><span><a href="'+link+'">Comment feed</a></span> | <a href="#">Back to Top</a></div>');
		$link.insertAfter('div#comments h2:first');
	}
	$title = $('div.box h2:first');
	if ($title.text() == 'Post new comment'){
		$title.text('Share your thoughts...');
	}
}

function initResize(){
	$(window).resize(function(){
		doResize();
	});
}
function doResize(){
	if ( $(window).width() <1000 ){
		$("#page").css("width","745px");
		$("#footer").css({
			width:"745px",
			marginLeft:"-372px"
		});
		if ( $("#page").hasClass("home") ){
			$("#sidebar-right").css({
				width:"495px",
				float:"right",
				marginTop:"-120px"
			});
			$("#who").css({
				position:"relative",
				top:"125px",
				left:"-500px"
			});
			$("#content-bottom").css({
				overflow:"visible"
			});
			$("#block-views-thingofbeauty-block_1").css({
				float:"left"
			});
			$("#block-views-shortnews-block_1").css({
				float:"left",
				display:"inline",
				width:"235px",
				marginTop:"0px",
				marginLeft:"5px"
			});
			$(".short-news-inner").css({
				height:"215px"
			});
			
		}else{
			$("#sidebar-right").hide();
		}
	}else{
		$("#page").css("width","995px");
		$("#page").css("height","auto");
		$("#footer").css({
			width:"995px",
			marginLeft:"-497px"
		});
		if ( $("#page").hasClass("home") ){
			$("#sidebar-right").css({
				width:"245px",
				position:"static",
				marginTop:"0px"
			});
			$("#who").css({"position":"static"});
			$("#block-views-thingofbeauty-block_1").css({
				float:"none"
			});
			$("#content-bottom").css({
				overflow:"hidden"
			});
			$("#block-views-shortnews-block_1").css({
				float:"none",
				width:"auto",
				display:"block",
				marginLeft:"0",
				marginTop:"5px"
			});
			$(".short-news-inner").css({
				height:"590px"
			});
		}else{
			$("#sidebar-right").show();
		}
	}
}
