/* Declare a namespace for the site */
var Site = window.Site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function ($) {

    //same as $(document).ready();
    $(function () {

    });


    $(window).bind('load', function () {

    });

})(jQuery);

//Center Body Content
function CenterItem(theItem) {
    var winWidth = $(window).width();
    var winHeight = $(window).height();
    var windowCenter = winWidth / 2;
    var itemCenter = $(theItem).width() / 2;
    var theCenter = windowCenter - itemCenter;
    var windowMiddle = winHeight / 2;
    var itemMiddle = $(theItem).height() / 2;
    var theMiddle = windowMiddle - itemMiddle;
    if (winWidth > $(theItem).width()) { //horizontal
        $(theItem).css('left', theCenter);
    } else {
        $(theItem).css('left', '0');
    }
    if (winHeight > $(theItem).height()) { //vertical
        $(theItem).css('top', theMiddle);
    } else {
        $(theItem).css('top', '0');
    }
}
$(document).ready(function () {
    CenterItem('.centered');
});
$(window).resize(function () {
    CenterItem('.centered');
});

//Deep Linking
$('document').ready(function () {
    // Event handlers
    $.address.init(function (event) {
        // console.log('init: ' + $('[rel=address:' + event.value + ']').attr('href'));
    }).change(function (event) {
        //contentOut(event.value.replace('/', ''));
        contentOut();
		previewClose();
        if (event.value.replace('/', '') == '') {
            sec = 'about'; // First section to load	
        } else {
            sec = (event.value.replace('/', ''));
        }
    })
});


//Fade Current Section Out
function contentOut() {
	//var totals = 2; //total number of thumbs
	$preloader.fadeIn("fast"); //show preloader
	/*$('#dh_body').animate({
		opacity: '0.0'
	}, {
		duration: 500,
		specialEasing: {
			opacity: 'easeOutExpo'
		},
		complete: function () {
			loadContent(sec);
		}
	});*/
	$('#dh_body').fadeOut(300, function(){
		loadContent(sec);
	});
}

//Load New Section
function loadContent(section) {
	totals = 2; //total number of thumbs
	$preloader.fadeOut("fast"); //hide preloader
	if (section == 'about') {
		$('#dh_body').load('about.php', function () {
			/*$('#dh_body').animate({
				opacity: '1.0'
			}, {
				duration: 600,
				specialEasing: {
					opacity: 'easeInOutExpo'
				}
			});*/
			$('#dh_body').fadeIn(300);
		});
		$('#dh_toparrow').animate({
			top: '-80px'
		}, {
			duration: 500,
			specialEasing: {
				top: 'easeInOutExpo'
			}
		});
		$('#dh_bottomarrow').animate({
			bottom: '-80px'
		}, {
			duration: 500,
			specialEasing: {
				bottom: 'easeInOutExpo'
			}
		});
	}
	if (section == 'work') {
		$('#dh_body').load('works.php', function () {
			
			//var totalSlides=$(".content").size() //total number of rows
			$('.content:gt(' + (totals - 1) + ')').css('opacity', '0.0') //hide other rows	
			$('.content:gt(' + (totals - 1) + ')').css('visibility', 'hidden') //hide other rows
			worx();
			bgload();
			peepsit();
			/*$('#dh_body').animate({
				opacity: '1.0'
			}, {
				duration: 500,
				specialEasing: {
					opacity: 'easeInOutExpo'
				}
			});*/
			$('#dh_body').fadeIn(300);
		});
		$('#dh_toparrow').animate({
			top: '0px'
		}, {
			duration: 500,
			specialEasing: {
				top: 'easeInOutExpo'
			}
		});
		$('#dh_bottomarrow').animate({
			bottom: '0px'
		}, {
			duration: 500,
			specialEasing: {
				bottom: 'easeInOutExpo'
			}
		});
	}
	if (section == 'contact') {
		$('#dh_body').load('contact.php', function () {
			formfocus();
			contactalert();
			/*$('#dh_body').animate({
				opacity: '1.0'
			}, {
				duration: 600,
				specialEasing: {
					opacity: 'easeInOutExpo'
				}
			});*/
			$('#dh_body').fadeIn(300);
		});
		$('#dh_toparrow').animate({
			top: '-80px'
		}, {
			duration: 500,
			specialEasing: {
				top: 'easeInOutExpo'
			}
		});
		$('#dh_bottomarrow').animate({
			bottom: '-80px'
		}, {
			duration: 500,
			specialEasing: {
				bottom: 'easeInOutExpo'
			}
		});
	}
}


