$(document).ready(function() {
 $(".scrollable").scrollable({circular: true}).autoscroll({interval: 7000});
 /*
 // add new effect to the tabs
$.tools.tabs.addEffect("horizontal", function(i, done) {

	// 1. upon hiding, the active pane has a ruby background color
	this.getPanes().fadeOut('100', function() {
    done.call();
  });


	// 2. after a pane is revealed, its background is set to its original color (transparent)
	this.getPanes().eq(i).fadeIn('100', function() {
    done.call();
  });

});
 */
 
 $("#accordion").tabs("#accordion div.accordion_item", {
 	tabs: 'img.tabs_header_img', 
 	effect: 'horizontal',
		initialIndex: 2
//		event: 'mouseover',
//		interval: 30000
 });	 
})


function CalculateRevenue(){
    var fields = {};
    $.each($('#calculateForm').serializeArray(), function(i, field) {
        fields[field.name] = field.value;
    });

    var auto_play_percent = 85; //85%
    var initiated_play_percent = 15; //15% 
   
    var play_percent = (fields.video_player_state == 1) ? auto_play_percent : initiated_play_percent;
    
    var estimated_video_streams = Math.floor(fields.page_views * play_percent / 100);      
    
    var preroll_x = 2;
    var ovrelay_per_video = 2;
    
    var preroll_low = 7;
    var preroll_height = 20;
    var preroll_medium = (preroll_low + preroll_height) / 2;     
    
    var ovrelay_low = 2.5;
    var ovrelay_height = 5;
    var ovrelay_medium = (ovrelay_low + ovrelay_height) / 2;     
    
    
    var estimated_ads = (estimated_video_streams / preroll_x) + (estimated_video_streams * ovrelay_per_video);
    
    
    var revenue_share_percentage = 0;
    if (estimated_ads > 750000) revenue_share_percentage = 20;
    else if (estimated_ads >= 500000) revenue_share_percentage = 15;
    else if (estimated_ads >= 250000) revenue_share_percentage = 10;
    else revenue_share_percentage = 0;
    
    
    
    var l = ((estimated_video_streams / 1000 / preroll_x * preroll_low * revenue_share_percentage) + (estimated_video_streams / 1000 * ovrelay_low * ovrelay_per_video * revenue_share_percentage)) / 30 / 100;
    var m = ((estimated_video_streams / 1000 / preroll_x * preroll_medium * revenue_share_percentage) + (estimated_video_streams / 1000 * ovrelay_medium * ovrelay_per_video * revenue_share_percentage)) / 30 / 100;
    var h = ((estimated_video_streams / 1000 / preroll_x * preroll_height * revenue_share_percentage) + (estimated_video_streams / 1000 * ovrelay_height * ovrelay_per_video * revenue_share_percentage)) / 30 / 100;
    
    
    $.each([1, 7.5, 30, 90, 360], function(i, v){
        $('#'+ Math.floor(v) +'_l').html('$'+ numberFormat(l * v)); 
        $('#'+ Math.floor(v) +'_m').html('$'+ numberFormat(m * v)); 
        $('#'+ Math.floor(v) +'_h').html('$'+ numberFormat(h * v));
    });
         
    
    $('#videoStreams').html(addCommas(estimated_video_streams));
    $('#estimatedAds').html(addCommas(estimated_ads));
    $('#sharePercentage').html(revenue_share_percentage +'%');
    
    
    var page_views = fields.page_views;
    
 
    var elm = 0;
    /*
    if (page_views >= 0 && page_views <= 50000) elm = 16 * estimated_video_streams / 1000;
    else if (page_views > 50000 && page_views <= 100000) elm = 14 * ((estimated_video_streams - 50000) / 1000) + 800;
    else if (page_views > 100000 && page_views <= 250000) elm = 12 * ((estimated_video_streams - 100000) / 1000) + 2200;
    else if (page_views > 250000 && page_views <= 500000) elm = 10 * ((estimated_video_streams - 250000) / 1000) + 5200;
    else if (page_views >= 500001) elm = 9 * ((estimated_video_streams - 500000) / 1000) + 10200;
    */
    
    elm = 6 * (estimated_video_streams / 1000);
    
    //All-Star Licensing
    /*
    var crm = 0;
    if (estimated_ads > 1000000) crm = 5;
    else if (estimated_ads > 500000) crm = 6;
    else if (estimated_ads > 250000) crm = 8;
    else crm = 10;
    */
    
    $('#videoStreams2').html(addCommas(estimated_video_streams));
    $('#estimatedLicensing').html(addCommas('$'+ numberFormat(elm)));
    


}

function handleNumberInput(self) {
    var value = $(self).val();
    if (!/^[0-9]+$/.test(value) || value.length > 14) $(self).val(value.substr(0, value.length - 1));                                                                                               
}



function numberFormat(nStr) {
    return new Number(nStr).numberFormat("#,###.00");
}

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;
}

