




var help = new function()
    {
        this.on = false;
        this.speed = 500;
        var self = this;

	this.init = function()
	{
	    if($('#help #help_inner').css('display') == "block")
		{
		    self.on = true;
		}
	};
	
	this.continueToggle = function()
	{
	    if(!self.on)
		{
		    $('#help #help_inner').slideDown(self.speed, self.contentIn);
		}
	    else
		{
		    self.contentOut();
		}
	};
	
	this.toggle = function()
	{
	    $.ajax({
		    url: "http://"+WEB_ROOT+PUBLIC_DIR+"/admin/toggle_help",
		    timeout: 5000,
		    type: 'GET',
		    dataType: 'json',
		    success: function(data, textStatus){
			self.continueToggle();
		    },
		    error: function(x, txt, e){
			alert(txt);
		    }
		});
	};
	
	this.contentIn = function()
	{
	    $('#help #help_inner div').fadeTo(self.speed/3, 1, function(){
		    self.on = true;
		});
	};
	       	
	
	this.contentOut = function()
	{
	    $('#help #help_inner div').fadeTo(self.speed/3, 0, self.hideHelp);
	    
	};
	
	this.hideHelp = function()
	{
	    $('#help #help_inner').slideUp(self.speed, function(){
		    self.on = false;
		});
	};	       
    };



var video_ui = new function()
{
    this.position = 1;
    var self = this;

    this.init = function()
    {
	self.position = $('#video_position').text();
	self.position = self.position - 2;

	$(window).load(function(){		
		$('#video').jcarousel({scroll: 1, vertical: true, start: self.position});		      
		$('#video').css('visibility', 'visible');
	    });
	
	/*
	  $('#video li a.thumb').bind("click", function(e){
	  e.preventDefault();
	  $this = $(this);		    		    
	  var link = $this.attr('src');
	  var thumb = $this.find('img');
	  var image = thumb.attr('src').replace('tn_', '');
	  var movie = image.replace('.jpg', '');
	  
	  $('#video li a.thumb img').each(function(){
	  $this = $(this);
	  $this.removeClass('current');
	  });
	  thumb.addClass('current');
	  
	  sendEvent('stop');
	  loadFile({file: movie,
	  title: 'Some video',
	  link: link,
	  id:'12345',image: image});					    
	  });
	*/

    };
};




var video_upload = new function()
    {
	this.locked = 0;
	this.form;
	this.iframe_container;
	
	var self = this;
	
	this.init = function()
	{
	    self.form = $('#video_upload form');
	    self.spawn_loading();
	    self.spawn_iframe_container();
	    self.spawn_iframe();			    
	
	    $('#upload').load(function(){
		    if(video_upload.locked == 1)
			{
			    video_upload.reset();
			    var error = $('#upload').contents().find('#error li').text();
			    if(error.length != 0)
				{
				    video_upload.error(error);
				}
			    else
				{
				    window.location = window.location.toString();
				}
			}
		});

	    this.reset = function()
	    {
		video_upload.locked = 0;			    
		video_upload.hide_loading();
		video_upload.enable();
		$('#video_upload #error').remove();
	    }
		   
	    self.form.bind('submit', function(e){		    		    
		    e.preventDefault(); 
		    if(self.locked == 0)
			{
			    self.locked = 1;		
			    var $this = $(this);		 	    
			    self.form.attr('target', 'upload');	       
			    self.form.submit();
			    self.disable();
			    self.show_loading();			    			    			    
			}
		});
	};

	this.begin_poll = function()
	{
	    var count = 1;
	    //while(self.locked == 1)
	    //	{
		    $.ajax({
			    url: window.location.toString(),
			    timeout: 5000,
			    type: 'POST',
			    dataType: 'json',
			    data: 'count='+self.id+'&value='+self.current_text,
			    success: function(data, textStatus){
				alert('hello');
				//								    						    
			    },
			    error: function(x, txt, e){
				alert(txt);
			    }
			});		    			    
	    //	}
	};

	this.spawn_iframe_container = function()
	{
	    $('<div style="display: none;" id="iframe_container"></div>').insertAfter(self.form);
	    self.iframe_container = $('#video_upload #iframe_container');
	};
	
	this.spawn_iframe = function()
	{
	    var src = window.location.toString()+'?iframe=true';
	    self.iframe_container.append('<iframe id="upload" name="upload" src="'+src+'"></iframe>');
	};

	this.spawn_loading = function()
	{
	    $('<img style="visibility: hidden;" src="http://'+WEB_ROOT+PUBLIC_DIR+'/img/Loading.gif" alt="" />').insertAfter('#video_upload form');
	};

	this.show_loading = function()
	{
	    $('#video_upload img').css('visibility', 'visible');
	};

	this.hide_loading = function()
	{
	    $('#video_upload img').css('visibility', 'hidden');
	};

	this.remove_iframe = function()
	{
	    self.iframe_container.empty();
	};

	this.error = function(msg)
	{	    
	    $('<ul id="error"><li>'+msg+'</li></ul>').insertBefore('#video_upload form');
	};

	this.disable = function()
	{
	    $('#video_upload form button').attr('disabled', 'disabled');
	};

	this.enable = function()
	{
	    $('#video_upload form button').attr('disabled', '');
	};

	this.upload = function()
	{	  	 
	    self.locked = 1;
	    
	};

    };



