$(document).ready(hidei);
function hidei(){

	$isTouchScreen = 0;
	$portrait = 0;
	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
 		$isTouchScreen = 1;
	    updateOrientation();

		function updateOrientation(){
		    var contentType = "show_";
		    switch(window.orientation){
		        case 0:
			contentType += "portrait";
			$portrait = 1;
			break;
		
			case -90:
			contentType += "right";
			break;
		
			case 90:
			contentType += "left";
			break;
		
			case 180:
			contentType += "flipped";
			break;
		    }
		}

	/* 	$('#content').jScrollTouch(); */
	 }else{
 		$isTouchScreen = 0;
 				
 		//hide photos until loaded
		$("#loading_anim").show();
		
			 if ( $.browser.msie ) {
   				$(".photo").css("visibility","hidden");
	 		  } else {
    			$(".photo").hide();
	 		  }
		
		
		$("#footer").hide();

 	}
}

//---------------ON IMAGE LOAD---------------//

$(document).ready(init);
function init(){
	//************************************************************//
	//---------------------------SCREEN---------------------------//
	//************************************************************//
	

	//reset default width
	$width = 0;

	//---------------RESOLUTON---------------//
	scrHeight = $(window).height();
	scrWidth = $(window).width();
	

	if($portrait == 1){
		//alert(scrHeight);
		scrHeight = 960;
		$("#content").css("border-top", "40px solid black");
		$("#content").css("border-bottom", "40px solid black");
		//alert(scrWidth);
	}

	
	//alert("scrH " + scrHeight);
	//alert("scrW " + scrWidth);
	
	$scroll_visible = 0;
	$photoCount = 0;

  
  	//************************************************************//
	//---------------------------LAYOUT---------------------------//
	//************************************************************//
	
	//---------------DIV SIZING---------------//
	
		divH = scrHeight - 181; 
		
		//alert("divH: " + divH);
		
		$("#content").height(divH);
		$(".sliders").height(divH);
		$("#photos").height(divH - 30);
		$("#name_frame").height(divH - 100);
		$("#cover_text").height(divH - 100);
		$("#cover_text h2").css("margin-top", ((divH - 100) * 0.33));
	
	//---------------BLOG SIZING---------------//
	if($("#posts").length > 0){
		$postsW = scrWidth - 550;
		$postsW = Math.round($postsW);
		//alert($postsW);
		
		if($isTouchScreen == 1){
			$(".post").width(scrWidth);
			$("#footer").remove();
			$("#sidebar").hide();
			/* $("#pageNav a").css("font-size", "30px"); */
			$("#loading_anim").hide();
			$(".comment-meta a").hide();
		}else{
			$(".post").width($postsW);
		}
	}
	
}

//---------------PAGE READY---------------//

