﻿/// <reference name="MicrosoftAjax.js"/>
//***********************************************************************************************************************************/
//	jutil.trekk.com.js v0.01
//
//	Author: Trekk Cross-Media
//  Website: http://www.trekk.com
//  StartDate: August 4, 2008
//	License: 
//  Browsers: 
//
//  Releases: 
//				* v0.01 (02/12/08)
//  Trekk jQuery Extensions
//***********************************************************************************************************************************/
var prm="";
var Sys;
$(window).resize(function(){
    var maxWidth, maxHeight;
    if ($.browser.msie) {
       maxWidth=document.documentElement.scrollWidth;
       maxHeight=document.documentElement.scrollHeight;
    } else {
       maxWidth=Math.max($('html').width(), $(window).width()-30);
       maxHeight=Math.max($('html').height(), $(window).height()-30);
    }
   
    $("#modal").height(maxHeight);
    $("#modal").width(maxWidth);
});
    	
$(document).ready(function(){    
//    $("input").each(function(){
//       var $that = $(this);
//       $that.submit(function(){
//          $that.find("input[type='image'],input[type='submit']").attr("disabled", "true");
//       });
//    }); 

    var maxWidth, maxHeight;
    if ($.browser.msie) {
       maxWidth=document.documentElement.scrollWidth;
       maxHeight=document.documentElement.scrollHeight;
    } else {
       maxWidth=Math.max($('html').width(), $(window).width()-30);
       maxHeight=Math.max($('html').height(), $(window).height()-30);
    }
          
    $("#modal").height(maxHeight);    
    $("#modal").width(maxWidth);
                
    $().mousemove(function(e){        
        $("#loading").css("left",e.pageX - 15);
	    $("#loading").css("top",e.pageY - 10);    
    });                                                       
        
//    if ($.jqURL.get("id") != undefined && $.jqURL.get("id") != ""){
//        alert("Query String Value for id is: " + $.jqURL.get("id"));
//    }    
    if (Sys != undefined){
        prm = Sys.WebForms.PageRequestManager.getInstance();   
        prm.add_initializeRequest(initializeRequest);
        prm.add_endRequest(endRequest);             
    }                     
});


$.fn.imageOver = function() {
    return this.each(function(){      
        var objSrc=$(this).attr("src");        
        var ftype = objSrc.substring(objSrc.lastIndexOf("."), objSrc.length);
        var objSrcOver = objSrc.replace(ftype, "-over" + ftype);   
        var objSrcActive = objSrc.replace(ftype, "-active" + ftype);                  
        
//        $(this).parent().append('<img src=\"'+objSrcOver+'\ class=' + 'hide' + ' />');
//        $(this).parent().append('<img src=\"'+objSrcActive+'\ class=' + 'hide' + ' />');
        
        $(this).hover(function(){	   
            $(this).attr("src",objSrcOver);                  
        },function(){
            $(this).attr("src",objSrc);
        });                               
    });    
};	

var _postBackElement;

function initializeRequest(sender, e){
    //alert("Called: initializeRequest");
    if (prm.get_isInAsyncPostBack()){
        e.set_cancel(true);
    }

    _postBackElement = e.get_postBackElement();    
    $("#modal").show(); 
    $("#loading").show();   
    $("input").attr("disabled", true);     
}

function endRequest(sender, e){
    //alert("Called: endRequest");    
    $("#loading").hide();    
    $("#modal").hide(); 
    $("input").attr("disabled", false);    
}

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}

jQuery.preloadCssImages = function(){
        var allImgs = [];//new array for all the image urls  
        var k = 0; //iterator for adding images
        var sheets = document.styleSheets;//array of stylesheets
        
        for(var i = 0; i<sheets .length; i++){//loop through each stylesheet
                var cssPile = '';//create large string of all css rules in sheet
                var csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
                var baseURLarr = csshref.split('/');//split href at / to make array
                baseURLarr.pop();//remove file path from baseURL array
                var baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir)
                if(baseURL!="") baseURL+='/'; //tack on a / if needed
                if(document.styleSheets[i].cssRules){//w3
                        var thisSheetRules = document.styleSheets[i].cssRules; //w3
                        for(var j = 0; j<thisSheetRules.length; j++){
                                cssPile+= thisSheetRules[j].cssText;
                        }
                }
                else {
                        cssPile+= document.styleSheets[i].cssText;
                }
                
                //parse cssPile for image urls and load them into the DOM
                var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g);//reg ex to get a string of between a "(" and a ".filename"
                if(imgUrls != null && imgUrls.length>0 && imgUrls != ''){//loop array
                        var arr = jQuery.makeArray(imgUrls);//create array from regex obj        
                        jQuery(arr).each(function(){
                                allImgs[k] = new Image(); //new img obj
                                allImgs[k].src = (this[0] == '/' || this.match('http://')) ? this : baseURL + this;     //set src either absolute or rel to css dir
                                k++;
                        });
                }
        }//loop
        return allImgs;
} 
//extend string prototype.. implement as follows: alert(mystring.trim());
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
/* End String Manipulation */

