﻿/*
 * Common JavaScript file for Amabassador Theatre Group
 */


//stop iframe
	<!--
		if (top.location!= self.location) {
			top.location = self.location.href
		}
	//-->

// Fix for jquery's buggy IE6 detection, and IE7's buggy version string
if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && this.XMLHttpRequest) {
    jQuery.browser.version = "7.0";
}

function commonInit() {

    var test = "http://at.office.winonaesolutions.net/getveunes.aspx?height=340&width=640";
    var test2 = queryString(test);
    //console.log(queryString(test));

    // Set current nav link to blue
    $(".nav_top li.current").each(function(){
        var classes = $(this).attr("class").split(" ");
        $(this).addClass(classes.join("-"));
    });

    // Prevent errors writing to the console in non-Firebug browsers

    //if(!window.console) {console={ log:function(){} }   };

    $("#adbanner").css("visibility","visible");
    
    $(".tabs").tabs();
    
   $('#jqmDialog').jqm({
        modal: true,
        //trigger: ".modal",
        closeClass: "jqmClose",
        onShow: dialogShow,
        onHide: dialogClose
    });
    
    // If the textbox has a title attribute, add that inside the box
    $(":text").labelify({ labelledClass: "blur" });
    
    // Enable modals
    $('.modal').livequery('click',function(){
        var w = "";
        var h = "";
        var t = $(this).attr("href");
        qs = queryString(t);
        if(qs.width) {
            w = qs.width;
        }
        if(qs.height) {
            h = qs.height;
        }
        dialogLoad(t,w,h);
        return false;
    });
    
    // Collapseable menus
	
	// Find each JS link
	$(".menu li > a").each(function(){
	
		// Is it a JS only link?
		if(!$(this).attr('href')) {
			
			// Add the bold class to the link
			$(this).addClass("bold");
			// Add a class to it's parent to tell is this li is JS controlled
			$(this).next("ul").addClass("js");	
			
		}
		
		// Add a class to the link's parent li if it's open
		if($(this).hasClass("open")) {
			$(this).next("ul").addClass("open");
		}
		
		// click event
		$(this).click(function(){
			
			// If the link is a JS one
			if($(this).hasClass("bold")) {
				// Close the other lists
				$(this).parent("li").siblings().find("> ul.open").removeClass("open").slideUp("fast");
				// Open the cliked link's list
				$(this).next("ul").addClass("open").slideDown("fast");
			}
		
		});
		
	});
	
	// Hide all the lists that are not open
	$(".menu li ul").not(".open").hide();
    
    // AJAX autocomplete search 
    
    ajaxSearchOptions = {
        delay:10,
        minChars:3,
        matchSubset:1,
        matchContains:1,
        cacheLength:10,
        onFindValue:findValue,
        maxItemsToShow:20,
        width:240,
        autoFill:true
    };
//    $("#ctl00_PageHolder_HeaderHolder_fldKeywords").autocomplete("/ajax_search.aspx",ajaxSearchOptions);
//    $("#ctl00_PageHolder_ContentHolder_Main_ctl00_fldProductionSearch").autocomplete("/ajax_search.aspx", ajaxSearchOptions);
////    $("#ctl00_PageHolder_ContentHolder_Main_txtSearch").autocomplete("/ajax_search.aspx", ajaxSearchOptions);
}

function drawResults(data) {
    //remove loading

   $("#listholder").html(data);
}

/* General functions */

//jQuery function for preloading images

jQuery.preloadImages = function() {
    var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
    for(var i = a.length -1; i > 0; i--) {
        jQuery("<img/>").attr("src", a[i]);
    }
}

var preload = [ "/images/popups/corners.png", "/images/popups/corners.gif", "/images/popups/close.gif" ];

$.preloadImages(preload);

/********************* Dialog box functions ******************/

// Load dialog
function dialogLoad(url,w,h) {
    $.ajax({
        type: "GET",
        url: url,
        cache: true,
        error: ajaxError,
        success: function(data) {
            // Add the content
            $("#jqmDialog").find(".jqmContent").html(data)
            // Set dimensions
            if (w) {
                $("#jqmDialog").css("width",w + "px");
            }
            if (h) {
                $("#jqmDialog").css("height",h + "px");
            }
            // Open the dialog
            $("#jqmDialog").jqmShow();
        }
    });
}

