/* ON - Main Javascript
=============================================================================================*/
var ON = {};

ON.globals = {};

/* Onload ============================== */

$(document).ready( function()
{
	//$('.hide').hide();
   //$('.show').show();

	$('#breadcrumb').jBreadCrumb();

	$('#feedbackclose').click(function(event){
		event.preventDefault();
	});

	$('body').supersleight({shim: '/assets/images/pixel.gif'});

   $('#newsletterbtn').click( function(event) {
		event.preventDefault;

		var email = $('#newsletter').val();

		if( email.match(/[\w\.-]+@[\w\.-]+\.\w{2,4}/) != null )
		{
         var site = $('#thesite').text();
			var url = site + 'services/newslettersignup/';
			var data = { email: email };

			$.ajax({
				type: "POST",
				url: url,
				data: data,
				async: false,
				success: function(result)
				{
					if( result == 1 )
					{
						$('#newsletter').hide();
						$('#newsletterbtn').hide();
						$('#succesbox').show().removeClass('hide');
					}
				}
			});
		}
	});

	new ON.buttons();

	if(typeof sIFR == "function"){

		sIFR.replaceElement("div", named({
		sSelector:".sifr",
		sWmode: 'transparent',
		sFlashSrc:"/assets/fonts/stonesans_semi.swf",
		sColor:"#55ba47",
		sCase:"upper",
		nPaddingTop:0,
		nPaddingBottom:0
		}));
/*
		sIFR.replaceElement("div", named({
		sSelector:"h2",
		sWmode: 'transparent',
		sFlashSrc:"/assets/fonts/stonesans_semi.swf",
		sColor:"#55ba47",
		sCase:"upper",
		nPaddingTop:0,
		nPaddingBottom:0
		}));
*/
      sIFR.replaceElement("div", named({
		sSelector:".h2",
		sWmode: 'transparent',
		sFlashSrc:"/assets/fonts/stonesans_semi.swf",
		sColor:"#55ba47",
		sCase:"upper",
		nPaddingTop:0,
		nPaddingBottom:0
		}));

		sIFR.replaceElement("div", named({
		sSelector:"h3",
		sWmode: 'transparent',
		sFlashSrc:"/assets/fonts/stonesans_semi.swf",
		sColor:"#999999",
		sCase:"upper",
		nPaddingTop:0,
		nPaddingBottom:0
		}));

	}

	$("a").each(function(i){
		if( $(this).attr("href").match(/[\w\.-]+@[\w\.-]+\.\w{2,4}/) != null ){
			$(this).addClass('emaillink');
		}
      if( $(this).attr("href").match(/(((f|ht){1}tp:\/\/)(www.)?[meg\-3]+)/) != null ){
         $(this).attr('target', '_blank');
      }
	});

	$("ul#nav li").hover(function() {
			if( $('a:first', this).hasClass('mainnavlink') )
			{
				$('a:first', this).addClass('on');
			}
			$('ul:first',this).removeClass('hide').addClass('show');
		}, function() {
			$('ul:first',this).addClass('hide').removeClass('show');
			if( $('a:first', this).hasClass('mainnavlink') )
			{
				$('a:first', this).removeClass('on');
			}
		}
	);

	$('.media_image').fancybox();

});

ON.dropdown =
{
	init: function( theclass )
	{
		$('.'+theclass+' .current').mouseover(
			function(){ $(this).next().slideDown('fast'); }
		);

		$('.'+theclass).hover(
			function(){},
			function(){ $(this).find('.options').slideUp('fast'); }
		);

		$('.'+theclass+' .current').text( $('.'+theclass+' .default').text() );
	},

	admin: function( theclass )
	{
		$('.'+theclass+' .current').text( $('.'+theclass+' .default').text() );

		$('.'+theclass).change( function()
		{
			window.location.href = $('.'+theclass+' option:selected').attr('value');
		});
	}
};

ON.buttons = function( active, filter )
{
   var that = this;
   active = active||false;
   filter = filter||"img, input:image";

   $(filter).each(function(i, val)
   {
      if( $(val).attr('src').match(/_u/) != null )
      {
         $('<img>').attr( 'src', that.over( $(val).attr('src') ) );

         $(val).hover (
            function() { $(this).attr( 'src', that.over($(this).attr('src')) ); },
            function() { $(this).attr( 'src', that.reset($(this).attr('src')) ); }
         );

         if( active )
         {
            $('<img>').attr( 'src', that.active( $(val).attr('src') ) );

            $(val).mousedown (
               function() { $(this).attr( 'src', that.active($(this).attr('src')) ); }
            ).mouseup (
               function() { $(this).attr( 'src', that.over($(this).attr('src')) ); }
            );
         }
      }
   });
};

ON.buttons.prototype =
{
   over:   function( src ) { return src.replace(/(_a\.|_u\.)/, '_o.'); },
   active: function( src ) { return src.replace(/(_o\.|_u\.)/, '_a.'); },
   reset:  function( src ) { return src.replace(/(_o\.|_a\.)/, '_u.'); }
};