//Work Section
function worx() {
    $('#dh_bottomarrow, #dh_toparrow').mouseover(function () {
        $(this).css('background-position', '0 -80px');
		$(this).css('cursor', 'pointer');
    });
    $('#dh_bottomarrow, #dh_toparrow').mouseout(function () {
        $(this).css('background-position', '0 0');
		$(this).css('cursor', 'pointer');
    });
    $('#dh_bottomarrow').click(function () {
        if (totals == totalSlides) {

        } else {
            Slidebox('next');
        }
    });
    $('#dh_toparrow').click(function () {
        if (totals == '2') {

        } else {
            Slidebox('previous');
        }
    });

    // Work Hover
    $('.dh_work, .dh_work-last').mouseover(function () {
        $(this).find('.thumb').stop().animate({
            top: "-240px"
        }, {
            duration: 300,
            specialEasing: {
                top: 'easeOutExpo'
            }
        })
    }).mouseout(function () {
        $(this).find('.thumb').stop().animate({
            top: "0px"
        }, {
            duration: 400,
            specialEasing: {
                top: 'easeInOutExpo'
            },
            complete: function () {
                $(this).css({
                    top: "0"
                })
            }
        })
    })


    ///Preview Close
    $('#preview_window').click(function () {
        previewClose();
    });
};

function previewClose(){
	$imagePan.unbind("mousemove");
	$('#preview_window').removeClass('active');
	$imagePan_contain.stop().animate({
		top: '0'
	}, 300, 'easeInOutExpo');
	$('#preview_window').animate({
		height: '0px'
	}, {
		duration: 300,
		specialEasing: {
			height: 'easeInOutExpo'
		},
		complete: function () {
			$('#preview_window').css("opacity", "0.0");
		}
	})
	$('#dh_body').animate({
		opacity: "1.0"
	}, {
		duration: 300,
		specialEasing: {
			opacity: 'easeInOutExpo'
		}
	})	
}

var totals = 2; //total number of thumbs
//var totalSlides=$('.content').size() //total number of rows

function Slidebox(slideTo) {
	//console.log("b " + totals);
    var animSpeed = 600; //animation speed
    var easeType = 'easeInOutExpo'; //easing type	
    if (!$('.content').is(':animated')) {
        if (slideTo == 'next') { //next
            totals = totals + 1;
            //console.log(totalSlides);
            //$.address.value('work/' + workNum);
            $('.content:eq(' + (totals - 3) + ')').animate({
                top: '-=' + 245,
                opacity: '0.0'
            }, 500, easeType, function () {
                $('.content:eq(' + (totals - 3) + ')').css('visibility', 'hidden');
            }); //next
            $('.content:eq(' + (totals - 2) + ')').animate({
                top: '-=' + 245
            }, 600, easeType); //next
            $('.content:eq(' + (totals - 1) + ')').css('visibility', 'visible');

            $('.content:eq(' + (totals - 1) + ')').animate({
                top: '-=' + 245,
                opacity: '1.0'
            }, 700, easeType); //next
            $('.content:gt(' + (totals - 1) + ')').animate({
                top: '-=' + 245
            }, animSpeed, easeType); //next	
        } else if (slideTo == 'previous') { //previous
            totals = totals - 1;
            //console.log(totals);
            //$.address.value('work/' + workNum);
            $('.content:eq(' + (totals - 2) + ')').css('visibility', 'visible');
            $('.content:eq(' + (totals - 2) + ')').animate({
                top: '+=' + 245,
                opacity: '1.0'
            }, 700, easeType); //previous
            $('.content:eq(' + (totals - 1) + ')').animate({
                top: '+=' + 245
            }, 600, easeType); //previous
            $('.content:eq(' + (totals) + ')').animate({
                top: '+=' + 245,
                opacity: '0.0'
            }, 500, easeType, function () {
                $('.content:eq(' + (totals) + ')').css('visibility', 'hidden');
            }); //previous
            $('.content:gt(' + (totals) + ')').animate({
                top: '+=' + 245
            }, animSpeed, easeType); //previous
        }
    }
}

//Mousewheel
jQuery(function ($) {
    $(window).bind('mousewheel', function (event, delta) {
        if ($('.container').hasClass('active')) {} else {
            var dir = delta > 0 ? 'Up' : 'Down',
                vel = Math.abs(delta);
            //console.log(dir + ' at a velocity of ' + vel);
            if (dir == 'Down') {
                if (totals == totalSlides) {

                } else {
                    Slidebox('next');
                }
            }
            if (dir == 'Up') {
                if (totals == '2') {

                } else {
                    Slidebox('previous');
                }
            }
            return false;
        }
    });

})

//Keydown Functions
$(window).keydown(function (e) {
    if ($('.container').hasClass('active')) {} else {
        if (e.which == 40) {
            if (totals == totalSlides) {

            } else {
                Slidebox('next');
            }
            return false;
        } else if (e.which == 38) {
            if (totals == '2') {

            } else {
                Slidebox('previous');
            }
            return false;
        }
    }
});


