/* Setting application options.  This is mostly used for specifying which modules are to be enabled.
 * We set everything to "true" by default, mainly for compatibility reasons.
 *
 * Current relevant arguments (all values are either "true" or "false" unless otherwise specified):
 *
 *		debug					- Console output and debugging features
 *		gta 					- Go To Anywhere
 *		garbageCollection		- Garbage Collection for main search results
 *		dragToLightbox			- Drag of thumbnail popup to lightbox
 *		userHistoryFlag			- User History
 *		squareSize				- The size, in pixels, of the width of the result image thumbnails.
 *		colSize					- The number of result images per column of results.
 *
 */
function Application(options) {
	// Set the defaults.
	this.gta = true;
	this.garbageCollection = true;
	this.userHistoryFlag = true;
    this.debug = true;
	this.gcDebug = false;

    this.colSize = 3;
    this.pageName = "search_results";
    this.em_version = 1;

    this.locale = "en_EN";

    /* Buffer sizes: expressed as a percentage of the window width.
	 *
	 *		1   = one window width on either side (so 3 window widths' total will be fetched).
	 *		2   = two window widths on either side (so 5 window widths' total will be fetched).
	 *		0.5 = one-half window width on either side (so 2 window widths' total will be fetched).
	 *
	 * This buffer size is used when calculating the visible window using resetVisibility().  It is used
	 * to fetch results for GTA, Auto Browse, and Manual Browse.
	 */
	this.bufferSize = 1;
	/* The default fetch size is only needed when fetching results for Scroll.
	 */
	this.defaultFetchSize = 60;

	// The dimensions for the Lightbox and Cart drop target areas, when they are open.
	this.lbCartHeight = 200;
	this.lbCartWidth = 420;

	// This is the center point of the LB/Cart area: the right edge of the Cart, and the left edge
	// of the Lightbox.  We need this because we animate the Cart's "left" value to get it to open
	// from right to left.
	this.lbCartCentre = 542;

    //image server
    this.imageServerURL = "http://image1.masterfile.com/";

    if ( screen.width > 1024 ) {
        this.em_version = 1;
    } else {
        this.em_version = 2;
    }

    // Override the defaults with the specified values.
	for (var prop in options){
		this[prop] = options[prop];
    }    

    this.em_version = getCookie("em_version");
    if ( this.em_version == null ) {
        // EM-1387 : for screens higher than 1024x768, the default grid mode will be "full aspect"
        if ( screen.width > 1024 ) {
            this.em_version = 3;
        } else {
            this.em_version = 2;
        }
        if ( $("em_version") ) {
            $("em_version").value = this.em_version;           
        }
    }

    this.minWidthFeature = this.squareSize * 4;
    this.thumbPrefix = "et";     //	 141	et
    this.simThumbPrefix = "et";  //	 141	et
    this.thumbRolloverPrefix = "er";     //	 200	er
    if ( this.em_version == 1) {
        this.squareSize = 140;
        this.simSquareSize = 140;
        this.thumbPrefix = "et";        //	 141	et
        this.simThumbPrefix = "et";     //	 141	et

        this.minWidthFeature = this.squareSize * 4;

        if ( this.pageName == "viewlightbox" ) {
            this.squareSize = 240;
            this.thumbPrefix = "er";        //	 200	er
            this.simThumbPrefix = "et";     //	 141	et

            this.minWidthFeature = this.squareSize * 2;

            document.write("<STYLE>.result_image{ width: " + this.squareSize + "px;	height: " + (this.squareSize-40) + "px; position: absolute; background-position: bottom center; margin: 0px; border: 0px;}    </STYLE>");
            document.write("<STYLE>.img_lb{ width: "+this.squareSize+"px; height: "+this.squareSize+"px; background-position: center center; margin: 0px; border: 0px;} </STYLE>");
            document.write("<STYLE>.search_results a:link{ border: 0px } </STYLE>");
            document.write("<STYLE>#search_results_wrapper{ height: " + (this.squareSize * this.colSize) + "px; } </STYLE>");
            document.write("<STYLE>#simsearch_wrapper{ top: " + (this.squareSize * this.colSize) + "px;  }    </STYLE>");
            document.write("<STYLE>#search_results{ height: " +(this.squareSize * this.colSize) + "px; } </STYLE>");
        } else {
            document.write("<STYLE>.result_image{ width: " + this.squareSize + "px; height: " + this.squareSize + "px; </STYLE>");
            document.write("<STYLE>#search_results_wrapper{ height: " + (this.squareSize * this.colSize + 4) + "px; } </STYLE>");
            document.write("<STYLE>#simsearch_wrapper{ top: " + (this.squareSize * this.colSize) + "px;  } </STYLE>");
        }

        document.write("<STYLE>#blur{ height: " + (this.squareSize * this.colSize + 10) + "px; } </STYLE>");
        document.write("<STYLE>#search_results_a{  z-index: 8; } </STYLE>");
        document.write("<STYLE>#search_results_b{  z-index: 7; } </STYLE>");

        document.write("<STYLE>.sim_result_image{ width: " + this.simSquareSize + "px; height: " + this.simSquareSize + "px; margin: 0px; border: 0px}    </STYLE>");
        document.write("<STYLE>#feature_ss_action_bar{  width: "+ (this.simSquareSize-2) + "px; height: " + (this.simSquareSize+2) + "px;}    </STYLE>");

    }  if ( this.em_version == 2) {
        this.squareSize = 93;
        this.simSquareSize = 93;
        this.thumbPrefix = "ft";            //	 93 	ft
        this.simThumbPrefix = "ft";         //	 93	    et
        this.thumbRolloverPrefix = "t";     //	 150	t
        this.colSize = 3;

        this.minWidthFeature = this.squareSize * 5;

        if ( this.pageName == "viewlightbox" ) {
            this.squareSize = 190;
            this.thumbPrefix = "t";    //	 150 	t
            this.colSize = 2;
            this.minWidthFeature = this.squareSize * 2;

            document.write("<STYLE>.result_image{ width: " + this.squareSize+"px;	height: " + (this.squareSize-40) + "px; position: absolute; background-position: bottom center; margin: 0px; border: 0px;}    </STYLE>");
            document.write("<STYLE>.img_lb{ width: " + this.squareSize+"px; height: " + this.squareSize + "px; background-position: center center; margin: 0px; border: 0px;}    </STYLE>");
            document.write("<STYLE>.search_results a:link{ border: 0px } </STYLE>");
            document.write("<STYLE>#search_results_wrapper{ height: " + (this.squareSize * this.colSize) + "px; } </STYLE>");
            document.write("<STYLE>#simsearch_wrapper{ top: " + (this.squareSize * this.colSize) + "px;  } </STYLE>");
            document.write("<STYLE>#search_results{ height: " + (this.squareSize * this.colSize) + "px; }    </STYLE>");
        } else {
            document.write("<STYLE>.result_image{ width: " + this.squareSize + "px; height: " + this.squareSize + "px; </STYLE>");
            document.write("<STYLE>#search_results_wrapper{ height: " + (this.squareSize * this.colSize + 4) + "px; } </STYLE>");
            document.write("<STYLE>#simsearch_wrapper{ top: " + (this.squareSize * this.colSize) + "px; } </STYLE>");
        }

        document.write("<STYLE>#blur{ height: " + (this.squareSize * this.colSize + 10) + "px; } </STYLE>");
        document.write("<STYLE>#search_results_a{  z-index: 8; } </STYLE>");
        document.write("<STYLE>#search_results_b{  z-index: 7; } </STYLE>");

        document.write("<STYLE>.sim_result_image{ width: " + this.simSquareSize + "px; height: " + this.simSquareSize + "px; margin: 0px; border: 0px} </STYLE>");
        document.write("<STYLE>#feature_ss_action_bar{  width: "+ (this.simSquareSize - 2) + "px; height: " + (this.simSquareSize + 2) + "px;} </STYLE>");
    }  if ( this.em_version == 3) {
        this.squareSize = 160;
        this.simSquareSize = 160;
        this.thumbPrefix = "t";              //	 150	t
        this.simThumbPrefix = "t";           //	 150	t
        this.thumbPrefix = "t";              //	 150	t
        this.thumbRolloverPrefix = "er";     //	 200	er
        this.colSize = 3;

        this.minWidthFeature = this.squareSize * 4;
        /* POC to make 2 rows search results.
        if ( screen.width <= 1024 ) {
            this.minWidthFeature = this.squareSize * 3;
            this.colSize = 2;
        }
        */
        if ( this.pageName == "viewlightbox" ) {
            this.squareSize = 190;
            this.thumbPrefix = "t";         //	 150	t
            this.simThumbPrefix = "t";      //	 150	t
            this.colSize = 2;

            this.minWidthFeature = this.squareSize * 3;

            document.write("<STYLE>.result_image{    width: "+this.squareSize+"px;	height: "+(this.squareSize-40)+"px; position: absolute; background-position: bottom center; margin: 0px; border: 0px;} </STYLE>");
            document.write("<STYLE>.img_lb{    width: "+this.squareSize+"px;	height: "+this.squareSize+"px; background-position: center center; margin: 0px; border: 0px;} </STYLE>");
            document.write("<STYLE>#search_results_wrapper{   height: "+(this.squareSize * this.colSize)+"px; } </STYLE>");
            document.write("<STYLE>#simsearch_wrapper{    top: "+(this.squareSize * this.colSize)+"px;  } </STYLE>");
            document.write("<STYLE>#search_results{   height: "+(this.squareSize * this.colSize)+"px; } </STYLE>");
        } else {
            document.write("<STYLE>.result_image{  width: "+this.squareSize+"px; height: " + this.squareSize + "px; background-position: center center; margin: 0px; border: 0px;} </STYLE>");
            document.write("<STYLE>#search_results_wrapper{   height: "+(this.squareSize * this.colSize+3)+"px } </STYLE>");
            document.write("<STYLE>#simsearch_wrapper{    top: "+(this.squareSize * this.colSize)+"px; } </STYLE>");
            document.write("<STYLE>#search_and_simsearch{    height: "+  (this.squareSize * this.colSize + 32 + this.simSquareSize )  +  "px; } </STYLE>");
        }

        document.write("<STYLE>#blur{   height: "+(this.squareSize * this.colSize + 10) +"px; } </STYLE>");
        document.write("<STYLE>#search_results_a{  z-index: 8; } </STYLE>");
        document.write("<STYLE>#search_results_b{  z-index: 7; } </STYLE>");

        document.write("<STYLE>#footer_for_em{ margin-top: 65px; } </STYLE>");
        document.write("<STYLE>.search_results a:link{ border: 0px } </STYLE>");
        document.write("<STYLE>#simsearch a:link{ border: 1px solid #a9a9a9; } </STYLE>");
        document.write("<STYLE>.sim_result_image{ width: " + this.simSquareSize + "px; height: " + this.simSquareSize + "px; background-position: center center; margin: 0px; border: 0px;} </STYLE>");
        document.write("<STYLE>#feature_ss_action_bar{  width: "+ (this.simSquareSize-2) + "px; height: " + (this.simSquareSize+2) + "px;} </STYLE>");
    }

    this.featurePrefix = "en";                  //	 450	en
    this.storageType = "watermarked";
    this.setStorageType = function ( storageType ) {
        this.storageType = storageType;
        if ( this.storageType == "watermarked" ) {
            if ( this.em_version == 2 ) {
                this.featurePrefix = "fw";      //	 380W	fw
            } else if (this.em_version == 3) {
                this.featurePrefix = "w";       //	 550W	w
                if ( this.colSize == 2 ) {
                    this.featurePrefix = "fw";  //	 380W	fw
                }
            } else {
                this.featurePrefix = "ew";      //	 450W	ew
            }
        } else {
            if (this.em_version == 2) {
                this.featurePrefix = "fn";      //	 380	fn
            } else if (this.em_version == 3) {
                this.featurePrefix = "n";       //	 550	n
                if ( this.colSize == 2 ) {
                    this.featurePrefix = "fn";  //	 380	fn
                }
            } else {
                this.featurePrefix = "en";      //	 450	en
            }
        }
    }
		
    this.openPanels = [];
	this.userHistory = new MfOrderedHash("desc");
	this.userHistoryIndex = 0;
	
	// Action codes for storing search-triggering events. (EM-517)
	this.events = {
		NEW_SEARCH: ".",
		AUTO_BROWSE_LEFT: "<|<",
		MANUAL_BROWSE_LEFT: "|<",
		MANUAL_BROWSE_RIGHT: ">|",
		AUTO_BROWSE_RIGHT: ">|>",
		GTA: "gta",					// GTA: Go-To-Anywhere, dragging and dropping the slider.
		GTI: "gti",					// GTI: Go-To-Index, entering a position index in the GTI form.
		GTP: "gtp"					// GTP: Go-To-Percentage, entering a position percentage in the GTI form.
	};
	
	// The local history storage for EM-122.
	this.searchDetailHistory = new MfHashtable();

	/** Determines if this is Internet Explorer. */
    this.isInternetExplorer = false;
	if ( BrowserDetect.browser == "Explorer" ) {
		this.isInternetExplorer = true;
    }
	this.isInternetExplorer6 = false;
	if ( BrowserDetect.browser == "Explorer" && BrowserDetect.version=="6" ) {
		this.isInternetExplorer6 = true;
    }

    //EM-1499 store the fact they have already seen the link popup
    this.hasSeenModalFromURL = false;  //false is default
    this.hasEnlargedImageFromURL = false; //false is default
    this.smallThumbEMVersion = 2; //2 is the small squares (the smallest version)
}

