jQuery.noConflict();

jQuery(function(){
    jQuery('ul.sf-menu').superfish({ 
        animation:   {height:'show'},
        dropShadows: false,
        speed:	'fast'
    }); 
    jQuery('ul.sf-menu2').superfish({ 
        animation:   {height:'show'},
        dropShadows: false,
        speed:	'fast',
        onBeforeShow:		 function (){
        	jQuery('ul.mcol3').slideUp('fast'); 
        }
    }); 

    make_media_type_radio_work();
    make_mcol3_work();
    make_search_options_work();
    make_country_select_work();
}); 

jQuery(
    function() {
        make_select_all_links_work();
        make_refine_picky();
        make_refine_removal_work();
        untick_all_if_necessary();
        make_motion_sensitive()
        accept_terms();
        make_all_untick_work();
        empty_text_field();
    }
);

function empty_text_field() {
    jQuery("form#newsletter input#email").click(
        function() {
            jQuery(this).attr('value', '');
        }
    );
}

function accept_terms() {
    jQuery("form#newsletter input#terms_check").click(
        function() {
            if (! jQuery(this).attr('checked') ) {
                jQuery("form#newsletter input#subscribe_btn").hide("slow");
            }
            else {
                jQuery("form#newsletter input#subscribe_btn").addClass("subscribe_btn_return");
                jQuery("form#newsletter input#subscribe_btn").css("display", "inline");
                jQuery("form#newsletter input#subscribe_btn").show("fast");
            }
        });
}


function make_motion_sensitive() {
    var regEx= /(motion|motionindex\.html)$/;
    if ( location.pathname.match(regEx) ) {
        jQuery("form#search input:radio[value='image']").attr('checked', 0);
        jQuery("form#search input:radio[value='motion']").attr('checked', 1);
    }
}


function make_media_type_radio_work() {
    jQuery('input[name=media_type]:radio').click(
        function() {
            //Which one did they click?
            if(jQuery(this).val() == 'image') {
                jQuery('ul#image_category_select').show();
                jQuery('ul#motion_category_select').hide();
                jQuery('ul#image_format_filter').show();
                jQuery('ul#motion_format_filter').hide();
            } else {
                jQuery('ul#motion_category_select').show();
                jQuery('ul#image_category_select').hide();
                jQuery('ul#motion_format_filter').show();
                jQuery('ul#image_format_filter').hide();
            }
        }
    );
}

// Initialise country list jQuery
function make_mcol3_work() {
    jQuery('ul.mcol3').bgiframe();
    jQuery('ul.mcol3').hide();
    jQuery('a#close_countries').click(
        function() {
            jQuery('ul.mcol3').slideUp('fast');     
        }
    );
}

function make_search_options_work() {
    jQuery('#search_options').hide();
    jQuery('#search_options').bgiframe();
    add_search_options_hook();
    add_search_options_close_hook();
}

function add_search_options_hook() {
    jQuery('input[name=keywords]:text,input[name=searchstring]:text').click(
        function() {
            jQuery('div#search_options:hidden').slideDown('medium');
        }
    );
}

function add_search_options_close_hook() {
    jQuery('img#closeSearch').click(
        function() {
            jQuery('div#search_options:visible').slideUp('fast');
        }
    );
}

function make_country_select_work() {
    jQuery('li#n_country').click(
        function() {
            jQuery('ul.mcol3').show();
            multiColumnLayoutAll();
            jQuery('ul.mcol3').hide();
            jQuery('ul.sf-menu2').hideSuperfishUl();
            jQuery('ul.mcol3').slideDown('medium',multiColumnLayoutAll);
        }
    );
}

        


// Multicolumn layout for country select

function multiColumnLayout(olElem, nCols) {
    var items = olElem.getElementsByTagName('li');
    var colWidth = olElem.clientWidth/nCols;
    var colLen = Math.max(1, Math.ceil(items.length / nCols));
    var hAcc = 0, hOff = 0, olHeight = 0;
    var rowCount = 0, colCount = 0;
    var colHeight = 0;
  
    for (var i=0; i<items.length; i++) {
        
        if(rowCount>=colLen){
            rowCount = 0;
            colCount++;
        }
        
        if(rowCount>0){
            colHeight = items[i-1].clientHeight;    
        } else {
            colHeight = 0;          
        }
        
        if(colCount<1){
            olHeight+=colHeight;
        }
        
        var c = items[i];
        var m = Math.floor(i / colLen);
        
        c.style.left = ((colCount*colWidth)+13)+'px';
        c.style.top = (rowCount*colHeight)+'px';
        
        rowCount++;
    }
    //olHeight = Math.max(olHeight, hAcc-hOff);
    //
    olElem.style.height = (olHeight+13) + 'px';
}

function multiColumnLayoutAll() {
    for (var nc=2; nc<5; nc++) {
        var cls = 'mcol'+nc;
        var lists = document.getElementsByClassName(cls);
        for (var i=0; i<lists.length; i++) {
            multiColumnLayout(lists[i], nc);
        }
    }
}

