var SoundPlayed = 0;
var SoundsActive = 0;
function playsound() {
  myid=document.getElementById("dummyspan");
  if (myid) {myid.innerHTML="<embed src='/notification.wav' hidden=true autostart=true loop=false>";}
  
}

function registerclick(id) {
$.get("/webservice.php", { act:"registerclick",id: id });
}

function registervote(id) {
$.get("/webservice.php", { act:"registervote",id: id },function(data){
alert(data);
});
}

function custom_push(r) {
var json = r;

//kindlalt peaks kontrollima kas sellise ID-ga itemit juba olemas ei ole
elem=document.getElementById("nw"+this.id+json.id);
//elem=$("#"+this.id).find("#nw"+this.id+json.id);
//if (elem1) { window.status='olemas';}
//window.status="--"+this.id;
if (elem) {
} else {
var html='<li id="nw'+this.id+json.id+'">';
html +='<strong>'+json.kell+'</strong>';
html +='<span><a onMouseDown="registerclick('+json.id+')" target=_new href="'+json.link+'">';
html +=json.title;
html +='</a> <span>('+json.kanal+')</span></span><div class="clear"></div></li>';

$('#'+this.id).prepend(html);
if (SoundsActive==1 & SoundPlayed==0) {
playsound();
SoundPlayed=1;
}
$('#nw'+this.id+json.id).show('highlight',{color: 'yellow'});

//kustutame viimase
$('#'+this.id).find('li:last').remove();
}
//alert(html);
}

function hidearticles(level) {
//if (getCookie('importance') !== level) 
setCookie("importance", level);
for (i=1;i<7;i++)
{
myelement=document.getElementById('filter'+i);
if (myelement) {
  if (i==level) myelement.className='act';
  if (i != level) myelement.className='';
}
if (level<i) changecss('.weightclass'+i,'display','none');
if (level>=i) changecss('.weightclass'+i,'display','');
}
}


function setfavorite(type,id) {
$.get("/webservice.php", { topic_id: id, favtype:type },
  function(data){
      //alert(data);
      if (data.trim()=="1") {
      document.getElementById('starimg').src='/img/star-on.gif';
      } else {
      document.getElementById('starimg').src='/img/star-off.gif';
      }
  });
}

function addnewblog(link) {
if (link.trim()=="" || link.trim()=="http://") {
document.getElementById("response").innerHTML='<font color="FF0000"><b>Palun sisestage blogi link</b>';
return false;
}
if ($('#companyblog').attr('checked') && $('#companyname').val()=="") {
alert('Kui tegemist on organisatsiooni blogiga, siis tuleb sisestada ka organisatsiooni nimi');
return false;
}

document.getElementById("response").innerHTML='<img src="/images/ajax-loader.gif"><b>Oodake palun, lisame!';

$.get("/webservice.php", { act: 'addnew', channellink:link,companyblog: $('#companyblog').attr('checked'),companyname:$('#companyname').val(),blogcategory:$('#category').val()},
  function(data){
document.getElementById("response").innerHTML=data;
document.getElementById("feedaddress").value='';

  });
}

function searchfeeds(myphr) {
if (myphr.trim()=="") {
document.getElementById("searchresults").innerHTML='<font color="FF0000"><b>Palun sisestage otsingus&otilde;na</b>';
return false;
}
document.getElementById("searchresults").innerHTML='<img src="/images/ajax-loader.gif"><b>Oodake palun, otsime!';

$.get("/webservice.php", { act: 'feedsearch', phr:myphr },
  function(data){
document.getElementById("searchresults").innerHTML=data;
return false;
});
}


function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	
//alert(getCookie('importance'));
function setCookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
expires = 30 * 1000 * 60 * 60 * 24;
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ";path=/" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
function changecss(theclass,element,value) {
var cssRules;
if (document.all) {
cssRules = 'rules';
}
else if (document.getElementById) {
cssRules = 'cssRules';
}
for (var S = 0; S < document.styleSheets.length; S++){
for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
if (document.styleSheets[S][cssRules][R].selectorText == theclass) {
document.styleSheets[S][cssRules][R].style[element] = value;
}
}
}
}





$(document).ready(




	function () {
		
//		$('div.groupWrapper').Sortable(
//			{
//				accept: 'groupItem',
//				helperclass: 'sortHelper',
//				activeclass : 	'sortableactive',
//				hoverclass : 	'sortablehover',
//				handle: 'div.itemHeader',
//				tolerance: 'pointer',
//				onChange : function(ser)
//				{
//				},
//				onStart : function()
//				{
//					$.iAutoscroller.start(this, document.getElementsByTagName('body'));
//				},
//				onStop : function()
//				{
//					$.iAutoscroller.stop();
//				}
//			}
//		);
        
    
    $(function() {
    $('a.closeEl').bind('click', toggleContent);
//$('#container-2 ul').tabs(1,{ click: function(clicked, show, hide) { alert(clicked)} });
});
    
    myelement=document.getElementById('filtermenu');
    if (myelement) {
      initialsliderValue=0;
      importancevar=getCookie('importance');
      if (importancevar>0) {
        initialsliderValue=importancevar;
        hidearticles(importancevar);
      }
    }
	}
);



var toggleContent = function(e)
{
	var targetContent = $('div.itemContent', this.parentNode.parentNode);
    
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		$(this).html('<span class="hidden">[-]</span>');
	} else {
		targetContent.slideUp(300);
		$(this).html('<span class="hidden">[+]</span>');
	}
	return false;
};
String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

function getlastid() {
return $('#newswire-container').find('li:first').attr('id');
}

function serialize(s)
{
	serial = $.SortSerialize(s);
	alert(serial.hash);
};