function setLastAction(action) {
	if (la = $("lastAction"))
		la.value = action;
}

function getLastAction() {
	if (la = $("lastAction"))
		return la.value;
}

// Adds a history image to the end of the list.
function appendHistoryImage(img) {
	//application.userHistory.push(img);
    alert("appendHistoryImage is not implemented!!!");
}

function initAppHistoryWith(aElemAry){
    for(var i =  (aElemAry.length-1); i > -1; i--){
        insertHistoryImage(aElemAry[i]);
    }
}

// Adds a history image to the beginning of the list.
function insertHistoryImage(img) {
	application.userHistory.set(img.href.getQueryValue("image"),img);
    application.userHistoryIndex = 0; //reset the history index upon image addition
}

function resetUserHistory() {
	application.userHistory = new MfOrderedHash("desc");
	application.userHistoryIndex = 0;
}

function getOlderHistory() {
	if (!hasOlderHistory())
		return false;
	
	debug("[NEXT/PREV] Older: Getting userHistory[" + (application.userHistoryIndex + 1) + "]");
	return application.userHistory.getIdx(++application.userHistoryIndex);
}

function hasOlderHistory() {
	return application.userHistoryIndex < application.userHistory.length() - 1;
}

function getNewerHistory() {
	if (!hasNewerHistory())
	 	return false;

	debug("[NEXT/PREV] Newer: Getting userHistory[" + (application.userHistoryIndex -1) + "]");
	return application.userHistory.getIdx(--application.userHistoryIndex);
}

