/* Ieteikt draugiem */

function DraugiemSay( title, url, titlePrefix ){
    var winname = 'window' + Math.round(Math.random() * 10000 + 1);
    var win = window.open(
    'http://www.draugiem.lv/say/ext/add.php?title=' + encodeURIComponent( title ) +
    '&link=' + encodeURIComponent( url ) +
    ( titlePrefix ? '&titlePrefix=' + encodeURIComponent( titlePrefix ) : '' ),
    winname,
    'location=1,status=1,scrollbars=0,resizable=0,width=530,height=400'
    );

    if (win.opener == null) win.opener = self;
    return false;
}

function tb_show(a, url, b){
	new Ajax.Request(url, {
        method:'get',
        onSuccess: function(t)
        {
            modal.content(t.responseText);
        }
    });
}

/* Ieteikt draugiem */

function checkMail(mailInput){
	
	email=mailInput.value;
	
	email=email.replace(/^\s+|\s+$/g, '');
	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

	if (!filter.test(email)){
		jQuery("#reg_error").show();
		jQuery("#next").hide();
	}
	else{
		jQuery("#reg_error").hide();
		jQuery("#next").show();
	}
}

/* Drop down menu  : start */





var t;

		

function showTimer(id){

	t=setTimeout("showMenu("+id+")",300);

}



function showMenu(id){

	document.getElementById('menu_'+id).style.display='block';

}



function hideMenu(id){

	document.getElementById('menu_'+id).style.display='none';

}



function clearTimer(){

	clearTimeout(t);

}



function hideTimer(id){

	t=setTimeout("hideMenu("+id+")",100);

}



/* Drop down menu : end */

function showHideMenu(id,action){
		if(action=='hide'){
			jQuery("#menu_"+id).hide();
		}
		else if(action=='show'){
			jQuery("#menu_"+id).show();	
			//tb_init('a.thickbox');
		}
}

function switchTab(tab,link){
	for(i=1;document.getElementById('tab'+i);i++){
		if(tab==i){
			document.getElementById('tab'+i).className='sel';
			
			jQuery.post(link,{},function(data){
				
				//alert(data);
				jQuery("#tabContent").html(data);
			
			});
		}
		else{
			document.getElementById('tab'+i).className='';
		}
	}
}

function hideMsgBox(id){
	document.getElementById(id);
	setTimeout("fadeOut('"+id+"')",2000)
}

function fadeOut(id){
	jQuery("#"+id).fadeOut("slow");
}

// JavaScript Document

function forums_check_location(val){
	if(val == '0'){
		jQuery('#noplace').attr('checked','true');
	}
	else{
		jQuery('#noplace').attr('checked','');
	}
}

function select_gal_pic(id){
	for(i=1;document.getElementById('thumb'+i);i++){
		if(i==id){
			document.getElementById('thumb'+i).className='selected';
		}
		else{
			document.getElementById('thumb'+i).className='pic';
		}
	}
}

function sendMsg(){
	if(jQuery("#message").val()!=''){
		jQuery.post("/tools/msg.php",{fromID:jQuery("#fromID").val(),toID:jQuery("#toID").val(),heading:jQuery("#heading").val(),msg:jQuery("#message").val()},function(data){
		
			data=data.split("|");
			
			if(data[0]=='true'){
				modal.close();
			}
			else{
				alert(data[1]);
			}
		
		});
	}
	else{
		document.getElementById('msg').style.display='block';
		hideMsgBox('msg');
	}
	return false;
}

function bind_datepicker(){
    jQuery(".datepicker").datepicker({
        dateFormat: "dd.mm.yy"
    });
}

function bind_thickbox(){
	//tb_init();
}

function bind_report(){
	jQuery(".report_action").click(function(event){
	var id = jQuery(this).attr("id");
	var send = id.split("-");
	xajax_xreport(send[1], send[2]);
	/*jQuery('#video_embeded').css('visibility','hidden');*/
	return false;
    });
}

function bind_anonim(){
	jQuery("#comment_anonim").click(function(event){
	var state = jQuery(this).attr("checked");
	if(state){
	    jQuery("#comment_anonim_options").show();
	    jQuery("#comment_profile_options").hide();
	}
	else{
	    jQuery("#comment_anonim_options").hide();
	    jQuery("#comment_profile_options").show();
	}
    });
}

