/**
 * @author info@30.com.au
 * Kyle Ryan
 */

var DSNUMLINKS = 0;
var DSDATA = new Object;
var DSDATA = new Object;
DSDATA  = {
	CONVERTLINK: ''
}
$(document).ready(function(){

	$('input#urlinput').hint();
	$('#urlForm').submit(function(){
		if ( $('#urlinput').val() == '') {
			return false;
		}	
	});
	
	
	
	initCheckBox();
	
	initDownloadPage();
	
});

var DSID = '';
function initDownloadPage()
{
	DSID = $('#taskid').html();

	if ((DSID == '') || (DSID <=0) ) return;
	if (DSID > 0) {
		ajaxUpdater();	
	} 	
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function ajaxUpdater() {
	  
	  $.ajax({
            type: 'GET',
			url: 'getTaskStatus.php',
			data:'taskid=' + DSID,
            success: function(data, it){
				$('#dDetails h3').html(data.MSG);
				var status = parseInt(data.STATUS);
				switch(status) {
					case 0: 
							delayAjaxUpdater();
							break;
					case 1: 
							var tsize = parseFloat(data.TSIZE);
							var dsize = parseFloat(data.DSIZE);
							var perc =  parseFloat(data.DSIZE /data.TSIZE);
							var newwidth = parseInt(200 * perc);
							$('#hload span').stop().animate({
								'width': newwidth + 'px'
							},1000);
							var p = parseInt(perc*100)  + '% (' + addCommas(parseInt(data.DSIZE/1024))+'kb/'+ addCommas(parseInt(data.TSIZE/1024)) +' kb)' + ' downloaded';
							
							$('#hloadtext').html(p);
							delayAjaxUpdater();
							break;
					case 2:delayAjaxUpdater();break;
					case 3: $('#hload span').css('width','0px');
							$('#hload span').stop().animate({
								'width':'200px'
							},1000);
							$('#hloadtext').html('Processing conversion algorithm...');
							delayAjaxUpdater();break;		
					case 4:// everything done
							$('#hload span').stop().animate({
								'width':  '210px'
							},1000,function(){
								//$('#hload').addClass('h');
								//$('#convertionHld').html(data.DLINK);
								$('#hload').addClass('h');
								$('#hloadtext').addClass('h');
								DSDATA.CONVERTLINK  = window.location.href;

								var redownloadLink = '<p><a href="'+DSDATA.CONVERTLINK+'">Problems with this download? Try again by clicking here</a></p>'
								$('#hloadlink').html(data.DLINK+redownloadLink);
							});
							break;		
					
				}
           },
		   error: function(data,it) {
		   		delayAjaxUpdater();
		   },
		   dataType: 'json',
           timeout: 10000,
           cache: false
	});		
				
}

function delayAjaxUpdater() {
	setTimeout("ajaxUpdater()",5000);
	return false;
}


function colorRows() {
	$('#dDetails ul li:even').addClass('even');
}

function initCheckBox() {
	
	DSDATA['mediaLinks'] = $('#dDetails ul li.DSmedia').length;
	
	DSNUMLINKS = $('#dDetails ul li').length;
	$('input#DSall:checkbox').attr('title',DSNUMLINKS);
	$('input#DSjs:checkbox').attr('title',$('#dDetails ul li.DSjs').length + ' javascript files');
	$('input#DScss:checkbox').attr('title',$('#dDetails ul li.DScss').length + ' stylesheet css files');
	$('input#DSimg:checkbox').attr('title',$('#dDetails ul li.DSimg').length + '  image files');
	$('input#DSanchor:checkbox').attr('title',$('#dDetails ul li.DSanchor').length + ' links');
	$('input#DSobject:checkbox').attr('title',$('#dDetails ul li.DSobject').length + ' objects');
	$('input#DSmedia:checkbox').attr('title',DSDATA['mediaLinks'] + ' media files');

	// if media files are there, show only media
	if (DSDATA['mediaLinks'] > 0) {
		$("#filters  input:checkbox").attr('checked', '');
		$("#filters  input#DSmedia:checkbox").attr('checked', '1');
		displaySelect();
	}
	displaySelected();
	$('#filters input:checkbox').click(function(){
		if ($(this).attr('id') == 'DSall') {
			if ($("#filters  input#DSall:checkbox").attr('checked')) {
				$('#filters   input:checkbox').attr('checked', '1');
			} else {
				$('#filters   input:checkbox').attr('checked', '');
			}	
		} else {
			if (!($(this).attr('checked'))) {
				$('input#DSall:checkbox').attr('checked', '');
			}	
		}
		displaySelected();
	});
	
}

function displaySelected() {


	if ($("#filters input#DSall:checkbox").attr('checked')) {
		
		$('#dDetails ul li').removeClass('hidden');	
	} else {
		$('#dDetails ul li').addClass('hidden');	
	}

	if ($("input#DSjs:checkbox").attr('checked')) {
		
		$('#dDetails ul li.DSjs').removeClass('hidden');	
	}
	
	if ($("input#DSimg:checkbox").attr('checked')) {
		$('#dDetails ul li.DSimg').removeClass('hidden');	
	}
	
	if ($("input#DScss:checkbox").attr('checked')) {
		$('#dDetails ul li.DScss').removeClass('hidden');	
	}			
	
	if ($("input#DSanchor:checkbox").attr('checked')) {
		$('#dDetails ul li.DSanchor').removeClass('hidden');	
	}

	if ($("input#DSobject:checkbox").attr('checked')) {
		$('#dDetails ul li.DSobject').removeClass('hidden');	
	}	
	
	if ($("input#DSmedia:checkbox").attr('checked')) {
		$('#dDetails ul li.DSmedia').removeClass('hidden');	
	}
		
	colorRows();
	
	var currentCount = $('#dDetails ul li:not(.hidden)').length;
	if (DSNUMLINKS > 0) {
	$('#hdrStatistics').html('Showing ' + currentCount + ' of ' + DSNUMLINKS + ' Items');
	}
	
}


function displaySelect() {
	$('input.formatsel').click(function(){

		var ct = $(this).prev().children(':selected').attr('value');
		var combData = $(this).parent().prev().html();
		var combAr = new Array;
		combAr = combData.split('$#$');
		var url = $(this).parent().prev().prev().html();
		var newL = 'convert.php?ctype='+ct+'&dtype='+combAr[0] + '&ytvideoid='+combAr[1]+ '&yttitle='+combAr[2]+'&'+url;
		window.location.href =newL;

		});

}

jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};