// init.js

jQuery.noConflict();

window.onload = function() {
    jQuery('#castingresults .jScrollPaneContainer').css('width', '620px');
}

var p05 = 0;

function verticalPosition() {
    // vertical align site
    var wrapper = jQuery('#wrapper');
    var wrapperHeight = wrapper.height();
    var screenHeight = jQuery(window).height();
    if (wrapperHeight < screenHeight) wrapper.css('margin-top', (screenHeight - wrapperHeight) / 2);
}

//Owain new storm bits ======================================================================
//=====================================================================================================
//======================================================================================================================

if (typeof forEach !== 'function'){
    function forEach(iterable, fn, bind) {
        for (var i = 0, j = iterable.length; i < j; i++) fn.call(bind, iterable[i], i, iterable);
    };
}
function updateTable() {
    jQuery('#castingTable td:first').filter(function(index) {
        return jQuery(this).css('width', '60%');
    });
}

function xSlider() {
    this.speed = 400;
    this.imageWidth = 300;
    this.size = jQuery('.stormSlider li').length * this.imageWidth;
    this.scrollAmount = 300;
    var newVal;
    this.animateRight = function() {
        var curPos = parseInt(jQuery('.stormHolder').css('left'));
        if (curPos > -(this.size - 960)) {
            newVal = this.scrollAmount;
            var posLeft = Math.floor(jQuery('.stormHolder').position().left);
            jQuery('.stormHolder').stop().animate({ left: '-=' + newVal }, { easing: 'easeInOutSine', duration: this.speed });
            updateSlideControlRight();
        }
    };
    this.animateLeft = function() {
        var curPos = parseInt(jQuery('.stormHolder').css('left'));
        if (curPos === 0 || curPos >= 0) {
            return;
        }else {
            jQuery('.stormHolder').stop().animate({ left: '+=' + this.scrollAmount }, { easing: 'easeInOutSine', duration: this.speed });
        }
        updateSlideControlLeft();
    };
}

xSlider.prototype.resizeContainer = function(el) {
    jQuery(el).css('width', this.size);
}

function handleSliderChange(e, ui) {
    var maxScroll = (jQuery('.stormSlider li').length * 300) - 960;
    jQuery(".stormHolder").animate({ left: -ui.value *
     (maxScroll / 100)
    }, 1000);

    //Set the global pos var to ui value
    p05 = ui.value;
}

function handleSliderSlide(e, ui) {
    var maxScroll = jQuery('.stormSlider li').length * 300 - 960;
    jQuery(".stormHolder").attr({ left: ui.value * (maxScroll / 100) });
}

function updateSlideControlRight() {
    //Issues are going to arise due to float errors
    var maxScroll = jQuery('.stormSlider li').length * 300 - 960;
    var updateAmount = (100 / maxScroll) * 300;
    p05 += updateAmount;
    jQuery("#widgetSlider").slider('value', p05);
}

function updateSlideControlLeft() {
    //Issues are going to arise due to float errors
    var maxScroll = jQuery('.stormSlider li').length * 300 - 960;
    var updateAmount = (100 / maxScroll) * 300;
    p05 -= updateAmount;
    jQuery("#widgetSlider").slider('value', p05);
}

var switchViewState = (function() {
    return {
        showList: function(clickIndex) {
            var targ = jQuery('.tPanel').eq(clickIndex);
            targ.hide();
            jQuery('.lPanel').eq(clickIndex).show();
            targ.removeClass('activePanel');
        },
        showTextArea: function(clickIndex) {
            jQuery('.lPanel').eq(clickIndex).hide();
            jQuery('.tPanel').eq(clickIndex).show();
            jQuery('.tPanel').eq(clickIndex).addClass('activePanel');
        },
        changeBg: function(index, col) {
            var args = Array.prototype.splice.call(this,arguments);
            if (typeof arguments[1] === 'boolean' && arguments[1] === true) {
                jQuery('#castList li').eq(arguments[0]).addClass('black');
            } else {
                jQuery('#castList li').eq(arguments[0]).removeClass('black');
            }
        },
        toggleIcon: function(clickIndex, value) {
            if (value === true) {
                jQuery('.toggleIcon').eq(clickIndex).removeClass('plusIcon');
                jQuery('.toggleIcon').eq(clickIndex).addClass('minusIcon');
            } else {
                jQuery('.toggleIcon').eq(clickIndex).removeClass('minusIcon');
                jQuery('.toggleIcon').eq(clickIndex).addClass('plusIcon');
            }
        }
    }
})();

function scrollUp(speed) {
    jQuery('html').animate({ scrollTop: 0 }, arguments[0]);
}