function hasNewerHistory() {
	return application.userHistoryIndex > 0;
}

function resetHistoryButtons() {
    if ( $("feature_previous") ) {
        if (hasOlderHistory())
    		$("feature_previous").style.display = "block";
    	else
    		$("feature_previous").style.display = "none";
    }

    if ( $("feature_next") ) {
        if (hasNewerHistory())
    		$("feature_next").style.display = "block";
    	else
	    	$("feature_next").style.display = "none";
    }
}

function addOpenPanel(panelID) {
	if (window.application) {
		application.openPanels.push(panelID);
	}
}

function closeAllPanels(excludeFeaturePanel) {
	if (window.application) {
		if ( window.openToolbarContent && !(excludeFeaturePanel == openToolbarContent) ) {
		    if ( openToolbarContent.isOpen ) {
		        openToolbarContent.isOpen=false;
			    openToolbarContent.start(0);
			}
        }

        if ( window.openSxDownloadContent && !(excludeFeaturePanel == openSxDownloadContent) ) {
            if ( openSxDownloadContent.isOpen ) {
                openSxDownloadContent.isOpen=false;
			    openSxDownloadContent.start(0);
			}
	    }
        
        var p;
		while (p = application.openPanels.pop()) {
			var q = $(p);
			if (q) panel_slide_in(q);
		}
	}
}


