function getResponseData(rid, logid, index, width, height)
{
    jQuery.ajax({
	   url: 'rootcause.php?vaction=showresponse&vrid='+rid+'&vlogid='+logid+'&vindex='+index,
	   dataType: 'html',
	   timeout: 5000,
	   success: function (data) {
           fillDialog(data, width, height, 'Response');
	   },
	   error: function (XMLHttpRequest, textStatus, errorThrown) {
	   }
	});
}

function getHTMLoutput(rid, logid, width, height)
{
    jQuery.ajax({
       url: 'rootcause.php?vaction=showhtmloutput&vrid='+rid+'&vlogid='+logid,
       dataType: 'html',
       timeout: 5000,
       success: function (data) {
           fillDialog(data, width, height, 'HTML output');
       },
       error: function (XMLHttpRequest, textStatus, errorThrown) {
       }
    });
}

function getBigSnapshot()
{
	var medium_snap = $('input#vsnapshot_medium').val();
	var large_snap = $('input#vsnapshot_large').val();
	data = '<a href="'+large_snap+'"  target="_blank" ><img src="'+medium_snap+'" width="320" height="240" alt="web snapshot"  /></a>';
	fillDialog(data, 350, 295, 'Snapshot');
}

function refreshIsland(island, rid, logid, pmid)
{
    $jq('span#'+island+'-loading').show();
    jQuery.ajax({
	   url: 'rootcause.php?vaction=refresh&vwhat='+island+'&vrid='+rid+'&vlogid='+logid+'&vpmid='+pmid,
	   dataType: 'html',
	   timeout: 5000,
	   success: function (data) {
            updateIslandDiv(island, data, pmid);
	   },
	   error: function (XMLHttpRequest, textStatus, errorThrown) {
	   }
	});
}

function updateIslandDiv(island, data, pmid)
{
    $jq('div#'+island+'_'+pmid).html(data);
    $jq('span#'+island+'-loading').hide();
}

function toggleResponse(num)
{
    $jq('tr#response_'+num).toggleClass('nodisplay');
}

function openBrowserSnap(url, width, height, id)
{
	var content = '';
	if($('div#first-opinion').css('display')=="block")
		var containerId = 'first-opinion';
	else if($('div#second-opinion').css('display')=="block")
		var containerId = 'second-opinion';
	var numOfElem = $('div#'+containerId+' div.snapshot-div').size();
	var currentTimes = $('div#'+containerId+' div#snapshot_times_'+id).text()+' ('+(id+1)+'/'+numOfElem+')';
	
	content += '<table width="100%">';
	content += '<tr valign="middle"><td colspan=3><div style="height:'+height+'px;"><img height="'+height+'" width="'+width+'" src="'+url+'" onerror="onSnapError(this)" id="big-snapshot" snapid="'+id+'"/></div></td></tr>';
	content += '<tr>';
	content += '<td align="left">';
	content += '<a onclick="newBrowserSnap(0)" id="dialog-prev" href="javascript:void(0);"><img src="/assets/img/fancy_left.png"/></a>';
	content += '</td>';
	content += '<td>';
	content += '<div class="centered bold" id="big-snapshot-times">'+currentTimes+'</div>';
	content += '</td>';
	content += '<td align="right">';
	content += '<a onclick="newBrowserSnap(1)" id="dialog-next" href="javascript:void(0);"><img src="/assets/img/fancy_right.png"/></a>';
	content += '</td>';
	content += '</tr></table>';
	
	fillDialog(content, width+20, height+85, 'Browser snapshot');
	dialogButtonsHandle(id, numOfElem);
}


function newBrowserSnap(d)
{
	var currentSnapId = $('img#big-snapshot').attr('snapid');
	
	if(d==0) {
		currentSnapId--;
		if(currentSnapId<0)
			currentSnapId = 0;
	} else if(d==1) {
		currentSnapId++;
	}
	$('a[id^=snapshot_]:visible').each(function (i, e) {
		if(e.id=='snapshot_'+currentSnapId) {
			e = $(e).children('img');
			if($('div#first-opinion').css('display')=="block")
				var containerId = 'first-opinion';
			else if($('div#second-opinion').css('display')=="block")
				var containerId = 'second-opinion';
			var numOfElem = $('div#'+containerId+' div.snapshot-div').size();
			$('div#big-snapshot-times').html($('div#'+containerId+' div#snapshot_times_'+currentSnapId).text()+' ('+(currentSnapId+1)+'/'+numOfElem+')');
			$('img[id=big-snapshot]').attr('src',$(e).attr('src'));
			$('img[id=big-snapshot]').attr('snapid',currentSnapId);
			dialogButtonsHandle(currentSnapId, numOfElem);
		}
	});
}

function dialogButtonsHandle(currentSnapId, numOfElem)
{
	if((currentSnapId+1)==numOfElem)
	{
		$('a#dialog-prev').removeClass('trans25');
		$('a#dialog-next').addClass('trans25');
	}
	else if(currentSnapId==0)
	{
		$('a#dialog-prev').addClass('trans25');
		$('a#dialog-next').removeClass('trans25');
	}
	else
	{
		$('a#dialog-prev').removeClass('trans25');
		$('a#dialog-next').removeClass('trans25');
	}
}

function onSnapError(img)
{
	img.onerror=null;
	img.src='/assets/img/empty-img.jpg';
	img.onclick=null;
	$(img).removeClass('pointer-cursor');
}


