function js_accepted(screenSize) {
    if ($('body').hasClass(screenSize)) {
        return true;
    }
    return false;
}

function image_adapt() {
    if (js_accepted('small') || (js_accepted('medium') && js_accepted('blog')) ||
			(js_accepted('medium') && js_accepted('single')) ||
				(js_accepted('medium') && js_accepted('archive'))) {
        $('img').removeAttr('width').removeAttr('height').css('max-width', '100%');
    }
}

$(document).ready(function () {

    /*
    Luke Snowden
    JS backup, to decline JS scripts for smaller devices if not needed.
    compare via if( js_accepted( $screenSize ) ) { }
    small, medium and main accepted
    */

    $.fn.chemImportAlt({
        main: '/css/main.css',
        medium: '/css/medium.css',
        small: '/css/small.css',
        onChange: function () {
            image_adapt();
        }
    });

    /*
    Luke Snowden
    If the device is "small" remove all height and width attributes from image elements.
    If the images are correct, they should be that size anyway and then we can use the
    max-width to scale down the image if it is too large.
    */

    image_adapt();

    /*
    Little JS hack to define the latest post. It's only styling enhancement and looks fine without it.
    */

    $('#blogMain div:first').addClass('latest-post');


    $('.noJS').removeClass('noJS');

    $('#homepageCarouselFrames').cycle({
        timeout: 5000,
        speed: 800,
        pause: true,
        fx: 'scrollHorz',
        next: '#homepageCarouselNext',
        prev: '#homepageCarouselPrev',
        easing: 'easeInOutExpo'
    });

    $('.clientsTabContainer').cycle(
    {
        timeout: 0,
        speed: 800,
        fx: 'scrollHorz',
        next: '.tabControls .plusLink',
        prev: '.tabControls .plusLinkLeft',
        easing: 'easeInOutExpo',
        after: function (curr, next, opts) {
            $('.tabControls .slideNumber').text(opts.currSlide + 1);
            $('.tabControls .slideTotal').text(opts.slideCount);
        }
    }
    );

    /*$('.clientsTabContainer').height($('.clientsTab:first').height());
	
    $('.tabControls').click(function()
    {
		
    });*/

    /* SHOW FIRST ACCORDIAN ITEM */
    if (js_accepted('main')) {
        $(".aboutAccordion h2 a:first").removeClass('minusLink');
        $(".aboutAccordion h2 a:first").toggleClass('minusLink').closest('.aboutAccordion').find('.accordionContent').slideToggle().addClass('openAccordion');


        $('.aboutAccordion h2 a').click(function () {
            if ($(this).hasClass('minusLink') == false) {
                $('.openAccordion').slideToggle().removeClass('openAccordion');
                $('.aboutAccordion h2 a').removeClass('minusLink');
                $(this).toggleClass('minusLink').closest('.aboutAccordion').find('.accordionContent').slideToggle().addClass('openAccordion');
            }
            else {
                $('.openAccordion').slideToggle().removeClass('openAccordion');
                $('.aboutAccordion h2 a').removeClass('minusLink');
            }
            return false;
        });
    } else {
        $('h2 .plusLink').each(function () {
            var ob = $(this);
            var text = ob.text();
            var obParent = ob.parent();
            obParent.html(text);
        });
    }

    /*
    $('.person').hover(
    function(){
    $(this).addClass('personOn').css('z-index',101);
    $('.personDetails',this).animate({height:387,width:318},500);
    $('.imageContainer',this).animate({height:384},500);		
    },
    function()
    {
    $(this).css('z-index','auto');
    $('.personDetails',this).animate({height:192,width:0},500);	
    $('.imageContainer',this).animate({height:190},500,function()
    {
    $(this).closest('.person').removeClass('personOn');
    });		
    }
    );
    */


    $('.person').click(function () {
        var ob = $(this);
        if (js_accepted('main')) {
            if ($(this).hasClass('personOn') == false) {
                var openPerson = $('.personOn');
                openPerson.css({ zIndex: '101' });
                $('.personDetails', openPerson).animate({ height: 192, width: 0 }, 500);
                $('.imageContainer', openPerson).animate({ height: 190 }, 500, function () {
                    openPerson.css({ zIndex: '1' });
                    openPerson.removeClass('personOn');
                });

                $(this).addClass('personOn').css({ zIndex: '102' });
                $('.personDetails', this).animate({ height: 387, width: 318 }, 500);
                $('.imageContainer', this).animate({ height: 384 }, 500);
            }
        }
    });

    $('.person .closeButton').click(function () {
        var openPerson = $('.personOn');
        openPerson.css({ zIndex: '101' });
        $('.personDetails', openPerson).animate({ height: 192, width: 0 }, 500);
        $('.imageContainer', openPerson).animate({ height: 190 }, 500, function () {
            openPerson.css({ zIndex: '1' });
            openPerson.removeClass('personOn');
        });
        return false;
    });

    $('.projectThumbs a').click(function () {
        var imgPath = $(this).attr('href');
        $('.projectMainImage').attr('src', imgPath);
        return false;
    });


    $(".contactForm .submitButton").click(function () {
        var hasError = false;
        $('.formField:has(input.required)', '.contactForm').each(function () {
            if ($('input', this).val() == '') {
                $(this).addClass('error');
                hasError = true;
            }
            else {
                $(this).removeClass('error');
            }
        });
        $('.formField:has(textarea.required)', '.contactForm').each(function () {
            if ($('textarea', this).val() == '') {
                $(this).addClass('error');
                hasError = true;
            }
            else {
                $(this).removeClass('error');
            }
        });

        if (hasError == false) {
            return true;
        }
        return false;
    });

    // JOB ACCORDIAN

    if (js_accepted('main')) {

        $(".jobdetails").slideToggle();

        $(".jobCategory h3 a").click(function () {

            var showonlyone = true;
            if (!$(this).hasClass("minusLink")) {
                if (showonlyone) {
                    $(".openAccordian").slideToggle().removeClass("openAccordian");
                    $(".jobCategory h3 a").removeClass("minusLink");
                }
                $(this).toggleClass("minusLink");
                $(this).closest(".jobItem").find(".jobdetails").slideToggle().addClass("openAccordian");
            } else {
                if (showonlyone) {
                    $(".openAccordian").slideToggle().removeClass("openAccordian");
                    $(".jobCategory h3 a").removeClass("minusLink");
                }
            }

            return false;
        });
    } else {
        $('.jobCategory h3 a').each(function () {
            var ob = $(this);
            var text = ob.text();
            var obParent = ob.parent();
            obParent.html(text);
        });
    }

});
