function hideURLbar() {
    window.scrollTo(0, 1);
}

function PopupCenter(pageURL, title, w, h) {
    var left = (screen.width / 2) - (w / 2);
    var top = (screen.height / 2) - (h / 2);
    var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
}

function loaded()
{

    if ($('#wrapper_h').length) {
        var $numIcons = $(".portfolio #scroller_h li").size() * 157
        $("#scroller_h").width($numIcons + 150);
        myScroll_h = new iScroll('wrapper_h', {
            vScroll: false,
            vScrollbar: true,
            hideScrollbar: false
        });
        myScroll_h.refresh();
        console.log("hor scroll");
    }


    $('.portfolio1 #portfolio-bot-row').css({
        'position': 'relative',
    })

    $('.portfolio2 #portfolio-bot-row').css({
        'position': 'relative',
        'left': '-600px',
        'width': '960px',
        'margin-bottom': '15px',
        'top': '250px'
    })

    $('.portfolio3 #portfolio-bot-row').css({
        'position': 'relative',
        'left': '-413px',
        'width': '960px',
        'margin-bottom': '15px',
        'top': '219px'
    })

    $('#horScrollInstruction').remove();

}

function hideURLbar() {
    window.scrollTo(0, 1);
}

var onMouseDownX;
var onMouseUpX;

function setupSocial() { 

	/* SOCIAL LINKS */
    $("#header #social ul li.twitter").click(function () {
        window.open("http://twitter.com/#!/BlueWhaleInc");
    })

    $("#header #social ul li.youTube").click(function () {
        window.open("http://www.youtube.com/user/BlueWhaleApps");
    })

    $("#header #social ul li.linkedIn").click(function () {
        window.open("http://www.linkedin.com/in/greggweiss");
    })

    $("#header #social ul li.facebook").click(function () {
        window.open("https://www.facebook.com/BlueWhaleApps");
    })

    $("#header #social ul li.bbb").click(function () {
        window.open("http://www.bbb.org/south-east-florida/business-reviews/website-and-web-page-development-services/blue-whale-web-solutions-in-wellington-fl-92017817");
    })
}

function setupHorizontalScroller() 
{
	if (navigator.platform.indexOf("iPad") != -1 || navigator.platform.indexOf("iPhone") != -1) {
        $("#scroller_h ul li img").click(function (e) {
            window.location = $(this).attr('rel');
        })

    } else

    {
        $("#wrapper_h, #portfolio-bot-row").css('height', '161px');
        $(document).mousedown(function (e) {
            onMouseDownX = e.pageX;
        });

        $(document).mouseup(function (e) {
            onMouseUpX = e.pageX;

            if (onMouseUpX != onMouseDownX) {
                console.log("swipe")
            } else {
                console.log("click")
                $("#scroller_h ul li img").click(function (e) {
                    window.location = $(this).attr('rel');
                })
            }
        });
    }
}

function setupLinks() 
{
	$("#footer-wrapper #footer #col5").click(function () {
        window.open("http://www.bbb.org/south-east-florida/business-reviews/website-and-web-page-development-services/blue-whale-web-solutions-in-wellington-fl-92017817");
    })

    $(".eBook").click(function () {

        window.open("http://blog.bluewhaleapps.com/free-ebook-why-every-business-needs-an-app/");
    })

    $(".getStarted").click(function () {
        window.open("http://blog.bluewhaleapps.com/app-development-inquiry-/")

    })

 	$("#header #main-nav-wrapper #main-nav ul.nav li a").hover(function (e) {
        $(this).parent().css('border-bottom', '2px solid #0097df');
    }, function (e) {
        $(this).parent().css('border-bottom', 'none');
    })

	/* MAIN NAV LINKS */

    $(".head-company").click(function () {
        window.location = "company.asp";
    })

    $(".head-blog").click(function () {
        window.open("http://blog.bluewhaleapps.com");
    })

    $(".head-portfolio").click(function () {
        window.location = "portfolio.asp";
    })

    $(".head-contact").click(function () {
        window.location = "contact.asp";
    })
}

function setupPageName() 
{
	var thisURL = window.location.pathname;
    var thisPageName = thisURL.substring(thisURL.lastIndexOf('/') + 1);
	
	if (thisPageName == 'company.asp') {
        $('li.head-company').addClass('page-active');
        $('li.head-company').unbind('mouseenter mouseleave')
    } else if (thisPageName.substr(0, 9) == 'portfolio') {
        $('li.head-portfolio').addClass('page-active');
        $('li.head-portfolio').unbind('mouseenter mouseleave')
    } else if (thisPageName == 'blog.asp') {
        $('li.head-blog').addClass('page-active');
        $('li.head-blog').unbind('mouseenter mouseleave')
    } else if (thisPageName == 'contact.asp') {
        $('li.head-contact').addClass('page-active');
        $('li.head-contact').unbind('mouseenter mouseleave');
    }
}

$(document).ready(function () {

    setupSocial();
	setupHorizontalScroller();
	setupLinks();
	setupPageName();

    window.addEventListener('DOMContentLoaded', function () {
        setTimeout(loaded, 200);
    }, false);


    addEventListener("load", function () {
        setTimeout(hideURLbar, 0);
    }, false);
   

    $("a[rel^='BlueWhaleApps-Video']").prettyPhoto(function (e) {
        $(this).preventDefault();
    });

   
    $("#contact-form").validate({

        messages: {
            name: "Please enter your full name.",
            email: "Please enter a valid email.",
            phone: "Please enter your phone number.",
            comments: "Please enter your comments.",
        },

        errorPlacement: function (error, element) {
            error.appendTo(element.next('div'));
        },
    });

})