if (document.getElementsByClassName==null) {
    document.getElementsByClassName = function(cl) {
        var retnode = [];
        var myclass = new RegExp('\\b'+cl+'\\b');
        var elem = this.getElementsByTagName('*');
        for (var i = 0; i < elem.length; i++) {
            var classes = elem[i].className;
            if (myclass.test(classes)) retnode.push(elem[i]);
        }
        return retnode;
    }; 
}

window.onresize = function() {
  multiColumnLayoutAll();
}

window.onload = function() {
  multiColumnLayoutAll();
}
// EOF Multicolumn

/* dev team changes */
/* Updated 20090920 */

jQuery(
    function() {
        make_select_all_links_work();
        make_all_untick_work();
        make_refine_picky();
        make_refine_removal_work();
        untick_all_if_necessary();
        make_media_type_sensitive();
        accept_terms();
        empty_text_field();
        make_channels_sensitive();
    }
);

function empty_text_field() {
    jQuery("form#newsletter input#email").click(
        function() {
            jQuery(this).attr('value', '');
        }
    );
}

function accept_terms() {
    jQuery("form#newsletter input#terms_check").click(
        function() {
            if (! jQuery(this).attr('checked') ) {
                jQuery("form#newsletter input#subscribe_btn").hide("slow");
            }
            else {
                jQuery("form#newsletter input#subscribe_btn").addClass("subscribe_btn_return");
                jQuery("form#newsletter input#subscribe_btn").css("display", "inline");
                jQuery("form#newsletter input#subscribe_btn").show("fast");
            }
        });
}

function make_media_type_sensitive() {
    var regEx= /\/(motion\/|motionindex\.html)/;
    if ( location.pathname.match(regEx) ) {
        jQuery("form#search input:radio[value='image']").attr('checked', 0);
        jQuery("form#search input:radio[value='motion']").attr('checked', 1);
    }
}

function make_channels_sensitive() {
    var regEx = /\/channel\/(\w+)\.html/;
    var res = location.pathname.match(regEx);
    if (res) {
        var str = res[1]+'_channel';
        jQuery("form#search input:checkbox[value='all']").attr('checked', 0);
        if (res[1] == 'technoscience') {
            jQuery("form#search input:checkbox[value=2]").attr('checked', 1);
        }
        else {
            jQuery("form#search input#"+str).attr('checked', 1);
        }
    }
}

function make_refine_picky() {
    /* submit a subcategory search only if selection is a real value */
    jQuery('select#refine_results').change(
        function(){
            if (jQuery(this).val() > 0) {
                jQuery('form#search').submit();
            }
        }
    );
}

function make_refine_removal_work() {
    /* on searching, remove refine/subcategory data */
    jQuery('form#search #search_btn').click(
        function(){
            jQuery("form#search input:hidden[name='oldsearchstring']").remove();
            jQuery("form#search input:hidden[name='subcat']").remove();
            jQuery("form#search input:hidden[name='license']").remove();
            jQuery("form#search").submit();
        }
    );
}

function make_all_untick_work() {
    hook_channel_unticker("form#search input:checkbox[name='image_channel']");
    hook_channel_unticker("form#search input:checkbox[name='motion_channel']");
}

function hook_channel_unticker(path) {
    jQuery(path).click(
        function() {
            if(jQuery(this).val() == 'all') {
                jQuery(path).each(
                    function() {
                        jQuery(this).attr('checked',false);
                    }
                );
                jQuery(path + "[value='all']").attr('checked',true);
            } else {
                jQuery(path + "[value='all']").attr('checked',false);
            }
       }
    );
}
function make_all_toggler(path,val) {
    return function() {
        jQuery(path).each(
            function(){
                jQuery(this).attr('checked',val);
            }
        );
    };
}

function untick_all_if_necessary() {
    untick_all_for_type("form#search #image_category_select input:checkbox[name='image_channel']");
    untick_all_for_type("form#search #motion_category_select input:checkbox[name='image_channel']");
}

function untick_all_for_type(path) {
    var boxes = jQuery(path);
    var all = boxes.filter("[value='all']");
    var rest = boxes.not("[value='all']");
    if (rest.filter("[checked='checked']").size() > 0) {
        all.removeAttr('checked');
    } else {
        all.attr('checked','checked');
    }
}

function make_select_all_links_work() {
    /* select all visible images. Use class not id as this happens twice on page */
    jQuery("form#search_results a.select_all").click(
        make_all_toggler("form#search_results input:checkbox",true)
    );
    /* This is for the lightbox area */
    jQuery("form#lightbox_manager a.select_all").click(
        make_all_toggler("form#lightbox_manager input:checkbox",true)
    );
    /* deselect all visible images. Use class not id as this happens twice on page */
    jQuery("form#search_results a.deselect_all").click(
        make_all_toggler("form#search_results input:checkbox",false)
    );
    jQuery("form#lightbox_manager a.deselect_all").click(
        make_all_toggler("form#lightbox_manager input:checkbox",false)
    );
}
