$j(document).ready(function(){
/*
    if(document.location.hash == ''){
        document.location.href=document.location.href + "#";
    }
 */
	$j('#keys_info').animate({'opacity':0} ,"fast");
    imagesArr = new Array();
    captionArr = new Array();
	var l = 0;
	$j(document).keydown(function (e) { 
		//console.log(e);
		if(e.keyCode == 108 || e.keyCode == 76){
			if( l==0 ){
				simLightroom(0);
				l=1;
			} else if(l == 1){
				simLightroom(1);
				l=2;
			} else if(l == 2){
				simLightroom(2);
				l=0;
			}
		}
		
		if(e.keyCode == 39){
			//right
			moveToNextImage();
		}
		if(e.keyCode == 37){
			//left
			moveToPrevImage();
		}
	});
    
	$j( window ).resize(
		function(){
			positionLRImage();
		}
	);
	if(imageJSON != undefined){
        $j.each(imageJSON, function(){
            imagesArr.push('/pictures/' + this.image_path);
	        captionArr.push(this.image_caption);
        });
    }
    $j('#captionText').html(captionArr[0]);
    if(captionArr[0] != undefined && captionArr[0] != ''){
	$j('#caption').css('padding-top','10px');   
    }
    
    
    $j('#arrLeftImg').hide();
    $j('#arrRightImg').hide();

 $j('#arrLeft').mouseenter(
    function(){
        $j('#arrLeft').css('background-image','url(/images/arr-left.jpg)');
        $j('#arrLeft').css('background-repeat','no-repeat');
        //$j('#arrLeftImg').show();
    }
    );

$j('#arrLeft').mouseleave(
    function(){
        $j('#arrLeft').css('background-image','url(/images/tg.gif)');
        $j('#arrLeft').css('background-repeat','repeat');
        //$j('#arrLeftImg').hide();
    }
    );

$j('#arrRight').mouseenter(
    function(){
        //$j('#arrRightImg').show();
        $j('#arrRight').css('background-image','url(/images/arr-right.jpg)');
        $j('#arrRight').css('background-repeat','no-repeat');
    }
    );

$j('#arrRight').mouseleave(
    function(){
        //$j('#arrRightImg').hide();
        $j('#arrRight').css('background-image','url(/images/tg.gif)');
        $j('#arrRight').css('background-repeat','repeat');
    }
    );
var pos = 0;

$j('#totalImages').html(imagesArr.length);
writeImagePosition(pos);

$j('#arrRight').click(
	function(){
        moveToNextImage();
	}
);

$j('#arrLeft').click(
function(){
        moveToPrevImage();
    }
);

var first = true;

$j('#image2').load(function(){
    $j('#image2').css('top', $j('#mainImage').offset().top);
    $j('#image2').css('left', $j('#mainImage').offset().left);
    $j('#lightroom').css('height', $j(document).height());
    $j('#image2').animate({'opacity':1} ,"fast");
});

$j('#mainImage').load(function(){
	if(!first){ 
        	$j('#mainImage').animate({'opacity':1} ,"normal");
		setHashLinks(imagesArr.length,pos);
		$j('#captionText').animate({'opacity':0} ,250, function(){
			$j('#captionText').html(captionArr[pos]);
			if(captionArr[pos] == '' || captionArr[pos] == undefined){
				$j('#caption').css('padding-top','0px');   
			} else {
				$j('#caption').css('padding-top','10px');   
			}
			$j('#captionText').animate({'opacity':1} ,250);
		});
	}
    positionLRImage();
    document.location.href= "#" + pos;
});

setHashLinks(imagesArr.length,pos);
checkHistoryUrl(imagesArr);

function moveToNextImage(){
	
	pos++;
	if(pos == imagesArr.length){
	 pos = 0;
	}
	changeImage(imagesArr[pos]);
	//setHashLinks(imagesArr.length,pos);
	first = false;
	writeImagePosition(pos);

}

function moveToPrevImage(){
	pos--;
        if(pos == -1){
         pos = imagesArr.length-1;
        }
        changeImage(imagesArr[pos]);
        //setHashLinks(imagesArr.length,pos);
	first = false;
    writeImagePosition(pos);
}

function checkHistoryUrl(imagesArr){
    s = document.location.hash.substr(1);
    
    if( s ){
        changeImage(imagesArr[s]);
       $j('#mainImage').animate({'opacity':1} ,"normal");
    } else {
        changeImage(imagesArr[0]);
        $j('#mainImage').animate( {'opacity':1}, "normal" );
    }

    pos = s*1;
    setHashLinks(imagesArr.length,pos);
}

var fxDone = true;
        
    $j('.keys_img').mouseover(function(){
        if(fxDone){
            fxDone = false;
            $j('#keys_info').animate({'opacity':1} ,"fast", function(){
                fxDone = true;
            });
        }
    });

    $j('.keys_img').mouseout(function(){
        if(fxDone){
            fxDone = false;
            $j('#keys_info').animate({'opacity':0} ,"fast", function(){
                fxDone = true;
            });
        }
    });

});

function writeImagePosition(pos){
    
    $j('#currentImage').html(pos+1);
}

function positionLRImage(){
    $j('#image2').animate({'opacity':0} ,"fast", function(){
        $j('#image2').attr('src', $j('#mainImage').attr('src'));

    });

}

function calculateNavPositions(l,pos){
    var next, prev;
    if(pos == 0){
        prev = l - 1;
        next = 1;
    } else if( pos == (l - 1) ){
        next = 0;
        prev = pos-1;
    } else {
        prev = pos - 1;
        next = pos + 1;
    }
    var a = new Array();
    a.push(prev);
    a.push(next);
    return a;
}

function setHashLinks(l,pos){
   
    var navPos = calculateNavPositions(l,pos);
    
    $j('#arrLeft').attr('href', "#"+ navPos[0]);
    $j('#arrRight').attr('href', "#"+ navPos[1]);
}


function changeImage(src){
    $j('#mainImage').animate({'opacity':0} ,"normal", function(){
        $j(this).attr('src', src);
		//$j('#image2').attr('src', src);
	});
    
}

function simLightroom(l){
	
	if( l==0 ){
		$j('#image2').css('display', 'block');	
		$j('#lightroom').css('top', 0);	
		$j('#lightroom').animate({
			opacity:0.8
		}, 300);	
	} else if(l == 1){
		$j('#image2').css('display', 'block');	
		$j('#image2').css('border-color', '#fff');	
		$j('#lightroom').css('top', 0);
		$j('#lightroom').animate({
			opacity:1
		}, 300);
	} else if(l == 2){
		$j('#lightroom').animate({
			opacity:0
		}, 500, function(){
			$j('#lightroom').css('top', -10000);
			$j('#image2').css('display', 'none');
		});
			
	}
	$j('#lightroom').css('height', $j(document).height());
}