jQuery(document).ready(function(){
	
	jQuery("#msg_list").autobox({
        ajax:"/tools/contact_list.php",
        match: function(typed) { return this.text.match(new RegExp(typed, "i")); },
        insertText: function(obj) { return obj.text }
      });
	jQuery('.autobox-hldr').click(function(event){
		jQuery('.autobox-input > input').focus();
	});
	
    /*
     jQuery("#events_tmp_more_opts").click(function(event){
	    jQuery("#events_tmp_more_opts_box").slideDown();
    });
    
    jQuery("#events_tmp_more_opts").dblclick(function(event){
	    jQuery("#events_tmp_more_opts_box").slideUp();
    });
    */
	bind_datepicker();
	bind_anonim();
	bind_report();
    
    jQuery(".vote_action").click(function(event){
	var id = jQuery(this).attr("id");
	var send = id.split("-");
	xajax_xrate(send[1], send[2], send[3]);
	return false;
    });    
    
    jQuery(".attach_action").click(function(event){
	var id = jQuery(this).attr("id");
	var send = id.split("-");
	xajax_xattach(send[1], send[2]);
	return false;
    });
    
    jQuery(".vote_result").click(function(event){
	jQuery(".vote_result").hide();
	jQuery(".vote_result_expanded").show();
    });
    
    jQuery(".vote_result_expanded").click(function(event){
	jQuery(".vote_result").show();
	jQuery(".vote_result_expanded").hide();
    });
    
   /* jQuery(".pic").click(function(event){
	jQuery(".pic").removeClass("selected");
	jQuery(this).addClass("selected");
    });*/
    
    jQuery("#searchtext").focus(function(event){
	var val = jQuery(this).attr('value');
	if(val == 'Ieraksti vārdu vai frāzi!'){
		jQuery(this).attr('value','');
	}
    });

    jQuery("#searchtext").blur(function(event){
	var val = jQuery(this).attr('value');
	if(val == ''){
		jQuery(this).attr('value','Ieraksti vārdu vai frāzi!');
	}
    });
    
    jQuery("#event_calendar_menu").click(function(event){
	jQuery(this).addClass("sel");
	jQuery("#events_calendar_holder").slideDown();
	return false;
    });
    
	
});


function switchGalleryImg(img){
	jQuery('#gallery_main_img').fadeTo("fast", 0);
	setTimeout("jQuery('#gallery_main_img').attr('src','"+img+"');",200);
	var img = jQuery('#gallery_main_img')[0];
	img.onload = function (){
		jQuery('#gallery_main_img').fadeTo('fast', 1);
	}
}


function show_comment(id){
	if(id == 'form'){
		jQuery.scrollTo( '#com_frm', 600);
	}
	else{
		jQuery.scrollTo( '#comment-item-main-'+id, 600);
	}
}

function pop_pic(url,name,w,h){
	window.open(url, name,'toolbar=no,status=no,scrollbars=no,menubar=no,width='+w+',height='+h+',left=50,top=50');
}

function check_text(id,text){
	if(document.getElementById(id).value == text){
		document.getElementById(id).value = '';
	}
}

function checkbox_all(self,elem){
	var status = document.getElementById(self).checked;
	var elems = document.getElementsByName(elem);
	for (var i=0, count=elems.length; i<count; ++i) {
	  elems[i].checked = status;
	}
}

function ch_class(id,cl){
	document.getElementById(id).className = cl;
}

function ch_style(id,prop,sty){
	document.getElementById(id).style.prop = sty;
}

function showhide(id){
    if(document.getElementById(id).style.display == 'none'){
	    document.getElementById(id).style.display = '';
    }
    else{
	    document.getElementById(id).style.display = 'none';
    }
}

/* PASTS */
function pasts_open_ab(){
	if(document.getElementById('pasts_ab').style.display == 'none'){
		Element.show('pasts_ab');
		ch_class('pasts_op_ab','pasts_open_ab_sel');
	}
	else{
		Element.hide('pasts_ab');
		ch_class('pasts_op_ab','pasts_open_ab');
	}
}

