var tog = true; 

function checkall() {
//	if (jQuery('.commentchk:first').is(':checked'))
//		jQuery('.commentchk').attr('checked', false);
//	else 
//		jQuery('.commentchk').attr('checked', true);
	jQuery('.commentchk').attr('checked', tog);
//	alert(tog);
	tog = !tog;
}

function testdel() {
	jQuery('.commentchk:checked').parents('.cam_block').remove();
}

function deletecomments() {
	var prms = jQuery('input[name^=deletecomment]:checked, .comments input[name=img_id]');
	jQuery.ajax({
		url:  '/engine/ajax/delcomments.php',
		type: "POST",
		dataType: 'json',
		data: prms,
		timeout: 15000,
		success: function(data) {
			if (data.success=="yes") jQuery('.commentchk:checked').parents('.cam_block').remove();
		}	
	});
}

var popupWindow = null;
function centeredPopup(url,winName,w,h,scroll) {
	LeftPosition = 0;
	TopPosition = 0;
	settings ='height='+screen.height+',width='+screen.width+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
	popupWindow = window.open(url,winName,settings);
}

var favorited=new Array();
var rating=new Array();
var comments=new Array();
var plus_num=new Array();
var minus_num=new Array();
var tools=new Array();
var vote=new Array();
var favorites=new Array();
var imagepath=new Array();
var basename=new Array();
var ext=new Array();
var index=new Array();
var aurl=new Array();

var nwidth=new Array();
var nheight=new Array();
var owidth=new Array();
var oheight=new Array();

function getthumb(postid,tofset)
{
	jQuery.ajax({
			url: "http://" + window.location.hostname +"/engine/ajax/getthumb.php",
			dataType: 'json',
			data: {
				postid: postid
			},
			success: function(resp) {
				var imgsrc=resp.src;
				var imgHeight=resp.height;
				var imgWidth=resp.width;
				if(imgHeight>imgWidth) {
					var imgSize="height:250px;";
					var imgTop=125;
					var imgLeft=230;
				} else {
					var imgSize="width:250px;";
					var imgTop=94;
					var imgLeft=260;
				}
				var offset = $(tofset).offset();
				$(tofset).css('text-decoration','underline');
				$('.commimg').css({'left':offset.left-imgLeft,'top':offset.top-imgTop}).html('<img src="'+imgsrc+'" style="'+imgSize+'"/>').fadeIn(400);
			}
		});
}

function getpicturedata(postid,el){
	var imgposid = jQuery(el).find("a[name^=pic]").html();
	if (tools[imgposid]==undefined || tools[imgposid]=="") {
		var img = jQuery(el).find("img").attr("src");
		var sitename = jQuery(el).find("img").attr("rel");
		var isolate= jQuery("input[id=img_id_isolate]").val();
		if (isolate>0) isolate=1; else isolate=0;
		jQuery.ajax({
			url: "http://" + window.location.hostname +"/engine/ajax/getpicinfo.php",
			dataType: 'json',
			data: {
				img: img,
				postid: postid,
				imgposid:imgposid,
				sitename:sitename,
				isolate:isolate
			},
			success: function(resp) {
				    tools[imgposid]=resp.tools;
				favorited[imgposid]=resp.favorited;
					 vote[imgposid]=resp.vote;
				 comments[imgposid]=resp.comments;
				favorites[imgposid]=resp.favorites;
				imagepath[imgposid]=resp.imagepath;
				 basename[imgposid]=resp.basename;
  			  	      ext[imgposid]=resp.ext;
				    index[imgposid]=resp.index;
				     aurl[imgposid]=resp.aurl;
				   nwidth[imgposid]=resp.nwidth;
				  nheight[imgposid]=resp.nheight;
				   owidth[imgposid]=resp.owidth;
				  oheight[imgposid]=resp.oheight;

				jQuery(el).find(".tools").html(tools[imgposid]);
				//jQuery(el).find(".picvote").html(vote[imgposid]);
				//jQuery(el).find(".piccomments").html(comments[imgposid]);
				//jQuery(el).find(".picfavorites").html(favorites[imgposid]);
				
				jQuery('.tools span').tipsy({gravity:'n', live:true});
				jQuery('.tools span').click(function () { jQuery('.tipsy').remove(); });

			}
		});
	}
}


