﻿// Global Java Script Codes
// © 2009 www.edreamer.ir, All Rights Reserved.

/*-----------------------------------------------------------------------------------------------
    fridge page contents
-----------------------------------------------------------------------------------------------*/

function fridge()
{
    //if the browser is IE4+
    document.onselectstart=new Function ("return false");
    document.ondragstart=new Function ("return false");
    document.oncontextmenu=new Function ("return false");

    //if the browser is NS6
    if (window.sidebar || document.layer)
        {
            document.onmousedown=disabletext;
            document.onclick=reenable;
        }
}

function disabletext()
{
    return false;
}
				
function reenable()
{
	return true;
}

/*-----------------------------------------------------------------------------------------------
    preloading shadow
-----------------------------------------------------------------------------------------------*/

function displayshadow(div, flag)
{
    if (flag == 1)
    {
        $(parent.document.getElementById(div)).fadeIn('normal');
        //parent.document.getElementById(div).style.display = 'block';
        
		// stretch overlay to fill page
		var arrayPageSize = getPageSize();
		parent.document.getElementById(div).style.height = arrayPageSize[1] + "px";
        //document.getElementById(div).style.top = document.body.scrollTop;
        
        window.onscroll = function()
	    {
	        arrayPageSize = getPageSize();
	        parent.document.getElementById(div).style.height = arrayPageSize[1] + "px";
	        //document.getElementById(div).style.top = document.body.scrollTop;
	    }
	    
	    window.onresize = function()
	    {
	        arrayPageSize = getPageSize();
	        parent.document.getElementById(div).style.height = arrayPageSize[1] + "px";
	        //parent.document.getElementById(div).style.top = document.body.scrollTop;
	    }
    }
    else if (flag == 0)
    {
        $(parent.document.getElementById(div)).fadeOut('fast');
        //parent.document.getElementById(div).style.display = 'none';
        window.onscroll = "";
        window.onresize = "";
    }
    //alert ("DEBUG: flag = " + flag);
}

// getPageSize()
// Returns array with page width, height and window width, height 
// Core code from - quirksmode.org // Edit for Firefox by pHaez
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

/*-----------------------------------------------------------------------------------------------
    dialogs
-----------------------------------------------------------------------------------------------*/

function showdialog(filename, type, title, width, height)
{
    displayshadow('dialogbox', 1);
	
	//parent.document.getElementById('dialogbox-title').innerHTML = title;
	parent.document.getElementById('dialogbox-container').style.width = width + 'px';
	parent.document.getElementById('dialogbox-container').style.height = height + 'px';
	
	// fix the dialogboxframe height bug
	//parent.document.getElementById('dialogboxframe').style.height = (height - 40) + 'px';
	
	if (type == "url")
	{
        window.open(filename, 'dialogboxframe');
        parent.document.getElementById('dialogboxframe').style.display = 'none';
	}
	else if (type == "inline")
	{
		var str = parent.document.getElementById(filename).innerHTML;
		parent.document.getElementById('dialogbox-content').innerHTML = str;
	    parent.document.getElementById('dialogbox-content').style.display = 'block';
	}
}

function showiframecontent()
{
    parent.document.getElementById('dialogboxframe').style.display = 'block';
}

function closedialog()
{
    parent.dialogboxframe.location.href = "about:blank";
    parent.document.getElementById('dialogboxframe').style.display = 'none';
    
    parent.document.getElementById('dialogbox-content').style.display = 'none';
    
    displayshadow('dialogbox', 0);
}

/*-----------------------------------------------------------------------------------------------
    other functions
-----------------------------------------------------------------------------------------------*/

function shadowconfirm(message)
{
    //displayshadow('preloading', 1)
	var returnvar = confirm(message);
	//displayshadow('preloading', 0)
	return returnvar;
}

function subscribetonewsletter()
{
    var email = "";
    
    if (window.document.getElementById('newsletteremail') != null) {
        var email = window.document.getElementById('newsletteremail').value;
        window.document.getElementById('newsletteremail').value = "";
    }
    
    showdialog('/NewsletterRegister.aspx?Email=' + email, 'url', 'Create New Account', 470, 415);
    scrollTo(0,0);
 }
 
// function close(what)
//{
//    (document.getElementById(what)).style.display = 'none';
//}

/*-----------------------------------------------------------------------------------------------
    event handlers and onload
-----------------------------------------------------------------------------------------------*/

$(document).ready(function()
    {
        // add dialog markup to the body of the doc
        $("body").append("<div id='dialogbox' class='shadow'><div id='dialogbox-container'>" +
        "<h3 id='dialogbox-title'></h3><span id='dialogbox-close' title='Close'" +
        "onclick='javascript:closedialog();'></span><div id='dialogbox-content' >" +
        "</div><iframe name='dialogboxframe' id='dialogboxframe' src='about:blank' scrolling='auto' " +
        "frameborder='0' title='Dialog'></iframe></div></div>");
        
        // register event handlers
        $('#dialogbox-close').bind('click', function()
            {
                closedialog();
                return false;
            });
        
        $('#login').bind('click', function()
            {
                showdialog('/logindialog.aspx','url','Members Login', 452, 217);
                return false;
            });
            
        $('#normal').bind('click', function()
            {
                chooseStyle('none', 60); 
                return false;
            });

        $('#high').bind('click', function()
            {
                chooseStyle('High Contrast', 60);
                return false;
            });

        $('.register').bind('click', function()
            {
                subscribetonewsletter();
                return false;
            });
            
        $('.status-trigger').toggle(function()
            {
                $('#status-contents').slideDown('normal');
                return false;
            },
            function() {
                $('#status-contents').slideUp('fast');
                return false;
            });

        // find a elements that have an "external link" class
        $('.externallink').bind('click', function()
        {
            window.open(this.href,"blankTarget");
            return false;
        });
        
        // ImageTrail
        $('.imagetrail').bind('mouseover', function()
        {
            showtrail(320,240,this.src);
            return false;
        });
        
        $('.imagetrail').bind('mouseout', function()
        {
            hidetrail();
            return false;
        });
        
        // smooth page scrolling
        $('a[href*=#]').click(function()
        {
            if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname && this.hash.length != 0)
            {
                var $target = $(this.hash);
                $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
                if ($target.length)
                {
                    var targetOffset = $target.offset().top;
                    $('html,body').animate({scrollTop: targetOffset}, 1000);
                    return false;
                }
            }
        });
        
        $('#print').bind('click', function()
            {
            parent.print();
            return false;
        });
        
        displayshadow('preloading',0);
        //fridge();
        
//        $('body').append('<div id="ajax-div"></div>');
//        
//        $('p a.1').click(function(e){
//           // var x = $(this).attr('href');
//            $('#ajax-div').load('logindialog.aspx');
//            e.preventDefault();
//        });
//        
    });