var jsonRequest = new Request.JSON({
	onSuccess: function(json) {
		doSuccess(json);
	}
});

var vbs_total = 0; // view based campaign
var ccs_total = 0; // click based campaign
var ccb_total = 0; // blog sponsorship
var total = 0;

window.addEvent('domready', function() {


	if ($('newsletter_signup_form')) {
		avLockCheck = new FormCheck('newsletter_signup_form', {
			tipsClass : 'fc-tbx',
			display : {
				scrollToFirst : true,
				showErrors : 1
			},
			submitByAjax : true,
			ajaxResponseDiv: 'newsletter_signup_response',
			onAjaxSuccess : function(response) {
				//
			}
		})
	}
	
	if ($('company_contact_form')) {
		avLockCheck = new FormCheck('company_contact_form', {
			tipsClass : 'fc-tbx',
			display : {
				scrollToFirst : true,
				showErrors : 1
			},
			submitByAjax : true,
			ajaxResponseDiv: 'company_contact_response',
			onAjaxSuccess : function(response) {
				//
			}
		})
	}

	/*
	if ($('publisher-signup')) {
		avLockCheck = new FormCheck('publisher-signup', {
			tipsClass : 'fc-tbx',
			display : {
				scrollToFirst : true,
				showErrors : 1
			},
			submitByAjax : true,
			onAjaxSuccess : function(response) {
				doSuccess(response);
			}
		})
	}
	*/
	
	/*
	if ($('publisher-signup')) {
		$('publisher-signup').addEvent('submit', function(e){
			new Event(e).stop();
	
			jsonRequest.options.url = this.action;
			jsonRequest.send(this);
		});
	}
	*/


	/**
	* That CSS selector will find all <a> elements with the
	* attribute rel="boxed"
	*
	* The second argument sets additional options.
	*/
	SqueezeBox.assign($$('a[rel=boxed]'), {
		size: {x: 320, y: 273},
		parse: true,
		parseSecure: true,
		ajaxOptions: {
			method: 'get', // we use GET for requesting plain HTML
			evalScripts:true
		}
	});
	
	
	/*
	SqueezeBox.assign($('btn-sbox-login'), {
		size: {x: 270, y: 200},
		parse: true,
		parseSecure: true,
		handler: 'iframe',
		ajaxOptions: {
			method: 'get', // we use GET for requesting plain HTML
			evalScripts:true
		}
	});
	*/

	
	
	if ($('money-o-meter')) {
		
		var prefix = $('prefix').get('html');
		var place = $('prefix_place').get('html');

		var vbsSlider = new Slider('vbs-slider', 'vbs-knob', {
			range: [0, 5000],
			wheel: true,
			snap: true,
			onChange: function(pos){
				$('vbs-multi').set('html',pos);
				vbs_total = pos * $('vbs').get('html');
				calculateTotal(prefix, place);
			}
		});
		
		var ccsSlider = new Slider('ccs-slider', 'ccs-knob', {
			range: [0, 3000],
			wheel: true,
			snap: true,
			onChange: function(pos){
				$('ccs-multi').set('html',pos);
				ccs_total = pos * $('ccs').get('html');
				calculateTotal(prefix, place);
			}
		});
		
		var ccsSlider = new Slider('ccb-slider', 'ccb-knob', {
			range: [0, 1000],
			wheel: true,
			snap: true,
			onChange: function(pos){
				$('ccb-multi').set('html',pos);
				ccb_total = pos * $('ccb').get('html');
				calculateTotal(prefix, place);
			}
		});
	}
});

function setLanguage(flag)
{
	form = $('language_selector');
	form.language.value = flag;
	form.submit();	
}


function hoverRow(obj, flag)
{
	if (flag) {
		$(obj).addClass('on');
	} else {
		$(obj).removeClass('on');
	}
}

function calculateTotal(prefix, place)
{
	total = (vbs_total + ccs_total + ccb_total) / 1000;
	
	if (place=='right') {
		total = total.toFixed(2) + ' ' + prefix;
	} else {
		total = prefix + ' ' + total.toFixed(2);
	}

	$('mom-total').set('html',total);
}


function doSuccess(json)
{
	if (json.handshake == 'success') {
		location.href = json.redirect;
	} else {
		$(json.messagediv).set('html', json.handshake);
	}
}
