Array.prototype.contains = function(element) {
    return $.inArray(element, this) != -1;
};

Array.prototype.remove = function(elementToRemove) {
    return $.grep(this, function(element, index) {
        return (element !== elementToRemove);
    });
};

var URLS = {};

var $U = {
    applyCufon : function() {
//        if(!navigator.userAgent.match(/ipod|iphone|android/i)){
            Cufon.replace('h1,.applyHeadingCufon,.navigation span', {fontFamily:"C4CufonHeading"});
            Cufon.replace('.applyTextCufon', {fontFamily:"C4CufonText"});
            Cufon.replace('#caraHeader, .componentTitle, .moduleTitle, .successMessage .title, .moduleID-AT01 h3, .moduleID-SN04 h4,.moduleID-AT03 h3,.moduleID-AT05 h3,.moduleID-LM07 h4, .moduleID-LM01 h3', {fontFamily:"C4CufonText"});
//        }
    },

    refreshCufon: function() {
        Cufon.refresh();
    },

    redirectTo: function(url) {
        window.location.href = url;
    },

    disableClickOn: function(jQueryElement) {
        jQueryElement.unbind('click');
        jQueryElement.click(function(event) {
            event.preventDefault();
        });
    },

    showJsVisibleElements: function() {
        $('.jsVisible').removeClass("jsVisible");
    },

    addPlaceholderText: function(element, placeholderText) {
        var supports_input_placeholder = function () {
            var i = document.createElement('input');
            return 'placeholder' in i;
        };

        if(!element || element.length==0)
            return;
        if(supports_input_placeholder()) {
            element.attr("placeholder", placeholderText);
        }
        else{
            if (element.val().length == 0) {
                element.addClass('placeholderText');
                element.val(placeholderText).focus(function() {
                    $(this).val("");
                    $(this).unbind("focus");
                    $(this).removeClass('placeholderText');
                });
                element.parents('form').submit(function(event){
                    if(element.hasClass('placeholderText')){
                        element.val('');
                    }
                });
            }
        }
    }
};

var V4 = {};

V4.Accordion = function() {
    this.init = function(selector, effect) {
        var blockTitleElement = $(selector).find('.expandable h2');
        var collapseOtherActiveBlock = function() {
            $(selector).find('.expandable h2.active').siblings('.module').attr("aria-hidden","true").hide(effect);
            blockTitleElement.removeClass('active');
        };

        $(selector).find('.expandable .module').attr("aria-hidden","true").hide();
        blockTitleElement.removeClass('active');

        blockTitleElement.click(function() {
            if ($(this).hasClass('active'))
                return;
            collapseOtherActiveBlock();
            $(this).addClass('active');
            $(this).siblings('.module').attr("aria-hidden","false").show(effect);
        });
    };
};

V4.Calender = function() {
    this.init = function() {
        var dateInputElement = $('.moduleID-FM01 .formRow.dateInput input[type=text]');
        var dateFormat = "dd/mm/yyyy";
        dateInputElement.attr("placeholder", dateFormat);
        var api = dateInputElement.dateinput({
            format: dateFormat,
            firstDay: 1,
            speed: 'fast',
            offset:[0,0],
            change: function(){
                $(this).blur();
            }
        });
    };
};

V4.UrlBaseHighlighting = function() {
    var hightlightedLinkCss = "highlightedLink";

    this.init = function(element, requesturl) {
        var links = $(element).find('a');
        links.filter(function(index) {
            return requesturl.match("^" + $(this).attr('href'));
        }).parents('li').last().addClass(hightlightedLinkCss);

    };
};

V4.Navigations = function() {
    this.init = function(selector,effectDuration) {
        var navigationHeaders = $(selector).find('.expandable .header');

        navigationHeaders.not('.highlightedLink').siblings('ul').slideToggle(effectDuration);
        navigationHeaders.click(function() {
            $(this).siblings('.navigationElements').slideToggle(effectDuration);
            $(this).toggleClass("open");
        });
    }
};


V4.Tracking= function() {
    this.trackEvents = function(eventName, eventId, obj) {
        var s = s_gi(self.s_account);
        s.trackingServer = 'webstat.channel4.com';
        s.linkTrackVars='eVar31,events';
        s.linkTrackEvents=eventId;
        s.eVar31='4viewers: ' + eventName;
        s.events=eventId;
        s.tl(obj,'o',s.eVar31);
    };
};

V4.SocialToolbar = function() {
    this.init = function() {

        // Share via Facebook
        $("#FacebookButton").click( function(e){
            new V4.Tracking().trackEvents("Facebook",'event46',this);
            var w = window.open($(this).attr("href"),"viewerssharer","height=640,width=800,menubar=no,toolbar=no");
            e.preventDefault();
        });

        // Share via Twitter
        $("#TwitterButton").click( function(e){
            new V4.Tracking().trackEvents("Twitter",'event46',this);
            var w = window.open($(this).attr("href"),"viewerssharer");
            e.preventDefault();
        });
    }
};