$(window).load(function() {


	//************************************************************//
	//---------------------------PHOTOS---------------------------//
	//************************************************************//
	
	//---------------IMAGE SCALE---------------//
	
	$('.photo').each(function() {
	
			
        //get original image size
            $width_original = (this).width;
            $width_original = Math.round($width_original);
            
            //alert("Original W: " + $width_original);
            
            //alert($width_original);
            $height_original = (this).height;
            $height_original = Math.round($height_original);
            //alert($height_original);
            
            //alert("Original H: " + $height_original);
            
            if($width_original < 1){
            	$(body).remove(this);
            }
            
            $photoCount += 1;
            
        //get image sizing ratio
            $ratio = $width_original / $height_original;
            
        //set photo height to fit
			photoH = scrHeight - 181;
			photoH = Math.round(photoH);
            
        //calculate width based on image format, screen size
            $width_photo = photoH * $ratio;
            $width_photo = Math.round($width_photo);
            			
		//set div height, with padding
			divH = photoH;
			$("#content").attr("max-height", divH);
			$("#content").height(divH);
			
		//if photo exceeds original size, stop there
			if (photoH > $height_original){
					photoH = $height_original;
			}
            
        //adjust image size to fit, in proper format
			$(this).height(photoH);
            $(this).width($width_photo);
            //alert($width_photo);
            //alert();
            
        //calculate div width to fit all photos, with padding, originally: 13. margin-right CSS: 5
            $widthP= $width_photo + 18;
            $width = $width + $widthP;
  });


	 //************************************************************//
	//---------------------------LAYOUT---------------------------//
	//************************************************************//
	
	//---------------DIV SIZING---------------//

	if($photoCount > 0){
			if($isTouchScreen == 1){
				$(".photo").show();
				$("#footer").show();
			}
	}
	
	$width = $width + $("#name_frame").width() + 2;
	$width = Math.round($width);
	$("#photos").width($width);
	
	if($("#about_me").length > 0){		
		$aboutH = $("#bio_text").height();
		$bioH = $("#cover_text").height();
		
		$aboutF = $("#bio_text p").css("font-size");
		
		$aboutFo = $aboutF.split("p");
		
		$aboutFont = $aboutFo[0];
		
		$aboutL = $("#bio_text p").css("line-height");
		$aboutL = $aboutL.split("p");
		$aboutLH = $aboutL[0];
		
		if($aboutH > ($bioH - 40)){
			$aboutFont = $aboutFont - 3;
			$aboutLH = $aboutLH - 6;
			$("#about_socmedia img").width("32px");
		}
		
		$aboutFont = $aboutFont + "px";
		$aboutLH = $aboutLH + "px";
	
		$("#bio_text p").css("font-size", $aboutFont);
		$("#bio_text p").css("line-height", $aboutLH);
	}
	
	//---------------LOADING ANIMATION---------------//

	$('#loading_anim').fadeTo(400,0);
	$('#loading_anim').hide();


		 if ( $.browser.msie ) {
   			$(".photo").css("visibility","visible");
		 } else {
    		$('.photo').fadeTo(1000,1);
		 }
	
	$("#footer").show();

	$currentPhoto = 0;
	//alert($currentPhoto);
	slide = "first";
	$("#slide_left a").css("cursor","default");
	

    arrayOfWidth = [];
    imgs = $('.slide');
    strImg = ".slide:eq(" + $currentPhoto + ")";
    thisImg = $(strImg);

    imgs.each(function(index, domElement){
        var $this = $(domElement);
        arrayOfWidth.push($this.width());
    });


	function getWidth(img){
	    $thisWidth = arrayOfWidth[$currentPhoto];
	   	//add margin
	    $thisWidth += 5;
	    if(slide=="right"){
			$leftMar = $("#photos").css("left");
			$leftMar = parseInt($leftMar);
			$leftMar -= $thisWidth;
		}else{
			$leftMar = $("#photos").css("left");
			$leftMar = parseInt($leftMar);
			$leftMar = $thisWidth + $leftMar;
		}
	}
	
	$(".sliders").mousemove(function(e){
			$cursX = e.pageX;
			$cursY = e.pageY;
			
			
			if($scroll_visible == 0){
				if($cursX < 80 & $currentPhoto > 0){
					$("#slide_left").fadeTo(400,1);
					$("#slide_left a").css("cursor", "pointer");
					$scroll_visible = 1;
				}else if($cursX > (scrWidth - 80) & ($currentPhoto < ($photoCount - 1))){
					$("#slide_right").fadeTo(400,1);
					$("#slide_right a").css("cursor", "pointer");
					$scroll_visible = 2;
				}
			}
			
			if($scroll_visible > 0){
				if($cursX > 80 & $scroll_visible == 1){
					$("#slide_left a").css("cursor","default");
					$("#slide_left").delay(300).fadeTo(300,0, "easeInSine");
					$scroll_visible = 0;
				}else if($cursX < (scrWidth - 80) & $scroll_visible == 2){
					$("#slide_right a").css("cursor","default");
					$("#slide_right").delay(300).fadeTo(300,0, "easeInSine");
					$scroll_visible = 0;
				}
			}
	});


slideLeft = function(){
		if(slide !="first"){
			slide = "left";
			$currentPhoto = $currentPhoto - 1;
			getWidth(thisImg, slide);
			$("#photos").animate({left: parseInt($leftMar)}, 400, "easeInSine");
		}
			if($scroll_visible == 1 & $currentPhoto == 0){
				$("#slide_left a").css("cursor","default");
				$("#slide_left").delay(300).fadeTo(300,0, "easeInSine");
					$scroll_visible = 0;
					slide="first";
					$("#slide_right").fadeTo(300,1, "easeInSine");
			}
	}
	


slideRight = function(){

			if(slide != "end"){
				slide = "right";
				getWidth(thisImg, slide);
							
				$currentPhoto = $currentPhoto + 1;
				$("#photos").animate({left: parseInt($leftMar)}, 400, "easeInSine");

				$currentDivW = $("#photos").width();
				
			}
		if($currentPhoto >= ($photoCount)){
			$("#slide_right a").css("cursor","default");
			$("#slide_right").delay(300).fadeTo(300,0, "easeInSine");
			$scroll_visible = 0;
			slide="end";
			$("#slide_left").fadeTo(300,1, "easeInSine");
		}
			
	}
	
	 
	$("#slide_right").click(function(){
		slideRight();			
	});
	
	$("#slide_left").click(function(){
		slideLeft();			
	});
	
	//reset to first photo on current category click
	$(".current").click(function(){
		$leftMar = 0;
		$currentPhoto = 0;
		slide = "first";
		$("#slide_left").fadeTo(300,0, "easeInSine");
		$("#slide_right").fadeTo(1200,1, "easeInSine");
		$("#photos").animate({left: parseInt($leftMar)}, 1200, "easeInSine");
		return false;
	});

    $leftCol = "";
    $leftCo = 0;


    });
    
    //return to top function
    $("#return").click(function(){
    $top = 0;
    $("#posts").animate({top: parseInt($top)}, 1200, "easeInSine");
    });
    
    $(window).mouseout(function (event) {
        if ($('#photos').data('down')) {
            try {
                if (event.originalTarget.nodeName == 'BODY' || event.originalTarget.nodeName == 'HTML') {
                    $('#photos').data('down', false);
                }                
            } catch (e) {}
        }
    });
    
$(document).keydown(function(e){
    if (e.keyCode == 37) { 
		slideLeft();
    }
    if (e.keyCode == 39) { 
		slideRight();
    }
});