//Big Preview Vars
$('document').ready(function () {
    $preview_window = $("#preview_window");
    $imagePan_panning = $("#imagePan .panning");
    $imagePan = $("#imagePan");
    $imagePan_contain = $("#imagePan .contain");
    $preloader = $("#preloader");
	$shade = $("#shade");
    $bgimg = $("#bgimg");

});

//Big Preview
function peepsit() {
    containerHeight = $imagePan.height();
    totalContentH = $imagePan_panning.height();
    $imagePan_contain.css("height", totalContentH);

    function MouseMove(e) {
        var mouseCoordsY = (e.pageY - $imagePan.offset().top);
        var mousePercentY = mouseCoordsY / containerHeight;
        var destY = -(((totalContentH - (containerHeight)) - containerHeight) * (mousePercentY));
        var thePosC = mouseCoordsY - destY;
        var thePosD = destY - mouseCoordsY;
        var marginT = $imagePan_panning.css("marginTop").replace("px", "");
        var animSpeed = 500; //ease amount
        var easeType = "easeOutCirc";
        if (mouseCoordsY > destY) {
            $imagePan_contain.stop().animate({
                top: -thePosC - marginT
            }, animSpeed, easeType); //with easing
        } else if (mouseCoordsY < destY) {
            $imagePan_contain.stop().animate({
                top: thePosD - marginT
            }, animSpeed, easeType); //with easing
        } else {
            $imagePan_contain.stop();
        }
    }

    $imagePan.bind("mousemove", function (event) {
        MouseMove(event);
    });

};

$(window).resize(function () {
    $imagePan.unbind("mousemove");
    peepsit();
});

function bgload() {

    //load 1st image
    var the1stImg = new Image();
    the1stImg.onload = CreateDelegate(the1stImg, theNewImg_onload);
    the1stImg.src = $bgimg.attr("src");

    //BackgroundLoad	

    function BackgroundLoad($this, imageWidth, imageHeight) {
        $preloader.fadeOut("fast"); //hide preloader
        //FullScreenBackground($this, imageWidth, imageHeight); //scale background image
        $this.show(); //display background image
        if ($('#preview_window').hasClass('active')) {
            $('#preview_window').css("opacity", "1");
            $('#preview_window').animate({
                height: "100%"
            }, {
                duration: 900,
                specialEasing: {
                    height: 'easeInOutExpo'
                },
                complete: function () {
                    peepsit();
                }
            })

            $('#dh_body').animate({
                opacity: "0.0"
            }, {
                duration: 600,
                specialEasing: {
                    opacity: 'easeInOutExpo'
                }
            })
        } else {

        }
    }

    //Clicking on thumbnail changes the background image
    $(".dh_work a, .dh_work-last a").click(function (event) {
        /*$shade.css('visibility','visible');
		$shade.animate({
                opacity: "0.8"
            }, {
                duration: 500,
                specialEasing: {
                    opacity: 'easeInOutExpo'
                },
                complete: function () {
                    
                }
            })*/
		$imagePan.unbind("mousemove");
        event.preventDefault();
        $preloader.fadeIn("fast"); //show preloader
        var $this = $(this);
		
        //get new image dimensions
        var theNewImg = new Image();
        theNewImg.onload = CreateDelegate(theNewImg, theNewImg_onload);
        theNewImg.src = this;
        $bgimg.attr("src", "").attr("src", this); //change image source
        $('#preview_window').addClass('active');

    });

    //get new image dimensions

    function CreateDelegate(contextObject, delegateMethod) {
        return function () {
            return delegateMethod.apply(contextObject, arguments);
        }
    }

    //new image on load

    function theNewImg_onload() {
        $bgimg.data("newImageW", this.width).data("newImageH", this.height);
        BackgroundLoad($bgimg, this.width, this.height);
    }

    //Image scale function

    function FullScreenBackground(thePic, imageWidth, imageHeight) {
        var winWidth = $(window).width();
        var winHeight = $(window).height();
        $(thePic).attr("width", imageWidth);
        $(thePic).attr("height", imageHeight);
        //$(theItem).css("margin-left",(winWidth-imageWidth)/2);
        //$(theItem).css("margin-top",(winHeight-imageHeight)/2);
    }

};

function formfocus() {

    $('input, textarea').focus(function () {
        if (this.value == this.defaultValue) {
            $(this).attr({
                value: ''
            });
			$(this).css('border', '1px solid white');
        }
        $(this).addClass('focused');
    }).blur(function () {
        if (this.value == '') {
            this.value = this.defaultValue;
            $(this).removeClass('focused');
        }
    });

};
