﻿
//
// Copyright (c) 2008  Kivanc Toker - http://www.kivanctoker.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing permissions and limitations under the
// License.
//

// Scripts for the window size and centering
window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
		
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
		
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
	
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{		
		    offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
		
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
		
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;
	
	return{x:_x,y:_y};
}

// Scripts for the pop-up windows
function ShowPopUp(obj, offsetY, offsetX){
    
    // Get the object
	var el = document.getElementById(obj);	
	
	// Get the content div
	var contentDiv = $get('ContentDiv');
	
	if (contentDiv != null){
	
	    // Get the location of the content div
        var locContentDiv = Sys.UI.DomElement.getLocation(contentDiv);            
    	
	    // Set the position
	    el.style.position = 'absolute';	
	    el.style.left = ((window.center().x - locContentDiv.x) + offsetX) + 'px';
	    el.style.top = ((window.center().y - locContentDiv.y) + offsetY) + 'px';
	
	}else{
	    
	    el.style.position = 'absolute';	
	    el.style.left = ((window.center().x) + offsetX) + 'px';
	    el.style.top = ((window.center().y) + offsetY) + 'px';
	}
	
	// Show the pop up
	el.style.display = 'block';
			
}

function HidePopUp(obj){

    // Get the object
	var el = document.getElementById(obj);
	
	// Show the pop up
	el.style.display = 'none'

}

// Shared scripts for the user interface

function SwitchMenu(obj){
	
	var el = document.getElementById(obj);
		
	if(el.style.visibility == 'hidden'){
		
		el.style.visibility = 'visible';
	
	}else{
	
		el.style.visibility = 'hidden';
	
	}				
}

function SlideDown(elementName, buttonToChange){

    var el = document.getElementById(elementName);
    
    if (el.style.display == 'none'){	
		
		Effect.SlideDown(elementName, {duration: 0.5});
		
		if (buttonToChange != null){
            
            buttonToChange.src = '/Images/Icons/minusButton.gif';            
        }
    }       
}

function SlideUp(elementName, buttonToChange){

    var el = document.getElementById(elementName);
    
     if(el.style.display == ''){		
	
		Effect.SlideUp(elementName, {duration: 0.5});
		
		if (buttonToChange != null){
            
            buttonToChange.src = '/Images/Icons/plusButton.gif';            
        }
		
    }   
}

function SlideItem(elementName, buttonToChange){

    var el = document.getElementById(elementName);
    
    if(el.style.display == ''){		
	
		Effect.SlideUp(elementName, {duration: 0.5});
		
		if (buttonToChange != null){
            
            buttonToChange.src = '/Images/Icons/plusButton.gif';            
        }
		
    }else if (el.style.display == 'none'){	
		
		Effect.SlideDown(elementName, {duration: 0.5});
		
		if (buttonToChange != null){
            
            buttonToChange.src = '/Images/Icons/minusButton.gif';            
        }
    }       
}

function SlideHorizontal(eventSource, elementName, direction, pixels, limit){

    var el = document.getElementById(elementName);
    
    if (direction == 'r') {
    
    }else{ 
    
        pixels *= -1;
    
    }
       	
	new Effect.Move(el, { x: pixels, y: 0 }); 

}

function SlideVertical(eventSource, elementName, direction, pixels, limit){

    // Get the element to slid
    var el = document.getElementById(elementName);
   
    if (direction == 'd') {
    
    }else{ 
    
        pixels *= -1;
    
    }   
    // Move the element
    new Effect.Move(el, { x: 0, y: pixels });   

}


function ToggleItem(target)
  {
      if ($(target).is(":hidden"))
      {
        $(target).slideDown("slow");
      }
      else
      {
        $(target).slideUp();
      }
  };
  
function SlideItem(target,way,container,speed) {
    
    var position = $(target).position();

    var height_item = $(target).height();
    var width_item = $(target).width();

    var height_cont = $(container).height();
    var width_cont = $(container).width();

    if(way=='left')
    {
        if(position.left<=0)
            $(target).animate({"left": "+="+speed+"px"},20, function(){SlideItem(target,way,container,speed);});
    }
    else if(way=='right')
    {
        if(position.left+width_item>=width_cont)
            $(target).animate({"left": "-="+speed+"px"}, 20, function(){SlideItem(target,way,container,speed);});
    }
    else if(way=='up')
    {
        if(position.top<=0)
            $(target).animate({"top": "+="+speed+"px"}, 20, function(){SlideItem(target,way,container,speed);});
    }
    else if(way=='down')
    {
        if(position.top+height_item>=height_cont)
            $(target).animate({"top": "-="+speed+"px"}, 20, function(){SlideItem(target,way,container,speed);});
    }
    else
    {
        
    }
};
  
function StopItem(target){

    $(target).stop();
};
      
function isNumeric(sText)
{
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   
   return IsNumber;
   
}

// AJAX Scripts for the user interface
function onAjaxRequestFailed(error){

    alert('Stack trace:' + error.get_stackTrace() + '/r/n');

}

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();