﻿String.prototype.beginsWith = function(t, i){
	 if (i==false) {
	 	return ( t == this.substring(0, t.length));
	 } else {
	 	return (t.toLowerCase() == this.substring(0, t.length).toLowerCase()); 
	 }
}

String.prototype.endsWith = function(t, i) {
	 if (i==false) {
	 	 return (t == this.substring(this.length - t.length)); 
	 } else {
	     return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());
	 }
} 
/*
 *
 *	Harbork listFilter plugin v0.1
 *		Soctt Zhang 
 */
 
jQuery.harborkFilter = function (filterBox, listBox)
{
        $("#" + filterBox ).keyup(function(){
        $("#" + listBox + " li a").each(function(){
                if($(this).html().substr(0,$("#" + filterBox).val().length) == $("#" + filterBox ).val()){
                    $(this).parents("li").css("display","");
                }else{
                    $(this).parents("li").css("display","none");
                }
                });
        });
}

/*
 *
 *	Harbork listFilter plugin v0.1
 *		Soctt Zhang 
 */
 
jQuery.harborkCheckAll = function (checkAll, listBox)
{
        $("#" + checkAll ).toggle(function(){
        $("#" + listBox + " li").find('input').each(function(){
            $(this).attr('checked',true);
                });
       },function(){
        $("#" + listBox + " li").find("input").each(function(){
            $(this).attr('checked',false);
                });
        });
         
}




/*
 *
 *	Harbork waterMark plugin v0.1
 *		Soctt Zhang 
 */
 jQuery.harborkWatermark = function(waterBox){
                if(!waterBox || waterBox.length == 0){
                    return;
                }
                waterBox.each(function(){
                    $(this).data('swapValue',$(this).val()).data("oldcolor",$(this).css("color")).css("color","#d5d5d5");
                    $(this).unbind("focus").unbind("blur");
			         $(this).focus(function(){			          
			              if($(this).val() == $(this).data('swapValue')){
			                $(this).val('').css("color",$(this).data("oldcolor"));
    			            
			              }
			            }
			         ).blur(function(){
			              if($(this).val() == ''){
			                $(this).css("color","#d5d5d5");
			                $(this).val($(this).data('swapValue'));    			            
			              }
			            }
			         );
			    });
}

function findPos(obj) {
    obj = obj.get(0);
    var curleft = obj.offsetLeft || 0;
    var curtop = obj.offsetTop || 0;
    while (obj = obj.offsetParent) {
	    curleft += obj.offsetLeft - 94;
	    curtop += obj.offsetTop - 151;
    }
    return {x:curleft,y:curtop};
}

jQuery.fn.extend({popupDlg:function(dlgId,options){
        options = options || {};
        options.moveLeft = options.moveLeft || 0;
        options.moveTop = options.moveTop || 0;
        if(options.alignRight){
            options.moveLeft = $("#" + dlgId).width();
        }
        if(options.alginBottom){
            options.moveTop = $("#" + dlgId).height();
        }
        
        if(options.popupNow){
            var pos = findPos($(this));
		    // reposition
		    $("#" + dlgId ).css("position","absolute").css("clear",'both').css("top",(pos.y + $(this).get(0).offsetHeight - options.moveTop ) + "px").css("left", (pos.x - options.moveLeft ) + "px").css("z-index",10).show();
        }else{
            $(this).click(function(){	        
	            //$("iframe").toggle();
                var pos = findPos($(this));
		        // reposition
		        $("#" + dlgId ).css("position","absolute").css("clear",'both').css("top",(pos.y + $(this).get(0).offsetHeight - options.moveTop ) + "px").css("left", (pos.x - options.moveLeft ) + "px").css("z-index",10).show();
		    });
		}
    
}});
/*
 *
 *	Harbork auto submit plugin v0.1
 *		Soctt Zhang 
 */
 
 jQuery.fn.extend({reload:function(){
    $('#subCommentDiv' + $(this).attr('rid')).load('comments.ashx?rid=' + $(this).attr('rid') + "&contentid=" + $(this).attr('cid') + "&posttotype=" + $(this).attr('pt') + "&replyto=" + $(this).attr('rt') );
    $(this).attr("load",1);
 }});
  jQuery.fn.extend({reloadUser:function(){
    $('#subCommentDiv' + $(this).attr('rid')).load('commentsUser.ashx?rid=' + $(this).attr('rid') + "&contentid=" + $(this).attr('cid') + "&posttotype=" + $(this).attr('pt') + "&replyto=" + $(this).attr('rt') );
    $(this).attr("load",1);
 }});
 jQuery.gtAutoSubmit = function(enterBox, submitButton){
        enterBox.each(function(){
            $(this).keypress(function(e){
                if(13 == e.keyCode){
                  //if(options.allownull && options.allownull == false && $.trim($(this).val()).length == 0){
                      //$(this).trigger("blur");
                      setTimeout(function(){ submitButton.trigger('click');},100);
                      e.preventDefault(); e.stopPropagation();
                 //}
                }   
            });
         });
     
      }