//Function to make stars black if tick boxes are checked
function catchStars() {
    var st = jQuery('.castingRight .starIcon');
    var count = [];
    jQuery('.fCheck').find('input').each(function(){
        if (jQuery(this).is(':checked')){
            var index = jQuery('.fCheck input').index(this);
            count.push(index);
        } 
    });
    
    var i;
    for (i=0;i<count.length;i++){
        jQuery('.castingRight .starIcon').eq(count[i]).addClass('ticked');
        //alert(i);
    }
}

//End Owain Edits========================================================================================================
//=======================================================================================================================
//=======================================================================================================================

jQuery(document).ready(function() {

    // Fix background image caching problem
    if (jQuery.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true);
            jQuery('#nal').css('top', '-57px');
            jQuery('#nar').css('top', '-57px');
        } catch (err) { }
    }

    VideoJS.setupAllWhenReady();
    catchStars();
    updateTable();

    jQuery("#widgetSlider").slider({
        animate: true,
        change: handleSliderChange,
        slide: handleSliderSlide
    });

    jQuery('#nal').live('click', function() {
        jQuery('.controlsLeft').trigger('click');
    });

    jQuery('#nar').live('click', function() {
        jQuery('.controlsRight').trigger('click');
    });

    //Catch all stars on the models page. If the checkboxes are ticked make the stars black
    //catchStars();

    //Switch the view state on the new bookings section
    jQuery('.castingLeft .toggleIcon').click(function() {
        var clickIndex = jQuery('.castingLeft .toggleIcon').index(this);
        if (jQuery('.tPanel').eq(clickIndex).hasClass('activePanel')) {
            switchViewState.showList(clickIndex);
            switchViewState.changeBg(clickIndex, true);
            switchViewState.toggleIcon(clickIndex, true);
        } else {
            switchViewState.showTextArea(clickIndex);
            switchViewState.changeBg(clickIndex, false);
            switchViewState.toggleIcon(clickIndex, false);
        }
        return false;
    });

    jQuery('.starIcon').click(function() {
        var starIndex = jQuery('.castingRight .starIcon').index(this);
        if (jQuery(this).hasClass('ticked')) {
            jQuery(this).removeClass('ticked');
            jQuery('.fCheck input').eq(starIndex).attr('checked', false);
        } else {
            jQuery(this).addClass('ticked');
            jQuery('.fCheck input').eq(starIndex).attr('checked', true);
        }
        //switchViewState.addToFav(starIndex);
        return false;
    });

    jQuery('#returntoTop').click(function() {
        scrollUp(500);
        return false;
    });

    stormSlide = new xSlider();
    stormSlide.resizeContainer('.stormSlider');

    jQuery('.controlsLeft').click(function(e) {
        if (jQuery('.stormHolder').not(':animated')) {
            e.stopPropagation();
            stormSlide.animateLeft();
        } else {
            return;
        }
        return false;
    });
    jQuery('.controlsRight').click(function() {
        if (jQuery('.stormHolder').not(':animated')) {
            stormSlide.animateRight();
        } else {
            return;
        }
        return false;
    });

    // drop downs
    jQuery('#nav-search').click(function() {
        var drop_down = jQuery('#dd-search');
        var other_dd = jQuery('#dd-sign_in');
        var examples_dd = jQuery('#dd-examples');
        // hide other drop-downs
        if (other_dd.hasClass('open')) other_dd.removeClass('open').fadeOut('fast');
        if (examples_dd.hasClass('open')) examples_dd.removeClass('open').fadeOut('fast');
        //
        if (drop_down.hasClass('open')) drop_down.removeClass('open').fadeOut('fast'); // hide search
        else drop_down.addClass('open').fadeIn('slow'); // show search
        return false;
    });
    jQuery('#nav-sign_in').click(function() {
        var drop_down = jQuery('#dd-sign_in');
        var other_dd = jQuery('#dd-search');
        var examples_dd = jQuery('#dd-examples');
        // hide other drop-downs
        if (other_dd.hasClass('open')) other_dd.removeClass('open').fadeOut('fast');
        if (examples_dd.hasClass('open')) examples_dd.removeClass('open').fadeOut('fast');
        //
        if (drop_down.hasClass('open')) drop_down.removeClass('open').fadeOut('fast'); // hide sign in
        else drop_down.addClass('open').slideDown(); // 500, 'easeOutBack' show sign in with custom easing						
        return false;
    });
    jQuery('.show_examples').click(function() {
        var drop_down = jQuery('#dd-examples');
        var first_dd = jQuery('#dd-sign_in');
        var other_dd = jQuery('#dd-search');
        // hide other drop-downs
        if (other_dd.hasClass('open')) other_dd.removeClass('open').fadeOut('fast');
        if (first_dd.hasClass('open')) first_dd.removeClass('open').fadeOut('fast');
        //
        if (drop_down.hasClass('open')) drop_down.removeClass('open').fadeOut('fast'); // hide examples
        else {
            drop_down.addClass('open').slideDown(); // show examples with custom easing
            // have to calculate heights here as they return 0 when hidden
            // vertically align example name
            /*
            var height = jQuery('.example span').height();
            jQuery('.example span').each(function() {
            var text_h = jQuery(this).height();
            var ph = 40;
            var mh = (ph - text_h) / 2;
            jQuery(this).css('padding-top', mh);
            });
            */
        }
        return false;
    });
    // close drop down
    jQuery('.btn-close').click(function() {
        jQuery(this).parent().parent('.drop-down').removeClass('open').fadeOut('fast');
        return false;
    });
    // sign in labels 
    jQuery('.input_sign_in').each(function() {
        if (jQuery(this).val() != '') jQuery(this).prev('.label_sign_in').hide();
    });
    jQuery('.label_sign_in').click(function() {
        jQuery(this).hide();
        jQuery(this).next('.input_sign_in').focus();
        return false;
    });
    jQuery('.input_sign_in').focus(function() {
        jQuery(this).prev('.label_sign_in').hide();
    });
    jQuery('.input_sign_in').blur(function() {
        if (jQuery(this).val() == '') jQuery(this).prev('.label_sign_in').show();
        return false;
    });

    // create bespoke drop-down

    jQuery('.bespoke').each(function() {
        // get drop down ID and options
        var selectID = jQuery(this).attr("id");
        var select_options = new Array();
        select_options = jQuery('#' + selectID + ' option');
        var output = '';
        var output_options = '';
        var first_option = '';
        var inline_styles = '';
        // loop through options
        for (i = 0; i < select_options.length; i++) {
            var option_value = select_options[i].getAttribute('value');
            var option_text = select_options[i].text;
            if (i == 0) first_option = option_text;
            if (option_value == null) option_value = '';
            if (i > 13) inline_styles = ' style="height: 200px;"';
            //alert(option_text);
            output_options += '<a href="#' + option_value + '" rel="' + selectID + '">' + option_text + '</a>\n';
        }
        output = '<div class="select_box">\n<span class="default">' + first_option + '</span>\n<div class="menu">\n<div class="options"' + inline_styles + '>' + output_options + '</div>\n</div>\n</div>';

        // write html
        jQuery(this).after(output);

        // bind functions to output
        jQuery(this).next('.select_box').children('.default').bind("click", function() {
            // get menu vars
            var menu = jQuery(this).parent().children('.menu');
            // check open status
            if (menu.hasClass('open')) {
                // close open menus
                jQuery('.menu.open').removeClass('open').slideUp();
                //menu.removeClass('open').slideUp();
            } else {
                // close open menus
                jQuery('.menu.open').removeClass('open').slideUp();
                menu.addClass('open').slideDown();
                jQuery(this).next('.menu').children('.options').jScrollPane();
                // turn off body click when menu open
                jQuery('.select_box').mouseover(function() {
                    jQuery('body').unbind("click");
                }).mouseout(function() {
                    // turn body click on
                    jQuery('body').bind("click", function() {
                        jQuery('.menu.open:not(:animated)').removeClass('open').slideUp();
                    });
                });
            }
        });

        jQuery('.options a').bind("click", function() {
            var value_text = jQuery(this).text();
            var link = jQuery(this).attr("href");
            var link_val = link.split('#');
            var value = link_val[1];
            var targetID = '#' + jQuery(this).attr("rel");
            // change drop down label to selected
            var default_value = value_text.split("/");
            jQuery(this).parent('.options').parent('.jScrollPaneContainer').parent('.menu').prev('.default').text(default_value[0]);
            // go through and select real form option
            //alert(link_val[1]);
            jQuery(targetID + ' option').each(function() {
                if (jQuery(this).val() == value) jQuery(this).attr("selected", "selected");
            });
            jQuery(this).parent('.options').parent('.jScrollPaneContainer').parent('.menu').removeClass('open').slideUp();
            return false;
        });
    });
    // close menu on body click
    jQuery('body').click(function() {
        jQuery('.menu.open:not(:animated)').removeClass('open').slideUp();
    });
    // show and hide initial text input value on focus/blur
    jQuery('.show_hide_value').each(function() {
        // get initial value on page load									 
        var value = jQuery(this).val();
        jQuery(this).bind("focus", function() {
            // compare current value with initial on focus
            if (jQuery(this).val() == value) jQuery(this).val('');
        }).bind("blur", function() {
            var current_value = jQuery(this).val();
            if (current_value == '') jQuery(this).val(value);
        });
    });

});

