troa_jquery = jQuery.noConflict();

function fill_dynamic_content() {
    var dynamic_divs = troa_jquery('div.dynamic_content');
    for (var i = 0; i < dynamic_divs.length; i++) {
        var dynamic_div = troa_jquery(dynamic_divs[i]);
        var url = troa_jquery(dynamic_div).attr('url');
        var callback = function(j) {
            return function(data) {
                troa_jquery(dynamic_divs[j]).html(data);
            };
        }(i);
        if (url) {
            troa_jquery.get(url, callback);
        };
    };
};

troa_jquery(document).ready(function() {
    fill_dynamic_content();
    setup_search_result_sort_button();
    troa_jquery('#page_container').pajinate({items_per_page: 12,
                                             nav_panel_id: '#TROAnextprev',
                                             nav_label_prev: '<<< PREV',
                                             nav_label_next: 'NEXT >>>'});
    // TODO move items_per_page to templates
    troa_jquery('#page_container_category_all').pajinate({items_per_page: 10,
                                             nav_panel_id: '#TROAnextprev',
                                             nav_label_prev: '<<< PREV',
                                             nav_label_next: 'NEXT >>>'});
   // TODO move items_per_page to templates
   troa_jquery('#page_container_search_result').pajinate({items_per_page: 8,
                                             nav_panel_id: '#TROAnextprev',
                                             nav_label_prev: '<<< PREV',
                                             nav_label_next: 'NEXT >>>'});
   // TODO move items_per_page to templates
   troa_jquery('#page_container_brochure_bank').pajinate({items_per_page: 16,
                                             nav_panel_id: '#TROAnextprev',
                                             nav_label_prev: '<<< PREV',
                                             nav_label_next: 'NEXT >>>'});

    departing_soon_setup();
    product_tabs_setup();
    brochure_tabs_setup();
    datepickers_setup();
    if(troa_jquery('#TROAnextprev a').size()<6) {
        troa_jquery('#TROAnextprev').hide();
    }

    if(troa_jquery('#mcs_container').length != 0){
        troa_jquery("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10); 
    }
    troa_jquery('.search_submit').live('click', function() {
        setup_search_submit();   
    });
    remove_empty_elements();



});

function compare(x, y, descending, value_getter) {
    if (descending) {
        var a = y;
        var b = x;
    } else {
        var a = x;
        var b = y;
    };
    if (value_getter) {
        a = value_getter(a);
        b = value_getter(b);
    };
    if (a < b) {
        return -1;
    } else if (a > b) {
        return 1;
    } else {
        return 0;
    };
};
   
function compare_products_by_price(descending) {
    return function(x, y) {
        return compare(x, y, descending, function(e) {
            return parseInt(troa_jquery(e).attr('price'));
        });
    };
}

function compare_products_by_name(descending) {
    return function(x, y) {
        return compare(x, y, descending, function(e) {
            return troa_jquery(e).attr('name').toLowerCase();
        });
    };
}

function setup_search_result_sort_button() {
    var setup_sort_button = function(button, other_button, comparer) {
        return function() {
            var state = button.attr('state');
            var products = troa_jquery('.search_elem');
            button.find('a').css('color', '#00A0FF');
            other_button.attr('state', 'off');
            other_button.find('a').css('color', '');
            other_button.next().find('.sort_arrow_pic').css('background-image', '');
            if (state == 'desc' || state == 'off') {
                button.attr('state', 'asc');
                button.next().find('.sort_arrow_pic').css('background-image', 'url(\'/resources/images/asc.png\')');
                products.sortElements(comparer(false));
            } else {
                button.attr('state', 'desc');
                button.next().find('.sort_arrow_pic').css('background-image', 'url(\'/resources/images/desc.png\')');
                products.sortElements(comparer(true));
            };
            return false;
        };
    };
    var sort_by_name_button = troa_jquery('#sort_by_name');
    var sort_by_price_button = troa_jquery('#sort_by_price');
    sort_by_name_button.click(setup_sort_button(sort_by_name_button.parent(), sort_by_price_button.parent(), compare_products_by_name));
    sort_by_price_button.click(setup_sort_button(sort_by_price_button.parent(), sort_by_name_button.parent(), compare_products_by_price));
    // in the beginning, products are sorted by name ascending
    sort_by_name_button.parent().attr('state', 'asc');
    sort_by_name_button.parent().find('a').css('color', '#00A0FF');
    sort_by_name_button.parent().next().find('.sort_arrow_pic').css('background-image', 'url(\'/resources/images/asc.png\')');
    var products = troa_jquery('.search_elem');
    if (products.length != 0) {
   	 products.sortElements(compare_products_by_name(false));
    } 
};

function departing_soon_setup(){
        troa_jquery('#departure_link_this_month').mouseover(function() {
                troa_jquery('#departure_tab_this_month').show();
		troa_jquery('#departure_link_this_month').addClass('tab_hover');
                troa_jquery('#departure_tab_next_month').hide();
		troa_jquery('#departure_link_next_month').removeClass('tab_hover');
                troa_jquery('#departure_tab_two_month').hide();
		troa_jquery('#departure_link_two_month').removeClass('tab_hover');
        });
        troa_jquery('#departure_link_next_month').mouseover(function() {
                troa_jquery('#departure_tab_this_month').hide();
		troa_jquery('#departure_link_this_month').removeClass('tab_hover');
                troa_jquery('#departure_tab_next_month').show();
		troa_jquery('#departure_link_next_month').addClass('tab_hover');
                troa_jquery('#departure_tab_two_month').hide();
		troa_jquery('#departure_link_two_month').removeClass('tab_hover');

        });
        troa_jquery('#departure_link_two_month').mouseover(function() {
                troa_jquery('#departure_tab_this_month').hide();
		troa_jquery('#departure_link_this_month').removeClass('tab_hover');
                troa_jquery('#departure_tab_next_month').hide();
		troa_jquery('#departure_link_next_month').removeClass('tab_hover');
                troa_jquery('#departure_tab_two_month').show();
		troa_jquery('#departure_link_two_month').addClass('tab_hover');
        });

}

function product_tabs_setup(){
        troa_jquery('#TROAproduct_overview_link').live('mouseover', function() {
                troa_jquery('#product_details_content').html(troa_jquery('#TROAproduct_overview').html());
		troa_jquery("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
		troa_jquery('#TROAproduct_overview_link').addClass('tab_hover');
		troa_jquery('#TROAproduct_gallery_link').removeClass('tab_hover');
		troa_jquery('#TROAproduct_supplier_link').removeClass('tab_hover');
		troa_jquery('#TROAproduct_tc_link').removeClass('tab_hover');
		troa_jquery('#TROAproduct_recommended_link').removeClass('tab_hover');
        });
        troa_jquery('#TROAproduct_gallery_link').live('mouseover', function() {
                troa_jquery('#product_details_content').html(troa_jquery('#TROAproduct_gallery').html());
                troa_jquery("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
                troa_jquery('#TROAproduct_overview_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_gallery_link').addClass('tab_hover');
                troa_jquery('#TROAproduct_supplier_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_tc_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_recommended_link').removeClass('tab_hover');

        });
        troa_jquery('#TROAproduct_supplier_link').live('mouseover', function() {
                troa_jquery('#product_details_content').html(troa_jquery('#TROAproduct_supplier').html());
                troa_jquery("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
                troa_jquery('#TROAproduct_overview_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_gallery_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_supplier_link').addClass('tab_hover');
                troa_jquery('#TROAproduct_tc_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_recommended_link').removeClass('tab_hover');

        });
        troa_jquery('#TROAproduct_tc_link').live('mouseover', function() {
                troa_jquery('#product_details_content').html(troa_jquery('#TROAproduct_tc').html());
                troa_jquery("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
                troa_jquery('#TROAproduct_overview_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_gallery_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_supplier_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_tc_link').addClass('tab_hover');
                troa_jquery('#TROAproduct_recommended_link').removeClass('tab_hover');

        });
        troa_jquery('#TROAproduct_recommended_link').live('mouseover', function() {
                troa_jquery('#product_details_content').html(troa_jquery('#TROAproduct_recommended').html());
                troa_jquery('#TROAproduct_overview_link').removeClass('tab_hover');
                troa_jquery('#TROAnextprev').show();
                troa_jquery('#page_container_recommended_products').pajinate({items_per_page: 8,
                                             nav_panel_id: '#TROAnextprev',
                                             nav_label_prev: '<<< PREV',
                                             nav_label_next: 'NEXT >>>'});
                if(troa_jquery('#TROAnextprev a').size()<6) {
        		troa_jquery('#TROAnextprev').hide();
     		}
 		troa_jquery("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
		troa_jquery(".dragger_container").hide();
                troa_jquery(".scrollUpBtn").hide();
                troa_jquery(".scrollDownBtn").hide();
                troa_jquery('#TROAproduct_gallery_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_supplier_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_tc_link').removeClass('tab_hover');
                troa_jquery('#TROAproduct_recommended_link').addClass('tab_hover');
        });

}

function brochure_tabs_setup(){
        troa_jquery('#TROAbrochure_overview_link').live('mouseover', function() {
		troa_jquery('#brochure_details_content').html(troa_jquery('#TROAbrochure_overview').html());
                troa_jquery("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
		troa_jquery('#TROAbrochure_overview_link').addClass('tab_hover');
		troa_jquery('#TROAbrochure_products_link').removeClass('tab_hover');
		troa_jquery('#TROAbrochure_supplier_link').removeClass('tab_hover');
        });
        troa_jquery('#TROAbrochure_products_link').live('mouseover', function() {
                troa_jquery('#brochure_details_content').html(troa_jquery('#TROAbrochure_products').html());	
                troa_jquery('#TROAnextprev').show();
                troa_jquery('#page_container_brochure_products').pajinate({items_per_page: 8,
                                             nav_panel_id: '#TROAnextprev',
                                             nav_label_prev: '<<< PREV',
                                             nav_label_next: 'NEXT >>>'});
                if(troa_jquery('#TROAnextprev a').size()<6) {
                        troa_jquery('#TROAnextprev').hide();
                }
                troa_jquery("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
                troa_jquery(".dragger_container").hide();
                troa_jquery(".scrollUpBtn").hide();
                troa_jquery(".scrollDownBtn").hide();
                
		troa_jquery('#TROAbrochure_overview_link').removeClass('tab_hover');
                troa_jquery('#TROAbrochure_products_link').addClass('tab_hover');
                troa_jquery('#TROAbrochure_supplier_link').removeClass('tab_hover');

	});
        troa_jquery('#TROAbrochure_supplier_link').live('mouseover', function() {
                troa_jquery('#brochure_details_content').html(troa_jquery('#TROAbrochure_supplier').html());
                troa_jquery("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
                troa_jquery('#TROAbrochure_overview_link').removeClass('tab_hover');
                troa_jquery('#TROAbrochure_products_link').removeClass('tab_hover');
                troa_jquery('#TROAbrochure_supplier_link').addClass('tab_hover');
        });
}

function datepickers_setup() {
    troa_jquery('.calendar').datepick({
        dateFormat: 'yyyy-mm-dd',    showTrigger: '<img class="trigger" src="portals/troa/img/icons/calendar.png" style="position: relative; left: 10px;">'
    });
}

function setup_search_submit() {
       	if (troa_jquery('#fullTextSearch').val() == 'find a holiday!') {
		troa_jquery('#fullTextSearch').val('');
	}
        if (troa_jquery('#fullTextSearchAdv').val() == 'find a holiday!') {
                troa_jquery('#fullTextSearchAdv').val('');
        }

}

function remove_empty_elements() {
	troa_jquery('.if_empty_dont_show').each(function(index) {
    		var linked_div_id = troa_jquery(this).attr('linked_div');
		if (troa_jquery('#' + linked_div_id).html() == "") {
			troa_jquery(this).remove();
		}
  	});
}

