$(function()
{
	//Process suggest form
	$(".inputSubmit").click(function(){  
		$(this).hide();	
	});
	
	$('.error').hide(); 
	$("#submit_suggest").click(function(){  
		$('.error').hide(); 
		
		var name = $("input#name").val();   		name = name.replace("&", "&amp;");   
        if (name == "") {  
	        //$("label#company_error").show();  
	        $("input#field_name").focus();     
	        return false;  
        }  
        
        var company = $("input#company").val();   	company = company.replace("&", "&amp;");   	
        if (company == "") {  
	        //$("label#company_error").show();  
	        $("input#field_company").focus();     
	        return false;  
        }  
       
        var ticker = $("input#ticker").val();     ticker = ticker.replace("&", "&amp;");         
        if (ticker == "") {  
	        //$("label#ticker_error").show();  
	        $("input#ticker").focus();  
	        return false;  
        }         
        
        var reason = $("#reason").val();         reason = reason.replace("&", "&amp;");
        if (reason == "") {  
	        //$("label#reason_error").show();  
	        $("input#field_reason").focus();  
	        return false;  
        }  
        
         //Encode &
		name = name.replace(/&/g, "%26");  
		company = company.replace(/&/g, "%26");        
		ticker = ticker.replace(/&/g, "%26"); 
		reason = reason.replace(/&/g, "%26"); 
        
        var dataString = 'name=' + name + '&company=' + company + '&ticker=' + ticker + '&reason=' + reason;
		//alert (dataString);return false;
		$.ajax({
		    type: "POST",
		    url: "process_suggest.php",
		    data: dataString,
		    success: function() {
		        $('#suggest_result').html("<div id='message'></div>");
		        $('#message').html("<h2 class='h-title'>Thank you!</h2>")
		        .append("<p>We will consider your suggestion.</p>")
		        .hide()
		        .fadeIn(1500, function() {
		         	 //$('#message').append("<img id='checkmark' src='images/check.png' />");
		        });
		    }
		});
		return false;

    });  
	//end process form
	
	$('.hr-lnews').html('<img src="' + cnf.vir_path + 'templates/media/ajax-loader.gif" width="220" height="19">');
	
	$('#login_email').focus(function()
	{
		if (this.value == 'Email Address') {
			this.value = '';
		}
	}).blur(function()
	{
		if (this.value == '') {
			this.value = 'Email Address';
		}
	});

	$('#home_tickern').focus(function()
	{
		if (this.value == 'Ticker or Name') {
			this.value = '';
		}
	}).blur(function()
	{
		if (this.value == '') {
			this.value = 'Ticker or Name';
		}
	});

	$('input[alt*="date_"]').focus(function()
	{
		if ((this.alt == 'date_month' && this.value == 'MM') || (this.alt == 'date_day' && this.value == 'DD') || (this.alt == 'date_year' && this.value == 'YYYY')) {
			this.value = '';
		}
	}).blur(function()
	{
		if (this.value == '')
		{
			if (this.alt == 'date_month') {
				this.value = 'MM';
			}
			else if (this.alt == 'date_day') {
				this.value = 'DD';
			}
			else if (this.alt == 'date_year') {
				this.value = 'YYYY';
			}
		}
	});


	/* check if exists market block */
	if($('.nav-info').length) {
		updateMarket(true);
	}
	
	//-------------------------------------------------------------------------
	// READ FEEDS
	//-------------------------------------------------------------------------
	$.ajax({
		url: cnf.vir_path + "libs/lib.get_news.php",
		//data: "m=get_news",
		cache: true,
		success: function(data, textStatus)
		{
			var arr = data.split('|$|');
			
			$('.hr-lnews').html(data);
		}
	});
	
	
});


function updateMarket(first) 
{
	var self, _colorc;
	$.get(cnf.vir_path + 'index.php?m=get_ajax&p=get_market', function (responseText)
	{
		data = eval("(" + responseText + ")");
		$('#item_dtime').html(data.time);
		$.each(data, function()
		{
			if (this.name)
			{
				self = $('#item_' + this.name);
				_colorc = first || this.last_bid == $(self).find('.ni-newrap').html() ? false : true;
				$(self).find('.ni-newrap').html(this.last_bid);
				$(self).find('.ni-leftdata').html(this.days_change).removeClass(this.days_procentage < 0 ? 'ni-up' : 'ni-down').addClass(this.days_procentage < 0 ? 'ni-down' : 'ni-up');
				$(self).find('.ni-rightdate').html(this.days_procentage + '%');

				if (_colorc)
				{
					$(self).find('.ni-newrap, .ni-leftdata, .ni-rightdate').animate({'color': 'red'}, 2000);
					setTimeout('deleteMarketColors()', 5000);
				}

				// show nav info block
				if (first) {
					$('.nav-info').slideDown();
				}
			}
		});
	});

	setTimeout("updateMarket()", 30000);
}


function deleteMarketColors()
{
	$('.ni-newrap').animate({'color': '#666666'}, 2000);			
	$('.ni-leftdata, .ni-rightdate').animate({'color': '#000000'}, 2000);
}

function popCentered(url, widthh, heightt, params)
{

    var tp = Math.ceil((screen.height - heightt) / 2);
    var lf = Math.ceil((screen.width - widthh) / 2);
    if (params.length > 0)
		params = "," + params;

    var newwindow=window.open(url,'',"width=" + widthh + ",height=" + heightt + ",top=" + tp + ",left=" + lf + params);
	if (window.focus)
		newwindow.focus();

    return newwindow;
}