var enlarger = new function()
    {     
	var current_image;
	var interval;
	var enlarged;
	var fade;
	var active;
	var self = this;
	
	
	this.resize = function()
	{
	    if(self.active == true)
		{
		    var width = self.current_image.width;
		    var height = self.current_image.height;
		    self.enlarged.css({
			    'left': ($('body').width() - width) / 2,
				'top': ($('body').height() - height) / 2});				
		    //  $('.enlarger_fade').css({'height': '100%', 'width': '100%'});
		}
	};
	
	this.load_image_complete = function()
	{
	    self.active = true;    
	    $('body').css({'overflow':'hidden'});
	    self.resize();
	    self.fade.css({'display':'block'});
	    self.fade.fadeTo("fast", 0.6);
	    self.enlarged.attr('src', self.current_image.src).fadeIn("fast");				
	};
	
	this.load_image = function()
	{
	    self.interval = setInterval("enlarger.watch_load()", 100);
	};
	
	this.watch_load = function()
	{
	    if(self.current_image.complete)
		{		
		    clearInterval(self.interval);
		    self.load_image_complete();
		}	    
	};

	
	this.init = function()
	{
	    self.active = false;
	    self.enlarged = $('<img/>')
	    .addClass('enlarged').hide().appendTo('body');

	    self.fade = $('<div/>')
	    .addClass('enlarger_fade').appendTo('body')
	    .css({'left': 0, 'top': 0, 'display':'none'}).fadeTo("fast", 0);



	    $('#ads img, .entry img').bind('click', function(e){
		    e.preventDefault();
		    $this = $(this);
		    var src = $this.attr('src');
		    src = src.replace(/tn_/, '');

		    self.current_image = new Image();
		    self.current_image.src = src;
		    self.load_image();		    		  
		});

	    $('.enlarged').bind('click', function(e){
		    $this = $(this);
		    self.fade.fadeOut("fast");
		    $this.fadeOut("fast");
		    self.active = false;
		    $('body').css({'overflow':'auto'});
		});
	};
    };


var image_rotate = new function()
    {
	this.images = new Array();
	this.img_index = 0;
	this.switched = 0;
	this.inited = 0;
	this.url = 'http://'+WEB_ROOT+PUBLIC_DIR+'/uploads/';
	this.interval;
	this.current_image;
	this.loading = $('#rider_images #load');

	var self = this;

	this.watch_load = function()
	{
	    if(self.current_image.complete)
		{		
		    clearInterval(self.interval);
		    self.load_image_complete();
		}	    
	};

	this.load_image_complete = function()
	{	    	    
	    $('#rider_images #count span').empty().append(self.img_index + 1);	    
	    $('#rider_images p img').attr('src', self.url+self.images[self.img_index]);
	    $('#rider_images img#load').css('visibility', 'hidden');
	};
	

	this.load_image = function()
	{
	    self.interval = setInterval("image_rotate.watch_load()", 1000); 
	};


	this.do_rotate = function()
	{	
	    $('#rider_images img#load').css('visibility', 'visible');
	    if(image_rotate.inited == 0)
		{
		    image_rotate.init();				
		}
	    else
		{
		    image_rotate.rotate();
		}	    	    
	};

	this.init = function()
	{
	    self.inited = 1;
	    $.ajax({
		    url: window.location.toString(),
			dataType: 'json',
			timeout: 5000,
			success: function(data, textStatus){			
			$.each(data, function(entryIndex, entry){
				self.images.push(entry);
			    });		
			image_rotate.rotate();
		    },
			error: function(x, txt, e){
			alert(txt);
		    }
		});	    	
	};    
	
	this.rotate = function()
	{
	    if((self.img_index + 1) == self.images.length)
		{
		    self.img_index = 0;
		}
	    
	    self.switched = 0;   
	    while(self.switched == 0 && self.img_index < self.images.length)
		{
		    if($('#rider_images p img').attr('src') != self.url+self.images[self.img_index])
			{
			    self.switched = 1;						    
			    self.current_image = new Image();
			    self.current_image.src = self.url+self.images[self.img_index];
			    self.load_image();						   
			}		    
		    self.img_index++;		       
		}
	    self.img_index--;
	};
    };