function hideCombos(){           
    //alert('hide');
    $("select").hide();    
}

function showCombos(){           
    //alert('show');
    $("select").show();   
}

//pass in desired QS var.
function getQueryStringVariable(variable){ 
    return $.jqURL.get(variable);   
} 

jQuery.jqURL = {

	url : // returns a string
	function(args) {
		args = 
			jQuery.extend({
				win : window
			},
			args);
		return args.win.location.href;
	},
	
	loc : 
	function(urlstr, args) {
		args = 
			jQuery.extend({
				win : window,
				w : 500,
				h : 500,
				wintype : '_top'
			},
			args);
			
		if (!args.t) {
			args.t = screen.height / 2 - args.h / 2;
		}
		if (!args.l) {
			args.l = screen.width / 2 - args.w / 2;
		}
		if (args['wintype'] == '_top') {
			args.win.location.href = urlstr;
		}
		else {			
			open(
			urlstr,
			args['wintype'],
			'width=' + args.w + ',height=' + args.h + ',top=' + args.t + ',left=' + args.l + ',scrollbars,resizable'
			);
		
		}
		return;
	},
	
	qs :
	function(args) {
		args = jQuery.extend({
			ret : 'string',
			win : window
		},
		args);
		
		if (args['ret'] == 'string') {
			return jQuery.jqURL.url({ win:args.win }).split('?')[1];
			}

		else if (args['ret'] == 'object') {
			
			var qsobj = {};
			var thisqs = jQuery.jqURL.url({ win:args.win }).split('?')[1];
			
			if ( thisqs ) {
				var pairs = thisqs.split('&');
				for ( i=0;i<pairs.length;i++ ) {
					var pair = pairs[i].split('=');
					qsobj[pair[0]] = pair[1];
				}
			}
			return qsobj;
		}
	},
	
	strip :
	function(args) {
		args = jQuery.extend({
			keys : '',
			win : window
			},
			args);
		
		if (jQuery.jqURL.url().indexOf('?') == -1) { // no query string found
			return jQuery.jqURL.url({ win:args.win });
		}
		// if no keys passed in, just return url with no querystring
		else if (!args.keys) {
			return jQuery.jqURL.url({ win:args.win }).split('?')[0];
		}
		else { //return stripped url

			var qsobj = jQuery.jqURL.qs({ ret:'object',win:args.win });  // object with key/value pairs		
			var counter = 0;
			var url = jQuery.jqURL.url({ win:args.win }).split('?')[0] + '?';
			var amp = '';
			
			for (var key in qsobj) {
				if (args.keys.indexOf(key) == -1) { 
					// pass test, add this key/value to string
					amp = (counter) ? '&' : '';
					url = url + amp + key + '=' + qsobj[key];
					counter++;
				}
			}
			return url;
		}			
	},
	
	get :
	function(key,args) {
		args = jQuery.extend({
			win : window
			},args);
	
	qsobj =  jQuery.jqURL.qs({ ret:'object', win:args.win });
	return qsobj[key];
	},
	
	set :
	function(hash,args) {
		args = jQuery.extend({
			win : window
			},args);
		
		// get current querystring
		var qsobj =  jQuery.jqURL.qs({ ret:'object',win:args.win });
		
		// add/set values from hash
		for (var i in hash) {
			qsobj[i] = hash[i];
		}
		
		var qstring = '';
		var counter = 0;
		var amp = '';
		
		// turn qsobj into string
		for (var k in qsobj) {
			amp = (counter) ? '&' : '';
			qstring = qstring + amp + k + '=' + qsobj[k];
			counter++;
		}
		return jQuery.jqURL.strip({ win: args.win }) + '?' + qstring;
	}
	
};