// Show the dialog
function dialogShow(dialog) {
    // IE6 specific
    if($.browser.msie && jQuery.browser.version == 6) {
        // Select boxes are in window mode in IE6, so turn off their invisibility
        $("select").css("visibility","hidden");
        $(dialog.w).find("select").css("visibility","visible");
    } else {
        // Disable body scrolling
        $("body").css("overflow","hidden");
    }
    
    // Center. Top must be set to 50% in CSS
    dialog.w.css("left","50%");
    dialog.w.css("margin-left",0 - dialog.w.width()/2);
    dialog.w.css("margin-top",0 - dialog.w.height()/2);        
    
    // Show the dialog
    dialog.w.show();
    dialog.o.css("cursor","default");
    
    // Set the escape key event
    $(document).keydown(dialogEscape);
}

// This function loads the href of the trigger link using AJAX
function dialogOpen(dialog) {
    // IE6 specific
    if($.browser.msie && jQuery.browser.version == 6) {
        // Select boxes are in window mode in IE6, so turn off their invisibility
        $("select").css("visibility","hidden");
        $(dialog.w).find("select").css("visibility","visible");
    } else {
        // Disable body scrolling
        $("body").css("overflow","hidden");
    }
    // Scan the link and find out the dimensions
    dialogQstring = queryString(dialog.t.href);
    $.ajax({
        type: "GET",
        url: dialog.t.href,
        cache: true,
        success: function(data) {
            // Add the content
            dialog.w.find(".jqmContent").html(data)
            // If width or height are set... use them
            if (dialogQstring.width) {
                dialog.w.css("width", dialogQstring.width + "px");
            } else {
                dialog.w.css("width", "auto");
            }
            if (dialogQstring.height) {
                dialog.w.css("height", dialogQstring.height + "px");
            } else {
                dialog.w.css("height", "auto");
            }
            // Center. Top must be set to 50% in CSS
            dialog.w.css("left", "50%");
            dialog.w.css("margin-left", 0 - dialog.w.width() / 2);
            dialog.w.css("margin-top", 0 - dialog.w.height() / 2);

            // Show the dialog
            dialog.w.show();
            dialog.o.css("cursor", "default");
            // Set the escape key event
            $(document).keydown(dialogEscape);
        }
    });
}


// Press escape to close the window
function dialogEscape(e) {
   if( e.which == 27) {
     $(".jqmWindow").jqmHide(); 
   }
}

// Close the dialog window 
function dialogClose(dialog) {
    dialog.w.hide();
    dialog.o.fadeOut(100,function(){dialog.o.remove();});
    $(document).unbind('keydown',dialogEscape);
    // IE6
    if($.browser.msie && jQuery.browser.version == 6) {
        $("select").css("visibility","visible");
    } else {
        $("body").css("overflow","auto");
    }
}

/********************** AJAX auto complete search ****************/

function findValue(li) {
	if( li == null ) return alert("No match!");
	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
	alert("The value you selected was: " + sValue);
}

function lookupAjax(){
	var oSuggest = $("#ctl00_PageHolder_HeaderHolder_fldKeywords")[0].autocompleter;
	oSuggest.findValue();
	return false;
}

function lookupAjax(){
	var oSuggest = $("#ctl00_PageHolder_ContentHolder_Main_ctl00_fldProductionSearch")[0].autocompleter;
	oSuggest.findValue();
	return false;
}

/***************** Some general functions ********************/

// Parse the query string into an object
function queryString(url) {
	var result = {};
    var querystring = url.split("?")[1]; // Extract the query string

	// If it's empty... return an empty list
	if (!querystring)
		return result;

	var pairs = querystring.split("&");
	var splitPair;

	// Load the key/values of the return collection
	for (var i = 0; i < pairs.length; i++) {
		splitPair = pairs[i].split("=");
		result[splitPair[0]] = splitPair[1];
	}
	return result;
}

function ajaxError(XMLHttpRequest, textStatus, errorThrown) {
    alert("There was an error loading the dialog");
}


/* Bind the ready event to the init function */

$(document).ready(commonInit);