var edit_box = new function()
    {
	this.current_text = '';
	this.old_text = '';
	this.parent_element;
	this.locked = 0;
	this.id = 0;
	
	var self = this;

	this.init = function(p, t, id)
	{
	    self.parent_element = p;
	    self.current_text = t;
	    self.old_text = self.current_text;	    
	    self.id = id;	    
	};

	this.error = function(msg)
	{
	    alert(msg);
	    self.current_text = self.old_text;
	    self.leave();
	};

	this.enter = function()
	{	  	 
	    self.locked = 1;
	    self.parent_element.empty().append('<input type="text" id="option_'+self.id+'" value="'+self.current_text+'" />');			
	    var input = self.parent_element.find('input');
	    input.focus();
	    input.bind('blur', function(e){
		    var $this = $(this);
		    self.current_text = $this.attr('value');
		    
		    if(self.current_text == self.old_text)
			{
			    self.leave();
			}
		    else
			{
			    $.ajax({
				    url: window.location.toString(),
					timeout: 5000,
					type: 'POST',
					dataType: 'json',
					data: 'id='+self.id+'&value='+self.current_text,
					success: function(data, textStatus){			
					if(data == 1)
					    {
						self.error('server error');
					    }
					else if(data == 2)
					    {
						self.error('invalid option value');
					    }
					else
					    {
						self.leave();
					    }								    						    
				    },
					error: function(x, txt, e){
					self.error(txt);				
				    }
				});		    
			}
		});
	    
	};

	this.leave = function()
	{
	    self.locked = 0;
	    self.parent_element.empty().append('<span class="option" id="option_'+self.id+'">'+self.current_text+'</span>');			
	    var span = self.parent_element.find('span');	    
	    span.bind('click', function(e){
		    var $this = $(this);
		    if(self.locked == 0)
			{
			    self.init($this.parent(), $this.text(), $this.attr('id').split('_')[1]);
			    self.enter();
			}
		});
	};
    };

// end of singleton classes

// general functions
    
var radios = function()
{
    $(".radios input[@name='data_type']").change(function(){
	    if($('.radios input')[4].checked)
		{
		    $('#containers').removeClass('hidden');
		}
	    else
		{
		    $('#containers').addClass('hidden');
		}
	});		
};
    

var toggle_archive = function(link)
{
    var item = link.parent();
    var list = item.find('> ul');
    if(list.css('display') == 'none')
	{
	    list.show(200);
	    //item.css('list-style', 'circle');
	}
    else
	{	  
	    list.hide(200);      	  
	    //item.css('list-style', 'disc');
	}
};

var toggle = function(link)
{	    	   
    var item = link.parent();
    var img = item.find('> img');
    link.empty();	    
    var list = item.find('> ul');
    if(list.css('display') == 'none')
	{
	    list.removeClass();
	    //list.show(200);
	    link.append('-');
	    if(!/data/.test(img.attr('src')))
		{
		    img.attr('src', 'http://'+WEB_ROOT+PUBLIC_DIR+'/img/t_folder_open.gif');
		}
	}
    else
	{
	    list.addClass('hidden_sections');
	    //list.hide(200);
	    link.append('+');
	    if(!/data/.test(img.attr('src')))
		{						      
		    img.attr('src', 'http://'+WEB_ROOT+PUBLIC_DIR+'/img/t_folder_closed.gif');
		}
	}
};

	
var blog_images = function()
{
    $('div.entry p img').each(function(index){
	    var $image = $(this);
	    var id = $image.attr('id');
	    var image_id = 0;
	    if(/^blog_image/.test(id))
		{
		    image_id = id.split("_")[2];
		    //$image.attr({'src': $image.attr('src').replace(/mid_/, 'tn_')});
		    $image.wrap('<a href="http://'+WEB_ROOT+PUBLIC_DIR+'/news/view_image/'+image_id+'"></a>');  
		    $image.parent().parent().addClass('img');
		}
	});
};
    
var news = function()
{
    $('#right img').bind('mouseover', function(){
	    $this = $(this);
	    $this.animate({opacity:"0.7"}, 200);
	});
    $('#right img').bind('mouseout', function(){
	    $this = $(this);
	    $this.animate({opacity:"1"}, 200);
	    
	    
	});
};

