// JavaScript Document

$(document).ready(function(){
	// Rollover and other functions
	$("#bottom img[align=left]").addClass("alignleft");
    $("#bottom img[align=right]").addClass("alignright");
    $("#bottom img[@src$=.png]").css("border", "none");


	$(".rollover").hover(
		function(){
			if($(this).attr("src").indexOf("-active") == -1 && $(this).attr("src").indexOf("#sticky") == -1) {
				if($(this).attr("src").indexOf(".gif") != -1) {
					var newSrc = $(this).attr("src").replace(".gif","_over.gif#hover");
					$(this).attr("src",newSrc);
				} else if($(this).attr("src").indexOf(".jpg") != -1) {
					var newSrc = $(this).attr("src").replace(".jpg","_over.jpg#hover");
					$(this).attr("src",newSrc);
				} else if($(this).attr("src").indexOf(".png") != -1) {
					var newSrc = $(this).attr("src").replace(".png","_over.png#hover");
					$(this).attr("src",newSrc);
				}
			
				
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.gif#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.gif#hover",".gif");
				$(this).attr("src",oldSrc);
			} else if($(this).attr("src").indexOf("_over.png#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.png#hover",".png");
				$(this).attr("src",oldSrc);
			} else if($(this).attr("src").indexOf("_over.jpg#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.jpg#hover",".jpg");
				$(this).attr("src",oldSrc);
			}
	
		}
	);
	
	
	$('.select_hide').each(function() {
									
		$(this).siblings('input').hide()
		$(this).change(function(){
			if (this.selectedIndex+1 == this.options.length) {
				$(this).siblings('input').show()
			} else {
				$(this).siblings('input').hide()
			}
		})
									
	})

	
	ma.ajax = [];
	ma.ajax_id = 0;
	ma.popup = {};
	ma.popup.aid = 0;
	ma.popup.enabled = true;
	ma.ajax_url = "/ajax/rotation";
	
	function slide_horizontal_right(type) {
				$('#' + type + "_loading").show();
				$('#' + type + "_loading").css({top:0, left: 950});
				$('#' + type + "_container").animate({left:-950,easing:'easeOutCubic'},800,function(){
					$('#' + type + "_main").html($('#' + type + "_loading").html());
					$('#' + type + "_container").css('left','0px');
					$('#' + type + "_loading").hide();
					ma[type].status = 0;
					ma_init_timer(type);															
				});
	}
	function slide_horizontal_left(type) {
				$('#' + type + "_loading").show();
				$('#' + type + "_loading").css({top:0, left: -950});
				$('#' + type + "_container").animate({left:950,easing:'easeOutCubic'},800,function(){
					$('#' + type + "_main").html($('#' + type + "_loading").html());
					$('#' + type + "_container").css('left','0px');
					$('#' + type + "_loading").hide();
					ma[type].status = 0;
					ma_init_timer(type);															
				});
	}
	function slide_vertical_down(type) {
				$('#' + type + "_loading").show();
				$('#' + type + "_loading").css({top: -280});
				$('#' + type + "_container").animate({top:280,easing:'easeOutCubic'},800,function(){

				$('#' + type + "_main").html($('#' + type + "_loading").html());
				$('#' + type + "_container").css('top','0px');
				$('#' + type + "_loading").hide();
				ma[type].status = 0;
				ma_init_timer(type);															
		});
	}
	function slide_vertical_up(type) {
				$('#' + type + "_loading").show();
				$('#' + type + "_loading").css({top: 280});
				$('#' + type + "_container").animate({top:-280,easing:'easeOutCubic'},800,function(){

				$('#' + type + "_main").html($('#' + type + "_loading").html());
				$('#' + type + "_container").css('top','0px');
				$('#' + type + "_loading").hide();
				ma[type].status = 0;
				ma_init_timer(type);															
		});
	}
	function fade(type) {	
		$('#' + type + "_main").fadeOut(800, function() {
			$('#' + type + "_main").html($('#' + type + "_loading").html());
			$('#' + type + "_main").fadeIn(800, function() {
				ma[type].status = 0;
				ma_init_timer(type);															
			});
		});
	}
	function simple_reveal(type) {	
		$('#' + type + "_main").html($('#' + type + "_loading").html()).show();
		ma[type].status = 0;
		ma_init_timer(type);															
	}
	function ma_do_call(d) {
		
		if (d.type == "quote") {
			var quote_id = quote_node_ids[quote_pointer];
			
			quote_pointer++;
			if (quote_pointer>=quote_node_ids.length) {
				quote_pointer = 0;	
			}
			var full_url = ma.ajax_url + "?type=" + d.type + "&id=" + quote_id + "&section=" + d.section;
		} else {
			var full_url = ma.ajax_url + "?type=" + d.type + "&id=" + d.id + "&section=" + d.section;
		}
		ma[d.type].aid = d.aid;
		$.getJSON(full_url,
			function(data){
			 	if(d.aid==ma[d.type].aid) {
					 $("#" + d.type + "_loading").html(data.content);
					if (d.callback) {
						d.callback(d.type);	
					}
				}
			});	
	}
	function ma_init_load(type, increment, no_ani) {
		if(!ma[type].status) {
			
			ma[type].status=true;
			clearTimeout(ma[type].timeout);
			ma.ajax_id++;
			ma.ajax[ma.ajax_id] = {};
			ma.ajax[ma.ajax_id].type = type;
			ma.ajax[ma.ajax_id].section = ma[type].section;
			ma.ajax[ma.ajax_id].id = ma[type].pointer;
			ma[type].pointer+=parseInt(increment);
		
			if (ma[type].pointer > ma[type].maximum) {
				ma[type].pointer = 0;	
			} else if (ma[type].pointer < 0) {
				ma[type].pointer = ma[type].maximum;	
			}
			if (no_ani) {
				ma.ajax[ma.ajax_id].callback = simple_reveal;
			} else {
				if (ma[type].ani == "slide_vertical") {
					if (increment < 0) {
						ma.ajax[ma.ajax_id].callback = slide_vertical_up;
					} else {
						ma.ajax[ma.ajax_id].callback = slide_vertical_down;
					}
				} else if (ma[type].ani == "slide_horizontal") {
					if (increment < 0) {
						ma.ajax[ma.ajax_id].callback = slide_horizontal_left;
					} else {
						ma.ajax[ma.ajax_id].callback = slide_horizontal_right;
					}
				} else {
					ma.ajax[ma.ajax_id].callback = fade;
				}
			}
			ma.ajax[ma.ajax_id].aid = ma.ajax_id;
			ma_do_call(ma.ajax[ma.ajax_id]);
		}
	}
	function ma_init_timer(type) {
		clearTimeout(ma[type].timeout);
		if(ma[type].enabled){
			ma[type].timeout = setTimeout(function(){
				ma_init_load(type, 1, false);
			},ma[type].interval*1000);
		}
	}
	
	function ma_init(type) {
		
		if (ma[type].ani == "slide_vertical") {
			$("#" + type + "_loading").addClass("slider");	
		} else if (ma[type].ani == "slide_horizontal") {
			$("#" + type + "_loading").addClass("hslider");	
		}
		if (type == "promo") {
			var controls = '';
			controls += '<a href="#" class="arrow_back">back</a>';
			controls += '<a href="#" class="arrow_next">next</a>';
			controls += '<a href="#" id="arrow_resume" class="arrow_pause hiddenset">resume rotation <img src="images/buttons/play.png" width="12" height="8" /></a>';
			controls += '<a href="#" id="arrow_pause" class="arrow_pause">pause rotation <img src="images/buttons/pause.png" width="12" height="8" /></a>';



			$("#promo_controls").html(controls);
			$(".arrow_back").bind("click",function(e){
				clearTimeout(ma.promo.timeout);	
				ma_init_load("promo", -1, false);
				return false;
												   
			});
			$(".arrow_next").bind("click",function(e){
				clearTimeout(ma.promo.timeout);	
				ma_init_load("promo", 1, false);
				return false;
												   
			});
			$(".arrow_pause").bind("click",function(e){	
				if (ma.promo.enabled) {
					clearTimeout(ma.promo.timeout);	   
					$("#arrow_pause").hide();
					$("#arrow_resume").show();
					ma.promo.enabled = false;
				} else {
					ma_init_load("promo", 1, false);
					$("#arrow_pause").show();
					$("#arrow_resume").hide();
					ma.promo.enabled = true;
				}
				return false;
			});
		}
		
		ma_init_load(type, 1, true);
	}
	// Initialize the rotating elements
	
	var quote_pointer = 0;
	for (var x in page_elements) {
		var is_ok = true;
		if (x == "quote" && quote_node_ids.length<1) {
			is_ok = false;	
		}
		if (is_ok) {
		ma[x] = {};
		ma[x].aid = 0;
		ma[x].pointer = 0;
		ma[x].enabled = true;
		ma[x].status = 0;
		ma[x].timeout = null;
		ma[x].section = (page_elements[x]["section"]) ? page_elements[x]["section"] : x;
		ma[x].ani = (page_elements[x]["ani"]) ? page_elements[x]["ani"] : 'fade';
		ma[x].maximum = page_elements[x]["max"];
		ma[x].interval = (page_elements[x]["interval"]) ? page_elements[x]["interval"] : 5;
		ma_init(x);	
		}
	}	

});

  // ajax request to subscribe an email address to the mailing list
  function ma_subscribe(subscriberaddress) {
    $.post("/ajax/mailinglist/subscribe", { subscriberaddress: subscriberaddress },
      function(data){
        if (data.error) {
          $('#subscribe_status').css('color', 'red');
          $('#subscribe_status').html(data.error);
        } else {
          $('#subscribe_status').css('color', 'green');
          $('#subscribe_status').html(data.content);
        }
      }, "json"
    );
  }

  // ajax request to unsubscribe an email address to the mailing list
  function ma_unsubscribe(subscriberaddress) {
    $.post("/ajax/mailinglist/unsubscribe", { subscriberaddress: subscriberaddress },
      function(data){
        if (data.error) {
          $('#subscribe_status').css('color', 'red');
          $('#subscribe_status').html(data.error);
        } else {
          $('#subscribe_status').css('color', 'green');
          $('#subscribe_status').html(data.content);
        }
      }, "json"
    );
  }

