$(function(){
	$("#q").focus(function() {
		if($(this).val() == 'sameklē')
			$(this).val('');
	});
	$("#q").blur(function() {
		if($(this).val() == '')
			$(this).val('sameklē');
	});
	$(".draugiem_say").click(
		function() {
			DraugiemSay($(this).attr("title"), $(this).attr("href"), "Open.lv");
			return false;
		}
	);
	
	$("#open_mail_acc").click( function() {
		if($(this).val() == "e-pasts")
			$(this).val("");
	});
	
	
	toggleOldCookieBoxes();
	$("#mednieki .box a.show, #sani .box a.show").bind("click", function(event) {  
		event.preventDefault();
		butt = $(this).parent().parent().parent().attr("id") == "sani" || $(this).parent().parent().parent().attr("id") == "twitter_div" ? 'rc' : 'lc';
		baseUrl = $("#base_url").val();
		if($(this).attr("title") == '')	{
			$(this).css('background', 'url(' + baseUrl + 'theme/img/btn_' + butt + '_show1.png) no-repeat');
			$(this).attr('title', 'clicked');
		}	else {
			$(this).css('background', 'url(' + baseUrl + 'theme/img/btn_' + butt + '_show0.png) no-repeat');
			$(this).attr('title', '');
		}
		cookieToggleBox();
		$("#" + this.id.substr(5)).slideToggle();    	
	});

	
	
});

function cookieToggleBox() {
	cookieData = '|';
	$("#mednieki .box a.show, #sani .box a.show").each(function() {
		if($(this).attr("title") == 'clicked')
			cookieData += $(this).attr("id").substr(5) + '|';
	});
	$.cookie("showhide", cookieData, { expires: 365 });
}

function toggleOldCookieBoxes() {
	cookieInfo = $.cookie("showhide");
	if(cookieInfo != null) {
		baseUrl = $("#base_url").val();
		var toggled = cookieInfo.split("|");
		for(i = 0; i < toggled.length; i++) {
			if(toggled[i] != '') {
				butt = $("#show-" + toggled[i]).parent().parent().parent().attr("id") == "sani" || $("#show-" + toggled[i]).parent().parent().parent().attr("id") == "twitter_div" ? 'rc' : 'lc';
				$("#show-" + toggled[i]).css('background', 'url(' + baseUrl + 'theme/img/btn_' + butt + '_show1.png) no-repeat');
				$("#show-" + toggled[i]).attr("title", 'clicked');
				$("#" + toggled[i]).slideToggle();
			}
		}
	}
}

function DraugiemSay( title, url, titlePrefix ) { 
  window.open( 
   'http://www.draugiem.lv/say/ext/add.php?title=' + encodeURIComponent( title ) + 
   '&link=' + encodeURIComponent( url ) + 
   ( titlePrefix ? '&titlePrefix=' + encodeURIComponent( titlePrefix ) : '' ), 
   '', 
   'location=1,status=1,scrollbars=0,resizable=0,width=530,height=400' 
  ); 
  return false; 
}