//JQuery Easing Plugin
//=======================================================================================================================
//=======================================================================================================================
//=======================================================================================================================

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend(jQuery.easing,
{
    def: 'easeOutQuad',
    swing: function(x, t, b, c, d) {
        //alert(jQuery.easing.default);
        return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
    },
    easeInQuad: function(x, t, b, c, d) {
        return c * (t /= d) * t + b;
    },
    easeOutQuad: function(x, t, b, c, d) {
        return -c * (t /= d) * (t - 2) + b;
    },
    easeInOutQuad: function(x, t, b, c, d) {
        if ((t /= d / 2) < 1) return c / 2 * t * t + b;
        return -c / 2 * ((--t) * (t - 2) - 1) + b;
    },
    easeInCubic: function(x, t, b, c, d) {
        return c * (t /= d) * t * t + b;
    },
    easeOutCubic: function(x, t, b, c, d) {
        return c * ((t = t / d - 1) * t * t + 1) + b;
    },
    easeInOutCubic: function(x, t, b, c, d) {
        if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
        return c / 2 * ((t -= 2) * t * t + 2) + b;
    },
    easeInQuart: function(x, t, b, c, d) {
        return c * (t /= d) * t * t * t + b;
    },
    easeOutQuart: function(x, t, b, c, d) {
        return -c * ((t = t / d - 1) * t * t * t - 1) + b;
    },
    easeInOutQuart: function(x, t, b, c, d) {
        if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
        return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
    },
    easeInQuint: function(x, t, b, c, d) {
        return c * (t /= d) * t * t * t * t + b;
    },
    easeOutQuint: function(x, t, b, c, d) {
        return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
    },
    easeInOutQuint: function(x, t, b, c, d) {
        if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
        return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
    },
    easeInSine: function(x, t, b, c, d) {
        return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
    },
    easeOutSine: function(x, t, b, c, d) {
        return c * Math.sin(t / d * (Math.PI / 2)) + b;
    },
    easeInOutSine: function(x, t, b, c, d) {
        return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
    },
    easeInExpo: function(x, t, b, c, d) {
        return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
    },
    easeOutExpo: function(x, t, b, c, d) {
        return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
    },
    easeInOutExpo: function(x, t, b, c, d) {
        if (t == 0) return b;
        if (t == d) return b + c;
        if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
        return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
    },
    easeInCirc: function(x, t, b, c, d) {
        return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
    },
    easeOutCirc: function(x, t, b, c, d) {
        return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
    },
    easeInOutCirc: function(x, t, b, c, d) {
        if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
        return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
    },
    easeInElastic: function(x, t, b, c, d) {
        var s = 1.70158; var p = 0; var a = c;
        if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3;
        if (a < Math.abs(c)) { a = c; var s = p / 4; }
        else var s = p / (2 * Math.PI) * Math.asin(c / a);
        return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
    },
    easeOutElastic: function(x, t, b, c, d) {
        var s = 1.70158; var p = 0; var a = c;
        if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3;
        if (a < Math.abs(c)) { a = c; var s = p / 4; }
        else var s = p / (2 * Math.PI) * Math.asin(c / a);
        return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
    },
    easeInOutElastic: function(x, t, b, c, d) {
        var s = 1.70158; var p = 0; var a = c;
        if (t == 0) return b; if ((t /= d / 2) == 2) return b + c; if (!p) p = d * (.3 * 1.5);
        if (a < Math.abs(c)) { a = c; var s = p / 4; }
        else var s = p / (2 * Math.PI) * Math.asin(c / a);
        if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
        return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
    },
    easeInBack: function(x, t, b, c, d, s) {
        if (s == undefined) s = 1.70158;
        return c * (t /= d) * t * ((s + 1) * t - s) + b;
    },
    easeOutBack: function(x, t, b, c, d, s) {
        if (s == undefined) s = 1.70158;
        return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
    },
    easeInOutBack: function(x, t, b, c, d, s) {
        if (s == undefined) s = 1.70158;
        if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
        return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
    },
    easeInBounce: function(x, t, b, c, d) {
        return c - jQuery.easing.easeOutBounce(x, d - t, 0, c, d) + b;
    },
    easeOutBounce: function(x, t, b, c, d) {
        if ((t /= d) < (1 / 2.75)) {
            return c * (7.5625 * t * t) + b;
        } else if (t < (2 / 2.75)) {
            return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
        } else if (t < (2.5 / 2.75)) {
            return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
        } else {
            return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
        }
    },
    easeInOutBounce: function(x, t, b, c, d) {
        if (t < d / 2) return jQuery.easing.easeInBounce(x, t * 2, 0, c, d) * .5 + b;
        return jQuery.easing.easeOutBounce(x, t * 2 - d, 0, c, d) * .5 + c * .5 + b;
    }
});

