/**
 * @author Ross Newton
 */
var imageLibrary;
jQuery(document).ready(function(){
 imageLibrary = {
    lib: {},
    init: function(){
    
    },
    preload: function(){
        var values = new Array();
        var keys = new Array();
        var images = new Array();
		var thumbs = new Array();
		
        jQuery.each(this.lib, function(index, value){
            values.push(value);
            keys.push(index);
			thumbs.push(value['thumb']);
            images.push(value['image']);
        })
		
		
	
	jQuery.preload(images, {
				threshold: 1,
            onComplete: function(data){
                var pictureBox = jQuery("<div></div>").attr({
                    id: keys[data.index]
                });
                var pictureCaption = jQuery("<span></span>").addClass("caption").append(values[data.index]['caption']);
                var pictureImage = new Image();
                pictureImage.src = data.image;
                jQuery(pictureBox).append(pictureImage);
                jQuery(pictureBox).append(pictureCaption);
                jQuery('#pictureFrame').append(pictureBox);
            },
            onFinish: function(){
                imageLibrary.loadDefault();
            }
        });



        jQuery.preload(thumbs, {
			threshold: 1,
			onComplete: function(data){
				
				//var thumbListItem = jQuery("<li></li>");
				var thumbImage = new Image();
				jQuery(thumbImage).attr({
                    id: keys[data.index] + '_thumb'
                }).addClass("thumb");
				thumbImage.src = data.image;
				jQuery(thumbImage).attr({alt: values[data.index]['caption'], title: values[data.index]['caption']});
				//jQuery(thumbListItem).append(thumbImage);
				jQuery('#projectListSmall #projectItem' + new String(keys[data.index]).substr(7, new String(keys[data.index]).length - 7)+" a").prepend(thumbImage);
				jQuery('#imageList #imageItem' + new String(keys[data.index]).substr(7, new String(keys[data.index]).length - 7)).append(thumbImage);
				jQuery("#officeList #officeItem" + new String(keys[data.index]).substr(7, new String(keys[data.index]).length - 7)+" div").prepend(thumbImage);
				jQuery("#projectList #projectItem" + new String(keys[data.index]).substr(7, new String(keys[data.index]).length - 7)+" a").prepend(thumbImage);
				jQuery("#projectList #projectItem" + new String(keys[data.index]).substr(7, new String(keys[data.index]).length - 7)+", #officeList #officeItem" + new String(keys[data.index]).substr(7, new String(keys[data.index]).length - 7)).hoverIntent({
					sensitivity: 1,
					interval: 10,
					timeout: 0,
					over: function(){
						jQuery(this).css({backgroundColor: 'rgba(255, 150, 15, 0.8)'});
						jQuery("#"+this.id +" img").trigger('mouseover');
					},
					out: function(){
						jQuery(this).css({backgroundColor: 'transparent'});
						jQuery("#"+this.id +" img").trigger('mouseout');
					}
				});
				
				jQuery('#' + thumbImage.id).fadeIn("slow", function(){
						jQuery(this).css({display: 'block'});
					});
			},
			onFinish: function(){
				
				jQuery('#sections').serialScroll({
					items: 'li',
					axis: 'x',
					prev: '#left',
					next: '#right',
					start: 0,
					duration: 500,
					exclude: 3,
					force: true,
					stop: true,
					lock: false,
					cycle: false, //don't pull back once you reach the end
					jump: false //click on the images to scroll to them
				});	
				imageLibrary.scrollBar();
				imageLibrary.thumbsHover();
				
			}
			
		});
    },
    largeAnimation: function(largeID){
        var durationVal = Math.abs(parseInt(jQuery("#pictureFrame #" + largeID).css('marginLeft')));
        jQuery("#pictureFrame #" + largeID).stop(true, false).animate({
            marginLeft: '0px'
        }, {
            queue: false,
            duration: durationVal,
            easing: 'easeOutCubic',
            complete: function(){
                var durationVal = Math.abs(parseInt(jQuery("#pictureFrame #" + largeID).children(".caption").css('left')));
                jQuery("#" + largeID).children(".caption").stop(true, false).animate({
                    left: "0px"
                }, {
                    queue: false,
                    duration: durationVal,
                    complete: function(){
                        if (jQuery("#logo").css('opacity') > 0) {
                            jQuery("#logo").stop().animate({
                                opacity: "1"
                            }, {
                                duration: 500
                            });
                        }
                    }
                });
            }
        });
    },
    thumbsHover: function(){
		jQuery("img[id^='picture'].thumb").bind('mouseover',function(e){
		jQuery(this).addClass("hoverOn");
                var largeID = new String(this.id).substr(0, new String(this.id).length - 6);
               // jQuery(this).css({
             //       borderColor: "#FFB900",
             //       outlineWidth: "2px"
             //   });
                jQuery("#pictureFrame div").css({
                    "zIndex": "2"
                });
                jQuery("#pictureFrame #picture0").css("zIndex", "1");
                jQuery("#" + largeID).css("zIndex", "10");
                imageLibrary.largeAnimation(largeID);
		});
		
		jQuery("img[id^='picture'].thumb").bind('mouseout',function(e){
		jQuery(this).removeClass("hoverOn");
                var largeID = new String(this.id).substr(0, new String(this.id).length - 6);
                var oldLarge = jQuery("#pictureFrame #" + largeID);
                var durationVal = parseInt(jQuery(oldLarge).children(".caption").css('left')) + 420;
              //  jQuery(this).css({
             //       borderColor: "#eee",
             //       outlineWidth: "0px"
             //   });
                jQuery(oldLarge).children(".caption").stop(true, false).animate({
                    left: "-420px"
                }, {
                    queue: false,
                    easing: 'easeOutCubic',
                    duration: durationVal,
                    complete: function(){
                        var durationVal = parseInt(jQuery(oldLarge).css('marginLeft')) + 900;
                        jQuery(oldLarge).stop(true, false).animate({
                            marginLeft: '-900px'
                        }, {
                            queue: false,
                            easing: 'easeOutCubic',
                            duration: durationVal,
                            complete: function(){
                                if ((imageLibrary.lib['picture0'] == undefined) && (jQuery("img[id^='picture'].thumb").hasClass("hoverOn") == false)) {
                                    jQuery("#logo").stop().animate({
                                        opacity: "1"
                                    }, {
                                        duration: 500
                                    });
                                }
                            }
                        });
                    }
                    
                });
		});
		
        jQuery("img[id^='picture'].thumb").hoverIntent({
            sensitivity: 1,
            interval: 20,
            timeout: 0,
            over: function(){
				jQuery(this).addClass("hoverOn");
                var largeID = new String(this.id).substr(0, new String(this.id).length - 6);
             //   jQuery(this).css({
              //      borderColor: "#FFB900",
             //       outlineWidth: "2px"
             //   });
                jQuery("#pictureFrame div").css({
                    "zIndex": "2"
                });
                jQuery("#pictureFrame #picture0").css("zIndex", "1");
                jQuery("#" + largeID).css("zIndex", "10");
                imageLibrary.largeAnimation(largeID);
                
            },
            out: function(){
				jQuery(this).removeClass("hoverOn");
                var largeID = new String(this.id).substr(0, new String(this.id).length - 6);
                var oldLarge = jQuery("#pictureFrame #" + largeID);
                var durationVal = parseInt(jQuery(oldLarge).children(".caption").css('left')) + 420;
              //  jQuery(this).css({
             //       borderColor: "#eee",
             //       outlineWidth: "0px"
            //    });
                jQuery(oldLarge).children(".caption").stop(true, false).animate({
                    left: "-420px"
                }, {
                    queue: false,
                    easing: 'easeOutCubic',
                    duration: durationVal,
                    complete: function(){
                        var durationVal = parseInt(jQuery(oldLarge).css('marginLeft')) + 900;
                        jQuery(oldLarge).stop(true, false).animate({
                            marginLeft: '-900px'
                        }, {
                            queue: false,
                            easing: 'easeOutCubic',
                            duration: durationVal,
                            complete: function(){
                                if ((imageLibrary.lib['picture0'] == undefined) && (jQuery("img[id^='picture'].thumb").hasClass("hoverOn") == false)) {
                                    jQuery("#logo").stop().animate({
                                        opacity: "1"
                                    }, {
                                        duration: 500
                                    });
                                }
                            }
                        });
                    }
                    
                });
            }
        });
    },
	scrollBar: function(){
	var billybob = {
		previous: 0,
		current: 0
	}
	
	jQuery("#left").click(function(){
		if (jQuery("#content-slider").slider('value') > 0) {
			
			jQuery("#content-slider").slider('value', jQuery("#content-slider").slider('value') - 1)
		}
	});
	jQuery("#right").click(function(){
		if (jQuery("#content-slider").slider('value') < jQuery("#content-slider").slider('option', 'max')) {
			jQuery("#content-slider").slider('value', jQuery("#content-slider").slider('value') + 1)
		}
	});
			function handleSliderSlide(e, ui){
				billybob['previous'] = billybob['current'];
				billybob['current'] = ui.value;
				if (billybob['current'] > billybob['previous']) {
					for (t = 0; t < (billybob['current'] - billybob['previous']); t++) {
						jQuery("#sections").trigger('next');
					}
				}else{
					for (t = 0; t < (billybob['previous'] - billybob['current']); t++) {
						jQuery("#sections").trigger('prev');
					}
				}
				jQuery("#shit").text(billybob['previous'] + ', ' + billybob['current']);
			}
			
			function countThumbs(){
					var x = 0;
					 	jQuery.each(imageLibrary.lib, function(index, value){
            			if (value['thumb'] != undefined){
							x++;
						}
						
      	  			})
					return x - 4;
				}
				
			jQuery("#content-slider").slider({
    			animate: true,
    			slide: handleSliderSlide,
				change: function(e, ui){
					if (e.originalEvent === undefined){
						billybob['previous'] = billybob['current'];
						billybob['current'] = ui.value;
					}
				},
				step: 1,
				value: 0,
				min: 0,
				max: jQuery("#sections img").length - 4
  			});
			
	},
	projectList: function(){
		var values = new Array();
        var keys = new Array();
        var images = new Array();
		var thumbs = new Array();
		
        jQuery.each(this.lib, function(index, value){
            values.push(value);
            keys.push(index);
			thumbs.push(value['thumb']);
            images.push(value['image']);
        })
		
		jQuery.preload(thumbs, {
			threshold: 1,
			onComplete: function(data){
				var thumbListItem = jQuery("<li></li>");
				var thumbImage = new Image();
				jQuery(thumbImage).attr({
					id: keys[data.index] + '_thumb'
				}).addClass("thumb");
				thumbImage.src = data.image;
				jQuery(thumbListItem).append(thumbImage);
				jQuery('#servicelist').append(thumbListItem);
				jQuery('#' + thumbImage.id).fadeIn("slow");
			}
		})
	},
    loadDefault: function(){
        this.largeAnimation("picture0");
    }
};




});