﻿var current_page = 1;
var search_in_progress = false;
var fancybox_controls = new Array();

function fancybox_control(sender_parent_id, sender_id, image_url, image_name, image_title, url_text, anchor_display_value, is_last_group_member)
{
    this._sender_parent_id = sender_parent_id;
    this._sender_id = sender_id;
    this._image_url = image_url;
    this._image_name = image_name;
    this._image_title = image_title;
    this._url_text = url_text;
    this._anchor_display_value = anchor_display_value;
    this._is_last_group_member = is_last_group_member;
}

$(document).ready(function() {
    RefreshSearchResults();

    //Initialize btnSearch onclick method
    $("#btnSearch").click(function() { Search(null, null); });
});       


function section(section_id, parent_id) {
    if (section_id == null)
        return;

    this.original_section_id = section_id;    
    this.section_id = '#' + section_id;
    this.parent_id = '#' + parent_id;
    
    this.clear = function() { this.section_id = null; this.parent_id = null; };
}

var opened_section = new section(null, null);

function AdjustPaginationHeader(circuitCount, pagination_content, js_script_onload, single_record_found_msg, multiple_records_found_msg) {
    var text = (circuitCount <= 1) ? " " + single_record_found_msg : " " + multiple_records_found_msg;
    $("#result_summary").text(circuitCount + text);
    $("td.pagination").html(pagination_content);

    if (js_script_onload != null)
        eval(js_script_onload);
}

function ShowSection(sender, section_id, scroll_to_section) {
    if (opened_section.section_id != null) {
        toggleSection();
        
        if (opened_section.original_section_id == section_id) {
            opened_section.clear();
            return;
        }
    }

    opened_section = new section(section_id, sender.id);
    toggleSection('#' + scroll_to_section);
}

function toggleSection(scroll_to_section) {
    $(opened_section.parent_id).toggleClass('SELECTED');
    $(opened_section.section_id).toggle();
    if (scroll_to_section != null)
        $.scrollTo(scroll_to_section);    
}

function InitFancyBoxes()
{
    for(var i=0;i<fancybox_controls.length;i++) {        
        var sender_parent_id = fancybox_controls[i]._sender_parent_id;
        var sender_id = fancybox_controls[i]._sender_id;
        var image_url = fancybox_controls[i]._image_url;
        var image_name = fancybox_controls[i]._image_name;
        var image_title = fancybox_controls[i]._image_title;
        var url_text = fancybox_controls[i]._url_text;
        var anchor_display_value = fancybox_controls[i]._anchor_display_value;
        var is_last_group_member = fancybox_controls[i]._is_last_group_member;
        InitFancybox(sender_parent_id, sender_id, image_url, image_name, image_title, url_text, anchor_display_value, is_last_group_member);
    }
}

/// Initializes fancybox behavior so that the associated images will be shown
/// properly.
///
/// This procedure is saving images locally on the Web Server in order to prevent
/// cross-site scripting errors when retrieving images from gammes url outside of
/// the web-site domain's.
function InitFancybox(sender_parent_id, sender_id, image_url, image_name, image_title, url_text, anchor_display_value, is_last_group_member) {
    if (search_in_progress)
        return false;
        
    //Get image id value
    var tmpStr = image_url.substring(image_url.indexOf('imageid'));
    var start_index = tmpStr.indexOf('=') + 1;
    var end_index = tmpStr.indexOf('&');
    var image_id = tmpStr.substring(start_index, end_index);
    if (url_text == null || url_text == 'undefined')
        url_text = "";
        
    //Get image type value
    tmpStr = image_url.substring(image_url.indexOf('imagetype'));
    start_index = tmpStr.indexOf('=') + 1;
    end_index = tmpStr.indexOf('&');
    if (end_index == -1)
        end_index = tmpStr.length;

    var image_type = tmpStr.substring(start_index, end_index);
    
    //Adjust ids for jQuery
    var jquery_sender_id = "a#" + sender_id;
    var jquery_sender_parent_id = "#" + sender_parent_id;

    //Perform async call to the controller.PreloadImage()
    $.getJSON("/Circuit/PreloadImage/" + image_id + "/" + image_type + "/" + image_name
        , null
        , function(result) {
            if (result == null)
                return;
            else if (result.indexOf('ERROR') == 0)
                document.title = result; //alert(result);
            else {
                if (sender_parent_id != null) {
                    //Element is part of a group thus insert the anchor element for the group set to the parent container
                    $("<a href='" + result + "' id='" + sender_id + "' class='group' rel='group' title=\"" + image_title + "\" style='display:" + anchor_display_value + ";'>" + url_text + "</a>").appendTo(jquery_sender_parent_id);

                    if (is_last_group_member) {
                        //Set images group to be shown within a fancyBox 
                        $(jquery_sender_parent_id + ' a').fancybox({
                            'zoomSpeedIn': 300,
                            'zoomSpeedOut': 300,
                            'overlayShow': false
                        });
                    }
                }
                else {
                    //Anchor is not part of a group of images so only adjust anchor's image path to the new local image path
                    $(jquery_sender_id).attr('href', result);
                    //Set image to be shown within a fancyBox 
                    $(jquery_sender_id).fancybox();
                }
            }
        });
}

function RefreshSearchResults(event) {
    search_in_progress = true;
    document.body.style.cursor = "wait";
    window.top.status = 'Traitement en cours...';

    var action = current_page;
    var target_page = current_page;
    
    if (event != null) {
        if ($(event).text() == ">") {
            action = "next";
            target_page++;
        }
        else if ($(event).text() == "<") {
            action = "prev";
            target_page--;
        }
        else {
            action = $(event).text();   //pageno            
            target_page = action;
        }
    }    
    
    //Only if not search for a particular circuit id, in which case controler will be called synchroneously.
    if (window.location.href.toLowerCase().indexOf("/detail/") > -1) {
        var circuit_id = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
        $('#OffersList_details').load("/Circuit/ucCircuitTemplate_Refresh_ByCircuitId/" + circuit_id, function() { $("div.otherpage").click(function() { RefreshSearchResults(this); }); document.body.style.cursor = 'default'; window.status = 'Terminé!'; });
    }
    else
        Search(target_page);
}
