$(document).ready(function(){

	//updateforloginstatus ()

      $('#textfrag').keyup(function(e) {
      //alert(e.keyCode);
      if(e.keyCode == 13) {
          DoAjax_search(this,'0')
      }
      });

      $('#textfrag').click(function() {remove_search()} )
});

function remove_search() {
    var cnt=document.getElementById("textfrag").value
    if (cnt=="Search") document.getElementById("textfrag").value=""
}


function DoAjqByPost(poststr)	{

		scrollTo(0,200)
		$("#titlecol").prepend('<div id="progressmainholder"><img  id="progressmain" src="bigrotate.gif"></div>')
		$.ajax({
			type: "POST",
			url: "ajax_buildtitlesjq.php",
			data: poststr,
			dataType: "json",
			success: function(jr){
				$("#titlecol").html(jr.matrix.drie+jr.matrix.vier+jr.matrix.vyf+jr.matrix.een+jr.matrix.vier+jr.matrix.ses)
				$('.imgresize').unbind().click(function() { DoenAjaxSwappic_jq(this) })
			}
		});
}

function DoenAjaxGetByCategory(hierdie) {
	var cat = hierdie.id
	var poststr="category="+cat
	DoAjqByPost(poststr)
}

function DoenAjaxGetByCategoryStep(hierdie,step) { //specifies limits for batch retrieval
	var cat = hierdie.id
	//alert (cat)
	var poststr="category="+cat+"&step="+step
	DoAjqByPost(poststr)
}

function DoenAjaxGetByStudio(hierdie) {
	var cat = hierdie.id
	var poststr="studio="+cat
	DoAjqByPost(poststr)
}

function DoenAjaxGetByStudioStep(hierdie,step) {
	var cat = hierdie.id
	//alert (cat)
	var poststr="studio="+cat+"&step="+step
	DoAjqByPost(poststr)
}

function DoenAjaxGetByTitleId_inplace(obj) {
	//var cat = hierdie.id
	//alert (title_id)
	var poststr="title_id="+$(obj).attr('title')

	$(obj).html('<img  id="progressmain" src="progress.gif">')
		$.ajax({
   			type: "POST",
   			url: "ajax_buildtitlesjq.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$(obj).html(jr.matrix.drie+jr.matrix.vier+jr.matrix.vyf+jr.matrix.een+jr.matrix.vier)
				$(obj).find('.imgresize').click(function() { DoenAjaxSwappic_jq(this) })
				$(obj).unbind()

   			}
 		});
	}

function DoenAjaxGetByTitleId(title_id) {
	//var cat = hierdie.id
	//alert (title_id)
	var poststr="title_id="+title_id
	DoAjqByPost(poststr)
	}


function DoenAjaxListalljq(bywhat) {
/*	var target1=document.getElementById("titlecol")
	target1.innerHTML="<span class=\"catname\">Loading ...</span>"*/
//	ajaxpack.postAjaxRequest("ajax_listall.php", "bywhat="+bywhat, processGetPost, "txt")

		var poststr="bywhat="+bywhat
		$("#titlecol").prepend('<img id="progressmain" src="progress.gif">')
		$.ajax({
   			type: "POST",
   			url: "ajax_listalljq.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$("#titlecol").html(jr.matrix.drie+jr.matrix.vier+jr.matrix.vyf+jr.matrix.een)
				scrollTo(0,200)
				$('.title_t').unbind()
					.click( function() { DoenAjaxGetByTitleId_inplace(this) })
				$('.canexpand').unbind()
					.click(function() {
						var bywhat=$(this).attr('bywhat')
						var cat_studio_title=$(this).attr('catstudiotitle')
						DoenAjaxExpandjq(bywhat,cat_studio_title)
					 })
				.css('cursor','pointer')

   			}
 		});


}

function DoenAjaxExpandjq(bywhat,cat_studio_title) {
	var poststr="bywhat="+bywhat+"&title="+cat_studio_title
		$.ajax({
   			type: "POST",
   			url: "ajax_expandlistjq.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('#listall_'+jr.respz.een).html(jr.respz.vier+jr.respz.drie)
// 				var target1=document.getElementById("listall_"+matrix[1])
// 				target1.innerHTML=matrix[4]+matrix[3]

   			}
 		});


//	ajaxpack.postAjaxRequest("ajax_expandlist.php", poststr, processGetPost, "txt")
}


function DoenAjaxSwappic_jq(obj) { // ACTUALLY : loadthis, is_current:setthistonext,titleide
	var imgname=$(obj).attr('title')
	var currentwidth=$(obj).children('img').width()
   	if (currentwidth > 150) {
		currentwidth=150
		instruction="Click on picture to enlarge"
		$(obj).children('img').attr('src','./gimages/'+imgname).css('width','150px')
		$(obj).children('.clickinstruction').html(instruction)
		}
	else {
		currentwidth = 240
		instruction="Click on picture to reduce"
		$(obj).children('img').attr('src','./uploads/'+imgname).css('width','240px')
		$(obj).children('.clickinstruction').html(instruction)
		}
/*	$(obj).html("Loading ...")
	poststr="loadthis="+loadthis + "&dest="+titleid + "&storethis="+storethis+"&width="+currentwidth
	var targetsmallpic=document.getElementById("smallpic"+titleid)
	targetsmallpic.innerHTML=instruction
	ajaxpack.postAjaxRequest("fullpic.php", poststr, processGetPost, "txt")*/
}

function DoAjax_search(obj,step) { // navstrip wants to put a this as first parameter
	var textfrag=document.getElementById("textfrag").value
	var searchfield=document.getElementById("searchfield").value
	var poststr="textfrag="+textfrag+"&searchfield="+searchfield+"&step="+step
	DoAjqByPost(poststr)
}

function DoenAjaxGetByCategoryLiteral(literalid) {
	var poststr="category="+literalid //id of pic, not name of cat, cat gets looked up by pic id
	DoAjqByPost(poststr)
//	ajaxpack.postAjaxRequest("ajax_buildtitles.php", poststr, processGetPost, "txt")
	}

function DoenAjaxGetByStudioLiteral(literalid) {
	var poststr="studio="+literalid //id of pic, not name of cat, cat gets looked up by pic id
	DoAjqByPost(poststr)

//	ajaxpack.postAjaxRequest("ajax_buildtitles.php", poststr, processGetPost, "txt")
	}