var products = function()
{
    $('#products .button a').bind('mouseover', function(e){
	    var $link = $(this);
	    var container = $link.parent();
	    var span = container.find('span');
	    var text = span.text();
	    
	    //alert(text);
	    
	    /*
	      var id = $link.attr('id');	
	      var offset = (id.substring(3) * 312);		
	      var rider = $link.children('span');			
	      container.css('backgroundPosition', '0 -'+offset+'px');	       
	    */
	    
	    $('#products_wrapper h1').empty().append(text);
	});       
    /*
      $('#products .button a').bind('mouseout', function(e){
      $('#products_wrapper h1').empty().append('&nbsp;');
      });
      
    */      
};

var toggle_p = function(legend)
{	    	   
    var fieldset = legend.parent();
    var form = fieldset.find('> form');	   
    if(form.css('display') == 'none')
	{
	    form.show();		    
	}
    else
	{
	    form.hide();		    
	}
};

var properties = function()
{
    /*
      $('#properties fieldset legend').bind('click', function(e){
      e.preventDefault();
      var $this = $(this);
      toggle_p($this);
      });
    */
    
    $('#properties form span.option').bind('click', function(e){
	    var $this = $(this);
	    if(edit_box.locked == 0)
		{
		    edit_box.init($this.parent(), $this.text(), $this.attr('id').split('_')[1]);
		    edit_box.enter();
		}
	});
};

var tree = function()
{
    $('ul#tree li a.toggle, ul#tree ul li a.toggle').bind('click', function(e){
	    e.preventDefault();
	    var $this = $(this);
	    toggle($this);
	});
};



var archive = function()
{
    $('#archive a[href^="#"]').bind('click', function(e){
	    e.preventDefault();
	    var $this = $(this);
	    toggle_archive($this);
	});
};



var team = function()
{
    (new Image()).src = 'http://'+WEB_ROOT+PUBLIC_DIR+'/img/team3.jpg';

    $(window).load(function(){
	   		
	    $('#team a').bind('mouseover', function(e){
		    var $link = $(this);
		    var container = $link.parent();
		    var id = $link.attr('id');	
		    var offset = (id.substring(3) * 312);		
		    var rider = $link.children('span');
		    
		    //container.css('backgroundPosition', '0 -'+offset+'px');	       
		    //$('p#rider').empty().append(rider.text());
		});       
	    //$('#team').show(2000);
	    $('#team').fadeIn(1000);
	    
	    $('#team a').tooltip({ 
		    track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 
			});
	});	    
};

