$(document).ready(function () {

    sfHover();
    cufonCalls();
    HomePageCarousel();
    flashBuilders();
    setupInquiryBox();
    setupFancyBoxes();

    $("#Details").toggle();
    $('#ToggleDetails').click(function () {
        $('#Details').toggle('fast');
        $(this).toggleClass('open');
    });

    $("#FiveYearContent").toggle();
    $('#FiveYearTitle').click(function () {
        $('#FiveYearContent').toggle('fast');
        $(this).toggleClass('open');
    });


    if ($(".ClientsPage").length != 0) {
        setupClientSorter();
    }

    $("select").uniform();

    if ($("#googlemap").length != 0) {
        generateGoogleMap();
    }

    if ($("#restaurant-support-content").length != 0) {
        supportFAQ();
    }

	if ($(".press-categories-buttons").length != 0) {
        pressRoomNews();
    }

    if ($(".HomePage").length != 0) {
        HomePageTrigger();
    }



    $('#TeamPage-content div.team-set-container').toggle();

    $("#TeamPage-content h2").each(function (index) {
        $(this).addClass("item-" + $("#TeamPage-content h2").index(this) + "")
        $(this).click(function () {
            $(this).toggleClass('open');
            $(this).next().toggle('slow');
        });

    });

    $('#TeamPage-content #partners-container').toggle();
    $('#TeamPage-content h2.first').toggleClass('open');


    $('#Jobs-content div.job-set-container').toggle();

    $("#Jobs-content h2").each(function (index) {
        $(this).addClass("item-" + $("#Jobs-content h2").index(this) + "")
        $(this).click(function () {
            $(this).toggleClass('open');
            $(this).next().toggle('slow');
        });

    });


    $("h2.promise-headline").each(function (index) {
        $(this).click(function () {
            $(this).toggleClass('open');
            $(this).next().toggle('slow');
        });
    });
    $('.promise').toggle();

	$("h2.toggle-headline").each(function (index) {
        $(this).click(function () {
            $(this).toggleClass('open');
            $(this).next().toggle('slow');
        });
    });
    $('.toggle').toggle();


    $(".mobile #Menu1").toggle();
    $(".mobile .mobile-link").click(function () {
        $(".mobile .mobile-link").toggleClass('open');
        $(".mobile #Menu1").toggle('slow');

        if ($(".mobile .mobile-link a").html() == "Show Pages") {
            $(".mobile .mobile-link a").text("Hide Pages");
        }
        else {
            $(".mobile .mobile-link a").text("Show Pages");
        }
    });

	/* Rotate home page banner slides */
	var autoSlideChange = setInterval("changeSlide()", 6000);
	$('.banner .carousel-pagination a, .banner > a.carousel-control').mouseup(function() {
		clearInterval(autoSlideChange);
	});
});

/* Rotate Banner Slides */
function changeSlide() {
	var nextSlide = $('.banner .carousel-pagination a.active').next('a');
	if(nextSlide.length > 0) {
		nextSlide.trigger('click');
	} else {
		$('.banner .carousel-pagination a:first-child').click();
	}
}

function HomePageTrigger() {

    $("#trigJs").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'scrolling': 'no'
    });

    if (window.addEventListener) {
        var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
        window.addEventListener("keydown", function (e) {
            kkeys.push(e.keyCode);
            if (kkeys.toString().indexOf(konami) >= 0) {
                $("#trigJs").trigger('click');
                kkeys = [];
            }
        }, true);
    }

}

function supportFAQ() {

    $('#restaurant-support-content div.help-detail').toggle();

    $("#restaurant-support-content h4").each(function (index) {
        $(this).addClass("item-" + $("#restaurant-support-content h4").index(this) + "")
        $(this).click(function () {
            $(this).toggleClass('open');
            $(this).next().toggle('slow');

        });

    });


}


function pressRoomNews() {
	$('.press-onosys-news-btn').click(function() {
		changePressRoomFeed(this, 'news');
		return false;
	});
	$('.press-in-the-media-btn').click(function() {
		changePressRoomFeed(this, 'media');
		return false;
	});

	function changePressRoomFeed(selector, makeActive) {
		$('.press-categories-buttons .current').removeClass('current');
		$(selector).addClass('current');
		if(makeActive == 'news') {
			$('#in-the-media').hide();
			$('#onosys-news').show();
		} else if(makeActive == 'media') {
			$('#onosys-news').hide();
			$('#in-the-media').show();
		}
		
	}
}


