/* vars */
var checkFormStartText;
/* \vars*/

/* index functions */
function getRandom()
{
	return Math.floor(Math.random()*1000000000);
}

// return input field value
function getFieldId() {
	return $("#checkfield").attr('value');
}
// \return input field value

// input field animation
function fadeInput() {
	if( $('#checkfield').attr('value') == "")
	{
		$('#checkfield').css({color:"black"}).attr('value', checkFormStartText);
		$('#checkfield').animate({color : "black"},"slow");
	}
}

function focusInput() {
	if( $('#checkfield').attr('value') == "Insert valid URL and press Generate..." || $('#checkfield').attr('value') == "http://www.megaupload.com/?d=")
		$('#checkfield').attr('value', "");
}
// \input field animation

// empty function
function nothing() {
	return false;
}
// \empty function

// enable and disable input field functionability
function enableSubmit() {
	$("#checkform").bind('submit',beginCheck);
	$("#checkform").unbind('submit',nothing);
	$("#checkaction").bind('click',beginCheck);
	$("#checkaction").unbind('click',nothing);
	$("#checkfield").removeAttr("disabled");
}

function disableSubmit() {
	$("#checkform").unbind('submit',beginCheck);
	$("#checkform").bind('submit',nothing);
	$("#checkaction").unbind('click',beginCheck);
	$("#checkaction").bind('click',nothing);
	$("#checkfield").attr("disabled",true);
}

function beginCheck()
{
	// check input value
	if(!getFieldId() || getFieldId().length == 0 || getFieldId() == "http://www.megaupload.com/?d=" || getFieldId() == "Insert valid URL and press Generate..." ) 
	{
		alert('Invalid link');
		return false;
	}

	disableSubmit(); //lock
	
	$link=encodeURIComponent(getFieldId());
	window.location = 'http://megapid.com/?link='+$link;
	return false;
}
// \enable and disable input field functionability

// countdown
function waitcountdown(wait_time) 
{
	if(wait_time == 2)
	{
    	top.location.replace(self.location);
	}

	wait_time--;
	if(wait_time > -1)
	{
		$("#delay").html(wait_time);
		setTimeout('waitcountdown('+wait_time+')',1000);
	}
}

function countDown()
{
    num = parseInt( $('#countdown').html() )-1;
    if(num<=0)
    {
    	$("#btn_download").removeAttr("disabled");
        $('#countdown_str').hide();
    }
    else
    {
    	$("#btn_download").attr("disabled",true);
        $('#countdown').html(num);
        setTimeout("countDown()",1000);
    }
}
// \countdown

// statistics
function statistics() {
	setTimeout('statistics()',60000);
	$.get("/statistics", procesStat);
}

function procesStat(code) {
	var result;
	
	result	=	code.split('|');

	$("#statistics .account_num").html(result[0]);
	$("#statistics .today_num").html(result[1]);
	$("#statistics .today_size").html(result[2]);
	$("#statistics .total_num").html(result[3]);
	$("#statistics .total_size").html(result[4]);
}
// statistics
 
function noIframe() {
	var whitelist = ['digg.com', 'facebook.com', 'www.facebook.com', 'stumbleupon.com', 'www.stumbleupon.com'];
   	if (window.top.location != window.location) {
      var match = document.referrer.match(/^http?:\/\/([^:\/\s]+)\/?.*/);
      if (match) {
         var domain = match[1];

		 if ($.inArray(domain, whitelist) == '-1')
		 {
		    window.top.location = window.location;
		 }
      }
   }
}

/* start script */
$(document).ready(function()
{
  	if($('#btn_download'))
	{
		$("#btn_download").attr("disabled",true);
    	setTimeout("countDown()",1000);
	}

	if(!(typeof(link)=='undefined'||link===null))
	{
		$('#checkfield').attr('value', link);
	}
	
	if(!(typeof(direct_link)=='undefined'||direct_link===null))
	{
			// show proper div
			$('#resultcontainer').slideDown('fast');
			$("#premiumlink").attr('href', direct_link);
			$("#fileinfo").hide();
			$("#downloadlink").show();
	}
	
	// start input field animation
	$("#checkfield").bind('blur', fadeInput);
	$("#checkfield").bind('focus', focusInput);
	// \start input field animation

	statistics();
	
	// you may check
	enableSubmit();
	// \you may check

	noIframe();
});
/* \start script*/