var video_embed = function(video_section)
{     
    if(video_section)
	{
	    $('a[href^="http://www.vimeo.com"]').flash(
            { width: 500, height: 400 },
            { version: 8 },
            function(htmlOptions){
	    $this = $(this);
	    var parent = $this.parent();
	    var url_arr = $this.attr('href').split("/");
	    var url_arr_size = url_arr.length;
	    htmlOptions.allowfullscreen = "true";
	    htmlOptions.allowscriptaccess = "always";    
	    htmlOptions.wmode = "transparent";					
	    htmlOptions.src = "http://vimeo.com/moogaloop.swf";
	    htmlOptions.src += "?clip_id="+url_arr[url_arr_size - 1]+"&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1";
	    parent.empty();
	    parent.append($.fn.flash.transform(htmlOptions));
	    });
	}
    else
	{
	    $('.entry a[href^="http://www.vimeo.com"]').flash(
            { width: 468, height: 300 },
            { version: 8 },
            function(htmlOptions){
	    $this = $(this);
	    var parent = $this.parent();
	    var url_arr = $this.attr('href').split("/");
	    var url_arr_size = url_arr.length;
	    htmlOptions.allowfullscreen = "true";
	    htmlOptions.allowscriptaccess = "always";    
	    htmlOptions.wmode = "transparent";					
	    htmlOptions.src = "http://vimeo.com/moogaloop.swf";
	    htmlOptions.src += "?clip_id="+url_arr[url_arr_size - 1]+"&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1";
	    parent.empty();
	    parent.append($.fn.flash.transform(htmlOptions));
	    });
	    
	    $('.entry a[href^="http://www.youtube.com"]').flash(
            { width: 468, height: 300 },
            { version: 8 },
            function(htmlOptions) {
		$this = $(this);
		var parent = $this.parent();
		htmlOptions.src = $this.attr('href');
		parent.empty();
		parent.append($.fn.flash.transform(htmlOptions));
	    });

	    
	    $('.entry a[href^="http://video.mpora.com"]').flash(
            { width: 468, height: 300 },
            { version: 8 },
            function(htmlOptions) {
		$this = $(this);
		var parent = $this.parent();
		htmlOptions.src = $this.attr('href');
		parent.empty();
		parent.append($.fn.flash.transform(htmlOptions));
	    });


	    $('.entry a[href^="http://www.facebook.com/v/"]').flash(
            { width: 468, height: 300 },
            { version: 8 },
            function(htmlOptions) {
		$this = $(this);
		var parent = $this.parent();
		htmlOptions.src = $this.attr('href');
		parent.empty();
		parent.append($.fn.flash.transform(htmlOptions));
	    });





	    /*
	    $('#video_container_news a[href^="http://www.vimeo.com"]').flash(
            { width: 425, height: 280 },
            { version: 8 },
            function(htmlOptions){
	    $this = $(this);
	    var parent = $this.parent();
	    var url_arr = $this.attr('href').split("/");
	    var url_arr_size = url_arr.length;
	    htmlOptions.allowfullscreen = "true";
	    htmlOptions.allowscriptaccess = "always";    
	    htmlOptions.wmode = "transparent";					
	    htmlOptions.src = "http://vimeo.com/moogaloop.swf";
	    htmlOptions.src += "?clip_id="+url_arr[url_arr_size - 1]+"&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1";
	    parent.empty();
	    parent.append($.fn.flash.transform(htmlOptions));
	    });
	    */	
	}	      
	    
    /*
    $('#video_container').flash(
    { width: 500, height: 400 },
    { version: 8 },
    function(htmlOptions){					
	$this = $(this);	
	
	var $file = $this.find('span').text();
	
	htmlOptions.src = "http://"+WEB_ROOT+PUBLIC_DIR+"/flvplayer.swf";
	htmlOptions.allowfullscreen = "true";
	htmlOptions.allowscriptaccess = "always";
	htmlOptions.wmode = "opaque";					
	htmlOptions.name = "playerID";
	htmlOptions.id = "playerID";					
	htmlOptions.flashvars = "&file=http://"+WEB_ROOT+PUBLIC_DIR+"/uploads/"+$file
	    +"&image=http://"+WEB_ROOT+PUBLIC_DIR+"/uploads/"+$file+".jpg"
	    +"&logo=http://"+WEB_ROOT+PUBLIC_DIR+"/img/prop_logo_tran_small.png"
	    +"&enablejs=true&javascriptid=playerID";
	$this.empty();
	$this.prepend($.fn.flash.transform(htmlOptions));
    });
    */	
};



// events

$(document).ready(function(){

	
	$('a.back').bind("click", function(e){
		e.preventDefault();
		history.go(-1);
	    });

	if($('map').length > 0)
	    {
		$('map *').tooltip({ 
			track: true, 
			    delay: 0, 
			    showURL: false, 
			    showBody: " - ", 
			    fade: 250 
			    });	    
	    }

	$('form.confirm').submit(function(){
		return confirm('Are you sure you want to do this?');
	    });
   

	if($('#blog_wrapper').length < 1 && ($('.entry').length > 0 || $('#ads').length > 0))
	    {
		enlarger.init();
		$(window).resize(function(){ enlarger.resize(); });

		blog_images();
	    }
	else if($('.radios').length > 0)
	    {
		radios();
	    }
	else if($('#rider_images').length > 0)
	    {
		$('#rider_images p img').css('cursor', 'pointer');
		$('#rider_images p img').bind("click", function(e){		
			image_rotate.do_rotate();
		    });
	    }	  
	/*
	else if($('#right').length > 0)
	    {
		news();
	    }
	*/
	else if($('form#paypal').length > 0)
	    {
		var f = $('form#paypal');
		f.submit();
	    }
	else if($('#products').length > 0)
	    {
		products();
	    }
	else if($('#properties').length > 0)
	    {
		properties();
	    }
	else if($('ul#tree').length > 0)
	    {
		tree();
	    }
	else if($('#team').length > 0)
	    {
		team();

	    }	

	if($('.entry').length > 0 || $('#video_container').length > 0)
	    {
		if($('.entry').length > 0)
		    {
			video_embed(0);
		    }
		else
		    {			
			video_embed(1);
		    }


		if($('#video_container').length > 0)
		    {			
			video_ui.init();
		    }
	    }	

	if($('#video_upload').length > 0)
	    {				    
		video_upload.init();	       
	    }

	if($('#help').length > 0)
            {
		help.init();
                $('#help_tab').bind("click", function(e){
                        e.preventDefault();
			help.toggle();
                    });
            }

	if($('#archive').length > 0)
            {
		archive();
	    }

    });
   