function getpicturedataex(postid){
	imgposid=1;
	
	if (tools[imgposid]==undefined || tools[imgposid]=="") {
		var img = jQuery("#img").attr("rel");
		//alert(img);
		jQuery.ajax({
			url: "http://" + window.location.hostname +"/engine/ajax/getpicinfo.php",
			dataType: 'json',
			data: {
				img: img,
				postid: postid,
				imgposid:imgposid,
				nofull:1
			},
			success: function(resp) {
				    tools[imgposid]=resp.tools;
				favorited[imgposid]=resp.favorited;
					 vote[imgposid]=resp.vote;
				 comments[imgposid]=resp.comments;
				favorites[imgposid]=resp.favorites;
				imagepath[imgposid]=resp.imagepath;
				 basename[imgposid]=resp.basename;
  			  	      ext[imgposid]=resp.ext;
				    index[imgposid]=resp.index;
				     aurl[imgposid]=resp.aurl;
				   nwidth[imgposid]=resp.nwidth;
				  nheight[imgposid]=resp.nheight;
				   owidth[imgposid]=resp.owidth;
				  oheight[imgposid]=resp.oheight;

				jQuery('.toolsx').html(tools[imgposid]);
				/*jQuery(el).find(".picvote").html(vote[imgposid]);
				jQuery(el).find(".piccomments").html(comments[imgposid]);
				jQuery(el).find(".picfavorites").html(favorites[imgposid]);*/
				
				jQuery('.tools span').tipsy({gravity:'n', live:true});
				jQuery('.tools span').click(function () { jQuery('.tipsy').remove(); });
			}
		});
	}
}

function change_image_size(el) {
	imgposid=jQuery(el).parents(".imgbox").find("a[name^=pic]").html().substr(1);
	if (imgposid==undefined || imgposid=="") imgposid=1; 
	imgsize=el.value;
	jQuery(el).parents(".emb_code").find(".large").html("Full size");
	jQuery(el).parents(".emb_code").find(".thumb").html("Thumbnail");
	jQuery(el).parents(".emb_code").find(".normal").html("Normal");
	switch(imgsize) {
	case "1":
		image_url="http://" + SITENAME + imagepath[imgposid] + "200/" + basename[imgposid] + "_200_" + index[imgposid] + "." + ext[imgposid] ;
		jQuery(el).parents(".emb_code").find(".thumb").html("Thumbnail <small>(200x200)</small>");	
	break;
	case "2":
		image_url="http://" + SITENAME + imagepath[imgposid] + "640/" + basename[imgposid] + "_640_" + index[imgposid] + "." + ext[imgposid] ;
		var size=nwidth[imgposid]+'x'+nheight[imgposid];
		jQuery(el).parents(".emb_code").find(".normal").html("Normal <small>("+size+")</small>");	
	break;
	case "3":
		image_url="http://" + SITENAME + imagepath[imgposid] + "1000/" + basename[imgposid] + "_" + index[imgposid] + "." + ext[imgposid] ;
		var size=owidth[imgposid]+'x'+oheight[imgposid];
		jQuery(el).parents(".emb_code").find(".large").html("Full size <small>("+size+")</small>");	
	break;
	}

		var forweb='<a href="'+aurl[imgposid]+'"><img border="0" src="'+image_url+'" alt="Really funny stuff - funny funny videos and photos at '+ SITENAME +'"></a><br><a href="'+aurl[imgposid]+'">Funny Stuff at '+SITENAME+'</a><br/><br/>';
		jQuery(el).parents(".emb_code").find("textarea[name=forweb]").val(forweb);
		var forforum="[URL=http://" + SITENAME +"/][IMG]"+image_url+"[/IMG][/URL]";
		jQuery(el).parents(".emb_code").find("textarea[name=forforum]").val(forforum);
		var forim=aurl[imgposid];
		jQuery(el).parents(".emb_code").find("input[name=forim]").val(forim);
		var fordirect=image_url;
		jQuery(el).parents(".emb_code").find("input[name=fordirect]").val(fordirect);
}


function quotepic(picnum) {
	$("#comments").val($("#comments").val()+'#'+picnum);
}

function show_hide_embed(el) {	
	jQuery(".emb_code").not(el).hide();
	jQuery(".socials").hide();
	if (jQuery(el).parents(".imgbox").find(".emb_code").css('display') != 'block') {
		if (jQuery(el).parents(".imgbox").find(".emb_code").html()){
			jQuery(el).parents(".imgbox").find(".emb_code").fadeIn(300);
		} else {
			jQuery(el).parents(".imgbox").append("<div class='emb_code'>"+jQuery(el).parents(".imgbox").find(".embedd").html()+"</div>");
			jQuery(el).parents(".imgbox").find(".embedd").html('');
			jQuery(el).parents(".imgbox").find(".emb_code").fadeIn(300);
		}
	} else {
		jQuery(el).parents(".imgbox").find(".emb_code").fadeOut(300);
	}
}

function socialnet(el){
	jQuery(".emb_code").hide();
	jQuery(".socials").not(el).hide();
	if (jQuery(el).parents(".imgbox").find(".socials").css('display') != 'block') {
		if (jQuery(el).parents(".imgbox").find(".socials").html()){
			jQuery(el).parents(".imgbox").find(".socials").fadeIn(300);
		} else {
			jQuery(el).parents(".imgbox").append("<div class='socials'>"+jQuery(el).parents(".imgbox").find(".social_buttons").html()+"</div>");
			jQuery(el).parents(".imgbox").find(".social_buttons").html('');
			jQuery(el).parents(".imgbox").find(".socials").fadeIn(300);
		}
	} else {
		jQuery(el).parents(".imgbox").find(".socials").fadeOut(300);
	}

	stLight.options({publisher:'4b599cfb-5dd5-4470-9c12-61cba66f306e'});
	stLight.onReady();
}


