(function($) {
    $.fn.doSearchnBrowse = function($params) {
        var newMethods = {
            initiate: function() {
                $('.three-titles-box select').widgetDropdownAjaxClick();
	            $('.box_title form select').submitForm();
                $('#recs a.cover_link').hoverDetails('recs');
                $('.three-titles-box a.cover_link').hoverDetails();
            },

			submitForm:function(){
			    $(this).each(function () {
	                        $(this).change(function () {
	                    	    $(this).parents('form').submit();
                    	        });
			    });
			},

            hideHover: function() {
                $("#hover-details").hide(0, function () {
                    $('#content_layout_AB').css('position','static');
                });
            },

            hoverDetails: function(div_type) {
                var hoverHttpRequest = null;
                var attrTitle = null;
				$(this).attr('title','');

                $(this).hoverIntent( {
                    //sensitivity: 3,
                    interval: 200,
                    over: function() {
                        $.el = $(this);
                        var is_cinema = 0;
                        $(this).parents('.fl_detail_image_area').find('.text_btn a.btn_book').each(function () {
                                is_cinema = 1;
                        });
                        $(this).parents('.fl_detail_image_area').find('ul.text_btn .future_cinema').each(function () {
                                is_cinema = 1;
                        });
                        $('#content_layout_AB').css({'position':'relative','left':'0'});
                        jQuery.each(jQuery.browser, function(i) {
                            if ($.browser.msie) {
                                if ($.browser.version == '6.0') $('#all_wrap select').hide();
                            }
                        });

                       // use showObjects method in here to disable selectboxes 
                        var offset = $.el.offset();
                        if(div_type == 'recs'){
                            if (offset.left <= 539){
                                $('#hover-details div.left_arrow').css({'background-image':'url(/images/css/hoverShadow_left_arrow.gif)','background-position':'0% 20%','background-color':'transparent','background-repeat':'no-repeat'});
                                $('#hover-details').css({ 'position' : 'absolute', 'top':(offset.top - 200 ), 'left':(offset.left - 105 ), 'z-index':90});
                            }else{
                                $('#hover-details div.left_arrow').css({'background-image':'url(/images/css/hoverShadow_right_arrow.gif)','background-position':'100% 20%','background-color':'transparent','background-repeat':'no-repeat'});
                                $('#hover-details').css({ 'position' : 'absolute', 'top':(offset.top - 200 ), 'left':(offset.left - 548 ), 'z-index':90});
                            }
                        }
                        else{
                            $('#hover-details div.left_arrow').css({'background-image':'url(/images/css/hoverShadow_left_arrow.gif)','background-position':'0% 20%','background-color':'transparent','background-repeat':'no-repeat'});
                            $('#hover-details').css({ 'position' : 'absolute', 'top':(offset.top - 200 ), 'left':(offset.left - 49 ), 'z-index':90});
                        }
                        hoverHttpRequest = $.ajax({ 
                            beforeSend: function() {
                                $("#hover-details").hide();
                            },
                            url: "/widgets/product/hover_details.html",
                            data: 'product_id='+$.el.attr('ref')+'&is_cinema='+is_cinema,
                            success: function(data, textStatus){
                                $("#hover-details").find('div.content').html('');
                                $("#hover-details").find('div.content').html(data);
                            },
                            complete:function(){
                                    $("#hover-details").show();
                            }
                        });
                    },
                    timeout: 0,
                    out: function () {
                        jQuery.each(jQuery.browser, function(i) {
                            if ($.browser.msie) { 
                                if ($.browser.version == '6.0') $('#all_wrap select').show();
                            } 
                        });
                        if (hoverHttpRequest) {
                            hoverHttpRequest.abort();
                            $(this).hideHover();
                        }
                        $(this).hideHover();
                    }
                });
            },

            widgetDropdownAjaxClick: function() {
                $(this).each(function () {
                        $(this).bind("change",function () {
                            $('.three-titles-box a.cover_link').unbind();
                            $(this).widgetDropdownAjaxLoad();   
                        });
                });
            },

            widgetDropdownAjaxLoad: function() {
                var selected = new Array();
                var selected_facets = new Array();
                var ajax_add = $(this).attr('value');
                    $(this).parents('form').find(':hidden:not(:submit)').each(function(i){
                        selected_facets.push($(this).attr('value'));                    
                        selected.push(new Array($(this).attr('name'),$(this).attr('value')));   
                    });
                var title = $(this).parents('.three-titles-box').find('h2').eq(0).text();
                var genre = $(this).text();
                $(this).parents('.three-titles-box').find('a').eq(0).focus();
                    $(this).parents('.three-titles-box').fadeTo(0, 0.5, function() {
                        $(this).load('/ajax/search_browse/three_titles.html', {
                                selected_f: selected,
                                selected_facets: selected_facets,
                                ajax_add: ajax_add,
                                title: title
                                            }, function() {
                                                $(this).fadeTo(0,1);
                                                $('.three-titles-box a.cover_link').hoverDetails();
                                                $('.three-titles-box select').widgetDropdownAjaxClick();
                                                $(this).find('h2').text( title ); // this overwrites the title passed through - specifically to fix i18n utf8 char issues
                        });
                    });
            }

        };

        $.each(newMethods, function(i) {
            $.fn[i] = this;
        });

        return this.each(function() {
            $this = $(this);
        });
    };

})(jQuery);
