

// JavaScript Document
var webRoot = '/limner/'
var titleFlag = false;
var searchObj;

loadImages();

function loadImages(){
	var imgArr = new Array('color_man2.png', 'background_texture.png', 'down_arrow.png', 'nav_arrow.png', 'nav_minus.png', 'nav_plus.png', 'up_arrow.png');
	var imageLoader = new Image();
	for(n in imgArr){
		imageLoader.src = webRoot+'images/'+imgArr[n];
	}
	imageLoader.src='';
	
}


$(function(){
	/* Password textfield */
	// cache references to the input elements into variables
    var passwordField = $('#quick_password');
    var emailField = $('#quick_user_login');
    // get the default value for the email address field
    var emailFieldDefault = emailField.val();
 
    // add a password placeholder field to the html
    passwordField.after('<input id="passwordPlaceholder" type="text" value="Password" autocomplete="off" />');
    var passwordPlaceholder = $('#passwordPlaceholder');
 
    // show the placeholder with the prompt text and hide the actual password field
    passwordPlaceholder.show();
    passwordField.hide();

    // when focus is placed on the placeholder hide the placeholder and show the actual password field
    passwordPlaceholder.focus(function() {
        passwordPlaceholder.hide();
        passwordField.show();
        passwordField.focus();
    });
    // and vice versa: hide the actual password field if no password has yet been entered
    
	passwordField
		.blur(function() {
			if(passwordField.val() == '') {
				passwordPlaceholder.show();
				passwordField.hide();
			}
		});
 
    // when focus goes to and moves away from the email field, reset it to blank or restore the default depending if a value is entered
    emailField.focus(function() {
        if(emailField.val() == emailFieldDefault) {
            emailField.val('');
        }
    });
    emailField.blur(function() {
        if(emailField.val() == '') {
            emailField.val(emailFieldDefault);
        }
    });
	
	/*-- HEIGHT --*/
	var pageH = (navigator.appName == "Microsoft Internet Explorer") ? document.body.offsetHeight: window.innerHeight;
	//var pageH = $(window).height();
	var topH = 137;
	
	$("#Shell").height(pageH-28);
	$("#GlobalContent")
		.height($("#Shell").height()-topH - 28)
	
	$(window).resize(function(){
		$('#GlobalContent').jScrollPane({
			showArrows:true, 
			scrollbarWidth: 14, 
			scrollbarMargin: 0, 
			reinitialiseOnImageLoad: true 
		});
		var pageH = $(window).height();
		$("#Shell").height(pageH-28);		
		$("#GlobalContent")
			.height($("#Shell").height()-topH - 28)
			.parent()
				.height($("#Shell").height()-topH - 28);
	});
	
	/*-- Title Bar --*/
	if(titleFlag){
		var $TitleBar = $("#TitleBar");
		$TitleBar.css({"width":"928px","opacity":"1"});
		$("#LoginForm").show();
	}else{
		var $TitleBar = $("#TitleBar");
		$TitleBar.css({"width":"0","opacity":"0"});
		$TitleBar.animate({"width": "928px", "opacity":"1"}, 750);
		$("#LoginForm").delay(750).fadeIn(500);
	}
	/*-- LEFT NAV --*/
	
		/*ACCORDIAN*/
		var $navSectionTitle = $('.nav-section-title');
		var $navSectionState = $('.section-state');
		var $navSection = $('.nav-section');
		
		$navSectionTitle.each(function(index){
			$(this).click(function(){
				//console.log('slide');
				$navSection.slideUp();
				$navSection.eq(index).slideDown();
				$navSectionState.removeClass('active');
				$navSectionState.eq(index).addClass('active');
			});
		});
		
	/*-- SECTION STATE  --*/
	var $path = $.address.pathNames();
	var $section = ($path[0]) ? $path[0] : '';
	var documentTitle = document.title;
	var current_request;
	function getContent(section){
		var sPath = window.location.pathname;
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
		
		if(current_request) current_request.abort();
		current_request = $.getJSON('content.php?page='+sPage+'&section='+section, function(data) {
				$("#Title").html(data['title']);
				$("#Subtitle").html(data['subtitle']);
				$("#GlobalContent").html(data['content']);	
				if(sPage == 'gallery.php') $("#gallery-search-results").html(''); 
				//Set Page title
				$.address.title(documentTitle+" | "+data['title']);
				$('#GlobalContent').jScrollPane({
					showArrows:true, 
					scrollbarWidth: 14, 
					scrollbarMargin: 0, 
					reinitialiseOnImageLoad: true 
				});
				$('#GlobalContent')[0].scrollTo(0);
				$('form.validate').ajaxForm({
					beforeSubmit:	function() {
						return $('form.validate').validate().form();
					},
					target: 		'form.validate'
				});
				var validator = $('form.validate').validate({meta: "validate"});

			}
		);
	}
	$.address.change(function(event) {
		
		$path = $.address.pathNames();
		
		/*if(location.pathname.indexOf('forum') > -1){
			$section = 'categories';
		}else if(location.pathname.indexOf('blog')>-1){
			$section = 'view_the_blog';
		}else if(location.pathname.indexOf('gallery')>-1){
			$section = 'gallery';
		}else{*/
			$section= $path[0];
			getContent($section);
		//}
		
		//console.log($path.length);
		if($path.length > 0 || $section != ''){
			$('.page-state').addClass('innactive');
			//console.log($section);
			$('#'+$section).next().removeClass('innactive');		
			$("#GlobalContent").children('div').attr('id',$section+'Wrapper');
			
			$(".page-state").each(function(){
				if(!$(this).hasClass('innactive')){
					$("#Subtitle").html($(this).prev().find('a').attr('title'));
				}
			});
		}
	});
	
	$("table").delegate("td", "hover", function(){
		$(this).toggleClass("hover");
	});

	
	/*-- PEOPLE ROLLOVERS --*/
	photoPreload = function(params){
		var $target = $('.entry .photos.'+params.target);
		var $loading = $('.loading', $target);
		$('<img />') //Image one
			.attr('src', params.image1)
			.addClass('image1')
			.load(function(){
				var $photo1 = $(this);
				$('<img />') //Image two
					.attr('src', params.image2)
					.addClass('image2')
					.load(function(){
						$('.photo1', $target).append( $photo1 );
						$('.photo2', $target).append( $(this) );
						$loading.addClass('hideAll');
						$target.removeClass('hideAll');
					});
			});		
	}
	$(".entry .photos")
		.live("mouseover",function(){
			$(this).addClass('hideTint');					   
		})
		.live("mouseout",function(){
			$(this).removeClass('hideTint');
		});
	
		
	/*-- GALLERY HOVERS - CLICKS --*/
	var position;
	$('.gallery ul li.galleryThumb img')
		.live('mouseover', function(e){
			$thumbHolder = $(e.target).parent().parent();
			$infoContainer = $('.infoContainer', $thumbHolder);
			position = $thumbHolder.position();
			$thumbHolder.css('z-index', '10');
			if( position.left > $('#PhotoGallery').width()/2){
				$infoContainer.css({'left':'-208px','text-align':'right'});	
			} else {	
				$infoContainer.css({'left':'','text-align':'left'});	
			}
			$infoContainer.stop(true, true).fadeIn(250);
		})
		.live('mouseout', function(e){
			$thumbHolder = $(e.target).parent().parent();
			$infoContainer = $('.infoContainer', $thumbHolder);
			$thumbHolder.css('z-index', '');
			$infoContainer
				.stop(true, true)
				.hide();
		});
	
	var current_request;
	
	$('.gallery ul li.galleryThumb a').live('click', function(e){
		e.preventDefault();
		var url = this.href;
		
		//ADD AJAX HERE, GET JSON
		if(current_request) current_request.abort();
		current_request = $.getJSON(url,
			function(data){
				PhotoGallery(data, 0);	  
			}
		);	  
	});
	
	$('#submit-search').click(function(){
		$("#gallery-search-results").html('<div class="search-loader"><img src="images/loader.gif" alt="" /></div>')
		if(current_request) current_request.abort();
		var query = $('#search-text').val();
		current_request = $.getJSON('gallerySearch.php?section='+$section+'&q='+query,
			function(data){
				searchObj = data;
				$("#gallery-search-results")
					.html(searchObj.results)
					.jScrollPane({
						showArrows:true, 
						scrollbarWidth: 14, 
						scrollbarMargin: 0, 
						reinitialiseOnImageLoad: true 
					});
			}
		);	
	})
	
});