function pasts_demo_open(){
	if(document.getElementById('demo_atverums').style.display == 'none'){
		Element.show('demo_atverums'); 
		ch_class('demo_atveramais','pasts_row pasts_row_sel');
	}
	else{
		Element.hide('demo_atverums'); 
		ch_class('demo_atveramais','pasts_row');
	}
}
/* ZINAS */
function pop_art_print(url){
	window.open(url, 'Izdrukat_rakstu','location=no,toolbar=no,status=no,scrollbars=yes,menubar=no,width=600,height=600,left=50,top=50');
}

function pop_grabber(url){
	window.open(url, 'Kontaktu importēšana','location=no,toolbar=no,status=no,scrollbars=yes,menubar=no,width=600,height=600,left=50,top=50');
}

function pop_video(url){
	window.open(url, 'video','toolbar=no,status=no,scrollbars=no,menubar=no,width=300,height=278,left=200,top=200');
}

/* KOMENTEŠANA */
function comAnonim(){
	if(document.getElementById('com_name').style.display == 'none'){
		document.getElementById('com_name').style.display = 'inline';
		document.getElementById('com_login_name').style.display = 'none';
	}
	else{
		document.getElementById('com_name').style.display = 'none';
		document.getElementById('com_login_name').style.display = 'inline';
	}
}

/* KONTAKTI */
function kontakti_age_sel(f,t){
	if(f != '0'){
		document.getElementById('age_sel_from').value = f;
	}
	if(t != '0'){
		document.getElementById('age_sel_till').value = t;
	}
	Element.hide('age_sel');
}

/* FORMAS */
function removeAllOptions(selectId){
    for (var element in document.getElementById(selectId))
    {
        document.getElementById(selectId).remove(element);
    }// end for
}

function addOption(selectId, txt, val){
    var objOption = new Option(txt, val);
    document.getElementById(selectId).options.add(objOption);
}

/* SWITCHERIM */
var antihide = 0;
var passed_time = 0;
function hideint(id){
	if(antihide == 0){
		passed_time++;
		if(passed_time >= 20){
			hide(id);
		}
		else{
			window.setTimeout( function() { hideint(id) }, 100);
		}	
	}
	else{
		window.setTimeout( function() { hideint(id) }, 100);
	}
}

function setpas(){
	passed_time = 0;	
}

function stophide(v){
	antihide = v;
}

/* SLUDINAJUMI */

var popstop = 0;
function set_stop(v){
	if(v == 0){
		window.setTimeout( function() { popstop = 0; }, 100);
	}
	else{
		popstop = 1;
	}
}

function lounch_slud(type,id){
	if(popstop == 0){
		if(type == 'open'){
			xajax_open_slud(id);
		}
		else{
			xajax_close_slud(id);	
		}
	}
}

function pop_slud(url,title){
	window.open(url, title,'resizable=yes,location=no,toolbar=no,status=no,scrollbars=yes,menubar=no,width=758,height=700,left=50,top=50');
}

function pop_radio(url){
	window.open(url, 'radio','location=yes,toolbar=no,status=no,scrollbars=no,menubar=no,width=425,height=367,left=200,top=200');
}


/* PASTS */

function lounch_pasts(type,id,act){
	if(popstop == 0){
		if(type == 'open'){
			xajax_show_msg(id,act);
		}
		else{
			xajax_close_msg(id,act);	
		}
	}
}

function pasts_clear(){
	document.getElementById('con_ids').value = '';
	document.getElementById('con_tmp_list').value = '';
	var elems = document.getElementsByName('con[]');
	for (var i=0, count=elems.length; i<count; ++i) {
	  elems[i].checked = false;
	}
	document.getElementById('pasts_del_address').style.display = 'none';
}

function check_pasts_but(){
	if(document.getElementById('con_tmp_list').value == ''){
		document.getElementById('pasts_del_address').style.display = 'none';
	}
	else{
		document.getElementById('pasts_del_address').style.display = 'block';
	}
}

function viewed(id, path)
{
    new Ajax.Request('/functions/feedAjax.php',
        {
            method:'post',
            parameters:{
                action: 'viewed',
                id: id
            },
            onSuccess:function(){
                window.location.href = path;
            }
        }
    );
}