ON.login =
{
   init: function( that )
   {
		var data = $(that).serialize();
		var done = false;

		//$('.submitbtn').hide();
		//$('.loadingbtn').show();

		$.ajax({
			type: "POST", async: false, dataType: "json", url: "/services/login", data: data+'&ajax=1',
			success: function( result )
			{
				done = true;

				if( result != 1 )
				{
					$('.errorbox').show();

					$.each(result.errors, function( i, val )
					{
						$("#" + i).parent().addClass('fail').change( function(){ $(this).removeClass('fail'); } );
					});

					done = false;
				}
				else
				{
					$('.errorbox').hide();
				}

				if( done == false )
				{
					//$('.submitbtn').show();
					//$('.loadingbtn').hide();
				}
			}
		});

		return done;
   }
};

ON.contact =
{
   init: function( that )
   {
		var data = $(that).serialize();
		var done = false;

		$.ajax({
			type: "POST", async: false, dataType: "json", url: "/services/contact", data: data+'&ajax=1',
			success: function( result )
			{
				done = true;

				if( result != 1 )
				{
					$('.errorbox').show();

					$.each(result.errors, function( i, val )
					{
						$("#" + i).parent().addClass('fail').change( function(){ $(this).removeClass('fail'); } );
					});

					done = false;
				}
				else
				{
					$('.errorbox').hide();
				}

				if( done == false )
				{
					//$('.submitbtn').show();
					//$('.loadingbtn').hide();
				}
			}
		});

		return done;
   }
};

ON.forgotpass =
{
   init: function( that )
   {
		var data = $(that).serialize();

		$('#forgetpasswordbox .submitbtn').hide();
		$('#forgetpasswordbox .loadingbtn').show();

		$.ajax({
			type: "POST", async: false, dataType: "json", url: "/services/forgotpassword", data: data+'&ajax=1',
			success: function( result )
			{
				done = true;

				if( result != 1 )
				{
					$('#forgetpasswordbox .errorbox').show();

					$.each(result.errors, function( i, val )
					{
						$("#" + i).parent().addClass('fail').change( function(){ $(this).parent().removeClass('fail'); } );
					});

					$('#forgetpasswordbox .submitbtn').show();
					$('#forgetpasswordbox .loadingbtn').hide();

					done = false;
				}
				else
				{
					$('#forgotstart').hide();
					$('#forgetpasswordbox .errorbox').hide();
					$('#forgetpasswordbox .successbox').show();
					$('#forgetpasswordbox .loadingbtn').hide();
				}

				ON.overlay.jig();

				if( done == false )
				{
					$('#forgetpasswordbox .submitbtn').show();
					$('#forgetpasswordbox .loadingbtn').hide();
				}
			}
		});

		return false;
   }
};

ON.feedback = {

   height: 100,

   show: function()
   {
      this.anim_in();
      this.set_timer();
   },

   hide: function()
   {
      window.clearTimeout(this.timer); delete this.timer;
      this.anim_out( function(){
         $('#feedback .text').text( '' );
         ON.feedback.kill_class();
         $('#feedback').css( 'display', 'none' );
      } );
   },

   summon: function( text, type )
   {
		type = type || '';
      SIO.feedback.kill_class();
      $('#feedback').removeClass();
      $('#feedback').addClass(type);
      $('#feedback .text').html( text );
      this.show();
   },

	set_timer: function()
	{
		if( typeof this.timer == "number" ) { window.clearTimeout(this.timer); delete this.timer; }
      var time = ($('#feedback .text').text().length * 100);
      if( time < 10000 ) time = 10000;
      this.timer = window.setTimeout( function(){ ON.feedback.hide() }, time );
	},

   anim_in: function()
   {
		$('#feedback').css( { 'bottom': '-'+this.height+'px' } );

      if( $.browser.msie && ($.browser.version <= 6) )
      {
         $('#feedback').show();
         $('#feedback').css( 'opacity', 0 );
         $('#feedback').animate( { opacity: 1 }, 'fast' );
      }
      else
      {
         $('#feedback').show().animate( { bottom: '0px' }, 'fast' );
      }
   },

   anim_out: function( complete )
   {
      complete = complete || function(){ $('#feedback').css( 'display', 'none' ); };

      if( $.browser.msie && ($.browser.version <= 6) )
      {
         $('#feedback').animate( { opacity: 0 }, 'normal', function(){ $('#feedback').hide() } );
      }
      else
      {
         $('#feedback').animate( { bottom: '-'+ON.feedback.height+'px' }, 'normal', complete );
      }
   },

   kill_class: function( complete )
   {
      $('#feedback').removeClass('general');
      $('#feedback').removeClass('error');
      $('#feedback').removeClass('credits');
   }
};