function setupClientSorter() {

    $('#content #sort-list').addClass('show');

    var $preferences = {
        adjustHeight: 'auto',
        useScaling: false
    };

    var $list = $('#list');
    var $data = $list.clone();

    $('.pizza-link a').click(function () {
        clearSelected();
        $('.pizza-link a').addClass('selected');
        var $filtered_data = $data.find('li.pizza');
        $list.quicksand($filtered_data, $preferences);

    });
    $('.sandwich-link a').click(function () {
        clearSelected();
        $('.sandwich-link a').addClass('selected');
        var $filtered_data = $data.find('li.sandwich');
        $list.quicksand($filtered_data, $preferences);

    });
    $('.casual-link a').click(function () {
        clearSelected();
        $('.casual-link a').addClass('selected');
        var $filtered_data = $data.find('li.casual');
        $list.quicksand($filtered_data, $preferences);

    });
    $('.fast-link a').click(function () {
        clearSelected();
        $('.fast-link a').addClass('selected');
        var $filtered_data = $data.find('li.fast');
        $list.quicksand($filtered_data, $preferences);

    });
    $('.catering-link a').click(function () {
        clearSelected();
        $('.catering-link a').addClass('selected');
        var $filtered_data = $data.find('li.catering');
        $list.quicksand($filtered_data, $preferences);

    });

    $('.all-link a').click(function () {
        clearSelected();
        $('.all-link a').addClass('selected');
        var $filtered_data = $data.find('li');
        $list.quicksand($filtered_data, $preferences);

    });

}

function clearSelected() {
    $('#sort-list a').removeClass('selected')
}


(function ($) {
    $.fn.sorted = function (customOptions) {
        var options = {
            reversed: false,
            by: function (a) {
                return a.text();
            }
        };
        $.extend(options, customOptions);

        $data = $(this);
        arr = $data.get();
        arr.sort(function (a, b) {

            var valA = options.by($(a));
            var valB = options.by($(b));
            if (options.reversed) {
                return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;
            } else {
                return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;
            }
        });
        return $(arr);
    };

})(jQuery);





function setupFancyBoxes() {

    $("#fancybox-newsletter, #fancybox-newsletter2").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'none',
        'transitionOut': 'none'
    });


    $("#fancybox-vid-link-01").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'none',
        'transitionOut': 'none'
    });


    $("#fancybox-vid-link-02").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'none',
        'transitionOut': 'none'
    });

    $(".fancybox-link").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'scrolling': 'no'
    });

	/*
	$("#fancybox-download-logos").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'none',
        'transitionOut': 'none',
		'scrolling': 'no'
    });
	*/

}

function setupInquiryBox() {
    $("#reqSalesInquiry-hover").mouseenter(function () {
        $("#reqSalesInquiry-hover").stop();
        $("#reqSalesInquiry-hover").animate({
            right: "0px",
            width: "257px"
        }, 500);
    });

    $("#reqSalesInquiry-hover").mouseleave(function () {
        $("#reqSalesInquiry-hover").stop();
        $("#reqSalesInquiry-hover").animate({
            right: "0px",
            width: "72px"
        }, 500);
    });
}

function flashBuilders() {
    //flash-video-holder
    if ($("#flash-iPhone").length != 0) {
        var flashvars = {};
        var params = {};
        params.wmode = "transparent";
        var attributes = {};
        attributes.id = "flash-iPhone";
        swfobject.embedSWF("/flash/iphone-movie.swf", "flash-iPhone", "240", "435", "9.0.0", false, flashvars, params, attributes);
    }

    if ($("#flashvideo-overview").length != 0) {
        var flashvars = {};
        var params = {};
        params.wmode = "transparent";
        var attributes = {};
        attributes.id = "flashvideo-overview";
        swfobject.embedSWF("/flash/Overview/overview.swf", "flashvideo-overview", "540", "425", "9.0.0", false, flashvars, params, attributes);
    }

    if ($("#flashvideo-repeat").length != 0) {
        var flashvars = {};
        var params = {};
        params.wmode = "transparent";
        var attributes = {};
        attributes.id = "flashvideo-repeat";
        swfobject.embedSWF("/flash/Repeat/repeat.swf", "flashvideo-repeat", "540", "425", "9.0.0", false, flashvars, params, attributes);
    }

    if ($("#mobile-style-flash").length != 0) {
        var flashvars = {};
        var params = {};
        params.wmode = "transparent";
        var attributes = {};
        attributes.id = "mobile-style-flash";
        swfobject.embedSWF("/flash/mobile-styling.swf", "mobile-style-flash", "260", "356", "9.0.0", false, flashvars, params, attributes);
    }

}