jQuery.harborkPostComment = function(comment){
    $.ajax({
        type:"POST",
        url:"saveComment.ashx",
        dataType:'html',
        data: $("#" + comment).serial(),
        success:function(){
            alert("Save ok");
            },
         error:function(){
            alert('error');
         }});
};

jQuery.harborkRateComment = function(commentid,isUp,rid){
  $.ajax({
    type:"POST",
    url:"rateComment.ashx",
    dataType:'html',
    data:{'commentid':commentid,'isUp':isUp},
    success:function(){
        $("#showReply" + rid).reload();
        },
    error:function(){
        alert("error");
    }});
};


//Get Center Position
function getCenterPosition(eleSize){
    function getPageSize() {
		var d = document.documentElement;
		var w = window.innerWidth	|| self.innerWidth	|| (d && d.clientWidth)		|| document.body.clientWidth;
		var h = window.innerHeight	|| self.innerHeight	|| (d && d.clientHeight)	|| document.body.clientHeight;

		return [w,h];
	}
	
    var lPageSize	= getPageSize();
    var m_left;
    m_left = Math.round(lPageSize[0] / 2)  - Math.round(eleSize[0] / 2);
	var m_top	= Math.round(lPageSize[1] / 2)  - Math.round(eleSize[1] / 2);

	//m_top	+= typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;
	//m_left	+= typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;
	return [m_top,m_left];
}

function adjustImageSize(img){
  a = $("<img />").hide().appendTo($("body"));
  a.attr("src",img.src);
  oriHeight = parseInt(a.height(),10);
  oriWidth = parseInt(a.width(),10);
  height = img.height;
  width = img.width;
    if (oriHeight > height && oriWidth > width)
    {
        var standardHeight = height;
        var standardWidth = width;

        if ((standardHeight / oriHeight) > (standardWidth / oriWidth))
        {
            //width rate is bigger than height
            height = standardHeight;
            width = ((standardHeight / oriHeight) * oriWidth) + 1;
        }
        else
        {
            width = standardWidth;
            height = ((standardWidth / oriWidth) * oriHeight) + 1;
        }
    } else if (oriHeight < height && oriWidth >= width) {
        width = ((height) / oriHeight) * oriWidth;
    }else if (oriWidth < width && oriHeight >= height){
        height = ((width) / oriWidth) * oriHeight;
    }else{
        var standardHeight = height;
        var standardWidth = width;
        if ((standardHeight / oriHeight) > (standardWidth / oriWidth)) {
            //height rate is bigger than height
            height = standardHeight;
            width = ((standardHeight / oriHeight) * oriWidth) + 1;
        } else {
            width = standardWidth;
            height = ((standardWidth / oriWidth) * oriHeight) + 1;
            
        }
    }
    a.remove();
    return {height:parseInt(height),width:parseInt(width)};
  
}



function harborkGoUrl(url){
    window.location.href = url;
}



jQuery.fn.ReplaceUrl = function() {
    return this.each(function() {
        var $ = jQuery,
        c = $(this).find('*').contents().filter(function() {
            return this.nodeType == 3 && $.trim(this.nodeValue).length > 0 && $(this).parents('a').length == 0;
        });
        c.each(function() {
            if (this.nodeValue) {
                var newValue = RenderUrl(this.nodeValue);
                if (newValue != this.nodeValue) {
                    var newNode = $(newValue);
                    $(this).after(newNode);
                    $(this).remove();
                }
            }
        });
    })
};


function RenderUrl(content){
    return content.replace(/(http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?)/,'<a href="$1" target="_blank">$1</a>');
}
$(document).ready(function(){


$.fn.gtWaiting = function(){
	$(this).block({message:"<img src='" + gBaseUrl + "/public/images/loader.gif' />",
								   css: {  
								        padding:        0, 
								        margin:         0,  
								        width:          '100%',
								        top:            '20px',  
								        left:           '0', 
								        color:          'red',  
								        border:         '0px solid #aaa', 
								        backgroundColor:'#FFF', 
								        cursor:         'wait' 
								    },
								    applyPlatformOpacityRules: false,
								    overlayCSS: {
								    	backgroundColor:'#FFF', 
										opacity:0.6
 									}});
}

$.gtBlockUI = function(){
    $.blockUI({message:"<img src='" + gBaseUrl + "/public/images/loader.gif' />",
								   css: {  
								        padding:        0, 
								        margin:         0,  
								        width:          '100%',
								        top:            '20px',  
								        left:           '0', 
								        color:          'red',  
								        border:         '0px solid #aaa', 
								        backgroundColor:'#FFF', 
								        cursor:         'wait' 
								    },
								    applyPlatformOpacityRules: false,
								    overlayCSS: {
								    	backgroundColor:'#000',opacity:'0.68'
 									}});
}
});

var posx = 0;
var posy = 0;
function mousePos(e)
{	
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	return(posx,posy);
}


function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}


function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

