

function place_bid( rights ) {
	var amount = 0;
	var right = 0;
	var el,elem = '';
	switch (rights) {
		case 'all-rights' : elem='bid_all_rights'; break;	
		case 'cinematic' : elem='bid_cinematic'; right=1; break;	
		case 'video' : elem='bid_video'; right=2; break;	
		case 'vod' : elem='bid_tv'; right=3; break;	
		case 'ppv' : elem='bid_ppv'; right=4; break;	
		case 'free_tv' : elem='bid_free_tv'; right=5; break;	
		default : ;
	}
	
	if (elem) {
		el = document.getElementById(elem);
		amount = el.value;
		if ( amount != parseInt(amount,10) ) { alert('please enter a number'); el.focus(); return false; } 
		if (!check_bid(right,amount)) { alert('You have to put an amount bigger than '+auction_prices[right]+'.'); el.focus(); return false; }
		if ( confirm("Are you sure to bid on "+rights.toUpperCase()+' with amount of $'+amount+'?') ) {
			$.post('/postdata.php', {"haction":"auction_place_bid",
			   "id": id,
			   "amount":amount,
			   "right":right }, function(data) { 
			   		switch(data) {
						case '100' : alert('Bid successful!'); window.location.reload(); break; // success
						case '1010' : ;
						default : alert('Bid failed. Please log-in and try again. \nError code: ' + data);
					}
			   
			   }, "text");
		}
	}
}


function check_bid ( rights, amount ) { if ( amount <= auction_prices[rights] ) { return false; } return true; }

function system_message(code) {
	switch (code) {
		case '100' : /* no_error */ break;	
		case '1015' : alert("The session has been inactive for a long time. \nPlease re-login and try again."); break;
		case '1022' : alert("The server is busy. Please try again in a minute.");
		default : ;
	}	
}

function make_an_offer_click() {
	var msg = '';
	if ($("#make_offer_all").attr("checked")) { 
		msg+='   * Cinematic\n'; 
		msg+='   * Video / DVD\n';
		msg+='   * VOD Internet / Mobile / IPTV\n'; 
		msg+='   * Pay Per View / Pay Cable / Satellite TV\n';
		msg+='   * Free TV\n';
	} else {
		if ($("#make_offer_cinematic").attr("checked")) { msg+='   * Cinematic\n'; }
		if ($("#make_offer_video").attr("checked")) { msg+='   * Video / DVD\n'; }
		if ($("#make_offer_vod").attr("checked")) { msg+='   * VOD Internet / Mobile / IPTV\n'; }
		if ($("#make_offer_ppv").attr("checked")) { msg+='   * Pay Per View / Pay Cable / Satellite TV\n'; }
		if ($("#make_offer_free_tv").attr("checked")) { msg+='   * Free TV\n'; }
	}

	if (msg) {
		// msg = "Are you sure to Make an Offer\n - including rights:\n" + msg;
		if ( ! $("#make_offer_amount").val() ) { alert("Please enter offer amount."); $("#make_offer_amount").focus(); return false; }
		msg += '\n - for term of: ' + $("#make_offer_term").val()+" year(s)\n";
		msg += ' - for territory: ' + $("#make_offer_territory").text()+"\n";	
		msg += '\n with amount of $' + $("#make_offer_amount").val()+" ?";
		msg = "Are you sure to Make an Offer\n\n - including rights:\n" + msg;

		if (confirm(msg)) { 
			$.post("/postdata.php", 
				   {'haction' : 'auction_place_offer', 
				   	'id' : id, 
					'term' : $("#make_offer_term").val(), 
					'amount' : $("#make_offer_amount").val(), 
					'rights_all' : ($("#make_offer_all").attr("checked") ? '1' : '0'),
					'rights_cinematic' : ($("#make_offer_cinematic").attr("checked") ? '1' : '0'),
					'rights_video' : ($("#make_offer_video").attr("checked") ? '1' : '0'),
					'rights_vod' : ($("#make_offer_vod").attr("checked") ? '1' : '0'),
					'rights_ppv' : ($("#make_offer_ppv").attr("checked") ? '1' : '0'),
					'rights_free_tv' : ($("#make_offer_free_tv").attr("checked") ? '1' : '0') }, 
			function (code) { if (code=='100') { 
				alert('Offer successful!');
				$.nyroModalRemove(); 
			} else {  /* system_message(code); */ alert(code); } } );
		} 
	} else {
		alert("Please select rights that your are interested in.");
		return false;
	}	
}

$(document).ready(init_movie);

function init_movie() {
	$("#make_offer_hint").hover(function(){  $("#w_hint_make_offer").fadeIn('normal');   },function(){ $("#w_hint_make_offer").fadeOut('normal'); }); 	
	// $(".auction_images").nyroModal();
	$(".auction_images").lightBox();
	$("._lightbox").lightBox();
	$("#make_offer").nyroModal( { showBackground : function(elts, settings, callback) { elts.bg.css({opacity:0}).fadeTo(250, 0.50, callback); } , 
								   endShowContent : function () { 
								   									$("#button_make_an_offer").click( make_an_offer_click ); 
								   									//$("#make_offer_all").bind("change", function () { 
																	//		$(".make_an_offer_rights").attr( 'checked' , $("#make_offer_all").attr("checked")  ); 
																	//} );
															  } 
								} 	); 
	
	$("#contact_filmmaker_button").click(function(e) { 
									e.preventDefault();			  
									$.nyroModalManual({
										url : '/haction.php?haction=send_user_message_dialog&id='+filmmaker_id,
										showBackground : function(elts, settings, callback) { elts.bg.css({opacity:0}).fadeTo(250, 0.50, callback); } , 
								   		endShowContent:function () {  }
									});			  
								 }); 



	$(".login_button").click( movie_minilogin );
	$("#movie_password").keypress( function(e){ if (e.which==13) { movie_minilogin(); } } );
	
	$("#watch_screener_by_code").click(function(){
			var hash = $("#screener_hash").val();										
			if (hash=='' || hash==null) {
				alert("Please enter the Code to screen the movie. \n\nYou may ask filmmaker for a code to watch the screener.\n\nAlso, you may sign-up for a free buyer account and receive unlimited access to the screeners and pricing information.");	
			} else {
				location.href='/screen?token='+hash;	
			}
		});
	
}

function movie_minilogin() {
	if ($("#movie_email").val()!=''&&$("#movie_password").val()) { 
		$.post('/postdata.php', {"haction":"login_alt",
			   "email": $("#movie_email").val() ,
			   "password": $("#movie_password").val() }, function(data) { 
			   		switch(data) {
						case '100' : window.location.reload(); break; // success
						case '1002' : alert("Login failed. Email or password is wrong."); break;
						default : alert('Unknown error: ' + data);
					}
			   
			   }, "text");
	}
}