$(window).scroll(function () {
    if ($(window).scrollTop() > 180) {
        $("#reqSalesInquiry-hover").stop();

        //$("#reqSalesInquiry-hover").css("top", $(window).scrollTop() + 43 + "px");
        $("#reqSalesInquiry-hover").animate({
            top: $(window).scrollTop() + 43 + "px",
            right: "0px",
            width: "72px"
        }, 500);
    }
    else {

        $("#reqSalesInquiry-hover").stop();

        //$("#reqSalesInquiry-hover").css("top", $(window).scrollTop() + 43 + "px");
        $("#reqSalesInquiry-hover").animate({
            right: "0px",
            width: "0px"
        }, 500);

    }
});


function HomePageCarousel() {
    $("div.banner").carousel({
        pagination: true,
        autoSlide: /*true*/false,
        autoSlideInterval: 5000
    });

    $("div.banner p a").each(function () {
        // alert($("div.banner p a").index(this));
        var thistext = $("div.banner li:nth-child(" + ($("div.banner p a").index(this) + 1) + "n)").attr("title");
        var thiscss = $("div.banner li:nth-child(" + ($("div.banner p a").index(this) + 1) + "n)").attr("class");
        $(this).text(thistext);
        $(this).wrapInner("<span></span>").wrapInner("<span class='" + thiscss + "'></span>");
    });
}

function cufonCalls() {
    Cufon.replace('.banner h2');
    Cufon.replace('#content h1');
    Cufon.replace('#tools-container .tool-content h2');
    Cufon.replace('.HomePage #content h2 span');
    Cufon.replace('#content #col-right h2');
    Cufon.replace('#sidebar h2');
    Cufon.replace('#client-display h2');
    Cufon.replace('#header .contact-number');
    Cufon.replace('#header #reqSalesInquiry a span.title');
    Cufon.replace('#content #special-content .special-item h2, #content #special-content .special-item .item-content .item-title');
    Cufon.replace('.navigation span');
}

function sfHover() {
    var sfEls = document.getElementById("nav-main").getElementsByTagName("LI");
    for (var i = 0; i < sfEls.length; i++) {
        sfEls[i].onmouseover = function () {
            this.className += " sfhover";
        }
        sfEls[i].onmouseout = function () {
            this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }

    //

    $("#nav-main .level4 li a.selected").parentsUntil('#nav-main ul.level1').addClass("parSelected clearfix");


    $("#nav-main .level3 li a.selected").parentsUntil('#nav-main ul.level1').addClass("parSelected clearfix");


    $("#nav-main a.level2.selected").parentsUntil('#nav-main ul.level1').addClass("parSelected clearfix");

    $("#nav-main .parSelected").prev("a").addClass("prevSelected");

    if ($("#nav-main li.parSelected ul").length != 0) {
        $("#content").addClass("levels1");
    }

    if ($("#nav-main li.parSelected li.parSelected ul").length != 0) {
        $("#content").addClass("levels2");
    }

    $("ul li:last-child").addClass("last");

}


function generateGoogleMap() {

    var map = new GMap2(document.getElementById("googlemap"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(41.522589, -81.659653), 12);


    // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.width = 80;
    baseIcon.height = 80;
    baseIcon.iconSize = new GSize(48, 80);
    baseIcon.shadowSize = new GSize(92, 80);
    baseIcon.iconAnchor = new GPoint(24, 80);
    baseIcon.infoWindowAnchor = new GPoint(24, 2);

    // Creates a marker whose info window displays the letter corresponding
    // to the given index.
    function createMarker(point, index) {
        // Create a lettered icon for this point using our icon class
        var letter = String.fromCharCode("A".charCodeAt(0) + index);
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = "http://www.onosys.com/images/onosys-map-marker.png";

        // Set up our GMarkerOptions object
        markerOptions = { icon: letteredIcon };
        var marker = new GMarker(point, markerOptions);

        GEvent.addListener(marker, "click", function () {
            marker.openInfoWindowHtml("<p><b>ONOSYS</b> <br /> Online Ordering System<br/><a href='http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=onosys&sll=37.0625,-95.677068&sspn=37.598824,64.423828&ie=UTF8&hq=onosys&hnear=United+States&ll=41.509405,-81.659478&spn=0.004146,0.007864&z=17&iwloc=A'>More Information from Google Maps</a></p>");
        });
        return marker;
    }

    // Add 10 markers to the map at random locations
    var point = new GLatLng(41.508689, -81.659653);
    map.addOverlay(createMarker(point, 0));

}