function Carousel(obj){

    this.carousel = obj.carousel;
    this.dataObj = obj.dataObj
    
    this.$overlay = $("#overlay");
    this.$album = $("#album");
    this.$close = $("#close-album");
    this.$mainImage = $("#main-image");
	this.$next = $("#right");
	this.$prev = $("#left");
	this.$thumbs = $("#thumbs");
	this.$strip = $("#thumbs div");
	this.$images = $("#thumbs img");
	this.$title = $("#title");
	this.$descr = $("#descr");
	
	this.tWidth = this.$thumbs.width()
	this.length = this.$images.length;
    this.index = 0;
    
    this.cH = $(window).height();
    this.cW = $(window).width();
    this.aH = this.$album.height();
    this.aW = this.$album.width();
    this.aL = Math.abs(this.cW-this.aW)/2;
    this.aT = Math.abs(this.cH-this.aH)/2;
    
    this.spacing = this.$images.width()+parseInt(this.$images.css('margin-right'))*2;    
    this.offset = this.$images.eq(this.index)[0].offsetLeft
    
    this.left = ((this.tWidth - this.spacing)/2) - this.offset;
    
    this.$strip.css({left : this.left});
    this.$album.css({left : this.aL, top : this.aT });
    
    /*-- ASIGN INDEX --*/
    this.$images.each(function(n){
        this.index = n;
    });
    
    Carousel.prototype.goTo = function(e){
        
        /*-- SET INDEX --*/
        if($(e.target).hasClass('next')){this.index++}
        if($(e.target).hasClass('previous')){this.index--}
        
        this.index = (e.target.index != undefined) ? e.target.index : this.index;
        
        /*-- STATES --*/
        switch(this.index)
        {
            case 0||-1:
                this.index =0;
            break;
            
            case this.carL:
                this.index = this.carL-1;
            break;
            
            default:
                this.$next.removeClass('disabled');
            break;
        }
        
        if(this.index == 0){
            this.$prev.addClass('disabled');
        }
        if(this.index == this.carL-1){
            this.$next.addClass('disabled');
        }
        this.$images.removeClass('selected');
        this.$images.eq(this.index).addClass('selected');
        	
        /*-- MOVE --*/
        this.offset = this.$images.eq(this.index)[0].offsetLeft
        
        this.left = ((this.tWidth - this.spacing)/2) - this.offset;
        
        this.$strip.animate({left:this.left});
        

        /*-- SWAP IMAGE --*/
        var newImage = new Image();
        newImage.src = this.dataObj[this.index].src;
        this.$mainImage.html(newImage);
        
        /*-- SWAP TITLE --*/
        this.$title.html(this.dataObj[this.index].title);
        
        /*-- SWAP DESCRP--*/
        this.$descr.html(this.dataObj[this.index].descr);
        
    }
    
    Carousel.prototype.close = function(){
    	this.$album.fadeOut().remove();
    	this.$overlay.fadeOut().remove();
    }
    
    /*-- CLICKING --*/
    this.$next.click(new Function("e", this.carousel+".goTo(e);" ));
    this.$prev.click(new Function("e",this.carousel+".goTo(e);"));
    this.$images.click(new Function("e", this.carousel+".goTo(e);" ));
    
    this.$close.click(new Function(this.carousel+".close()"));
    
}