function copythis(el) {
	el.select();
}

function getpictureurls(post_id) {
	jQuery.ajax({
			url: "http://" + window.location.hostname +"/engine/ajax/getpicurls.php",
			dataType: 'json',
			data: {
				post_id: post_id
			},
			success: function(resp){
//			favorites[imgposid]=resp.images;
//			jQuery(el).find(".tools").html(tools[imgposid]);
//			loadjsfile("http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d33058c0327e5bb");
			}
		});

}

function makeAvatar(post_id,img_id,confirmtext) {
    DLEconfirm( confirmtext, 'Confirmation!', function (){ 
		jQuery.ajax({
			url: "http://" + window.location.hostname +"/engine/ajax/makeavatar.php",
			dataType: 'json',
			data: {
				post_id: post_id,
				img_id: img_id,
			},
			success: function(resp) {
				jQuery('input[name=randavatarimg_id]').val('');
				jQuery('input[name=randavatarnews_id]').val('');
			}
		});
	});
}


(function($){
$.fn.highlightRegex = function(regex) {
    if(regex == undefined || regex.source == '') {
      $(this).find('span.highlight').each(function(){
        $(this).replaceWith($(this).text());
        $(this).parent().each(function(){
          $(this).get(0).normalize();
        });
      });
    } else {
      $(this).each(function(){
        elt = $(this).get(0)
        elt.normalize();
        $.each($.makeArray(elt.childNodes), function(i, node){
          if(node.nodeType == 3) {
            var searchnode = node
            while((pos = searchnode.data.search(regex)) >= 0) {
              match = searchnode.data.slice(pos).match(regex)[0];
              if(match.length == 0) break;
              var spannode = document.createElement('span');
              spannode.className = 'commentimage';
              var middlebit = searchnode.splitText(pos);
              var searchnode = middlebit.splitText(match.length);
              var middleclone = middlebit.cloneNode(true);
              spannode.appendChild(middleclone);
              searchnode.parentNode.replaceChild(spannode, middlebit);
            }
          } else {
            $(node).highlightRegex(regex);
          }
        })
      })
    }
    return $(this);
  }
})(jQuery);


function loadjsfile(filename){
  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", filename);
 }

function randomavatar(category_id) {
	jQuery.ajax({
			url: "http://" + window.location.hostname +"/engine/ajax/randomavatar.php",
			dataType: 'json',
			data: {
				category_id: category_id
			},
			success: function(resp) {
				jQuery('#randavatar').attr('src',resp.randavatar);
				jQuery('input[name=randavatarimg_id]').val(resp.img_id);	
				jQuery('input[name=randavatarnews_id]').val(resp.news_id);	
	
			}
		});

}

var p_id=0;

function get_id( id ) {
	return document.getElementById( id );
}

function answer( parent_id ) {
	get_id('comment_' + parent_id ).innerHTML = get_id('comment_' + p_id).innerHTML;
	get_id('dle-comments-form').parent_id.value =parent_id;
	if ( p_id != parent_id )
	{
		get_id( 'comment_' + p_id ).innerHTML 		= '';
      		get_id( 'comment_' + p_id ).style.display 	= 'none';
	}
	get_id('comment_' + parent_id ).style.display 	= '';

       // setNewField( get_id('sub_comments').name, get_id('sub_form'));
        p_id = parent_id;
}

function when_added() {
	get_id( 'add_main_comment' ).style.display = '';
}

function add_sub_comment() {
	var sub_form 	= get_id('sub_form');
	var comm_ajax 	= new dle_ajax();
     	closeall();
	if ( sub_form.sub_comments.value == '' || sub_form.name.value == '')
	{
		alert ( dle_req_field );
		return false;
	}
        comm_ajax.onShow();
	if ( sub_form.sec_code )
	{
		comm_ajax.setVar( "sec_code", sub_form.sec_code.value );
	}
	var varsString = "post_id=" + sub_form.post_id.value;
	comm_ajax.setVar( "comments",	comm_ajax.encodeVAR(sub_form.sub_comments.value));
	comm_ajax.setVar( "name", 		comm_ajax.encodeVAR(sub_form.sub_name.value));
	comm_ajax.setVar( "mail", 		comm_ajax.encodeVAR(sub_form.sub_mail.value));
	comm_ajax.setVar( "skin", 		dle_skin);
	comm_ajax.setVar( "parent_id",	p_id );
	comm_ajax.requestFile = dle_root + "engine/ajax/addcomments.php";
	comm_ajax.method  = "POST";
	comm_ajax.execute = true;
	comm_ajax.element = "comment_" + p_id;
	comm_ajax.onCompletion = when_added;
	comm_ajax.sendAJAX( varsString );
}

function close_answer () {
	if ( p_id ) {
		get_id( 'comment_' + p_id ).style.display = 'none';
		get_id( 'add_main_comment' ).style.display = '';
	}
}
