/**
 * Common javascript library file for sukker.no
 * 
 * @version SVN: $Id: sukker.js 3349 2011-05-05 11:25:44Z mnb $
 */


/**
 * Put style on the input box.
 */
function style_input_box(id) {
	var input = $(id);
	input.setStyles({
		border: '1px solid #c55',
        backgroundColor: '#fee',
        MozBoxShadow: '4px 4px 4px #ccc'
    });
}

/**
 * Adding functionality for returning browser tokens to Browser
 * Browser is a mootools object for browser information.
 */
Browser.nickName = function() {
	var testFirefox = new RegExp('Firefox');
	var testChrome  = new RegExp('Chrome');
	var testSafari  = new RegExp('Safari');
	var testMSIE    = new RegExp('MSIE');
	if (testFirefox.test(navigator.userAgent)) {
		return 'firefox';
	} else if (testChrome.test(navigator.userAgent)) {
		return 'chrome';
	} else if (testSafari.test(navigator.userAgent)) {
		return 'safari';
	} else if (testMSIE.test(navigator.userAgent)){
		return 'msie';
	} else {
		return false;
	}
}

/**
 * workaround to make captcha boxes render correctly across browsers.
 *
var setCaptchaStyle = function() {
    //alert("This is setStyleCaptcha\n" + navigator.userAgent +
    //      "\nnickName: " + Browser.nickName() +
    //      "\nistrident: " + Browser.Engine.trident +
    //      "\nversion: " + Browser.Engine.version);

    var captcha = $('recaptcha_controls');

    if (Browser.Engine.trident) {
        if (Browser.Engine.version == 6) {
            // IE 8
            captcha.setStyle('height', '74px');
        }
        if (Browser.Engine.version == 5) {
            // IE 7
            captcha.setStyle('clear', 'both');
            captcha.setStyle('height', '75px');
        }    
    }
}
*/



var getViewportSize = function() {
	var size = document.getSize();
	return size.x + "x" + size.y;
}


/**
 * Calculate offset of new window according to browser
 */
var calculateOffset = function() {
	var offset = {x: 0, y: 32 };
	
	if (Browser.nickName() == 'firefox') {
		offset.y = 14;				
	}
	if (Browser.nickName() == 'chrome') {
		offset.y = 64;				
	}
	if (Browser.nickName() == 'safari') {
		offset.y = 14;				
	}
	if (Browser.nickName() == 'msie') {
		offset.y = 28;
		offset.x = 24;
	}
	return offset;
}

var openSqueezeOrPopup = function(options) {
	var miny = options.size.y + 50;
    var box  = document.getSize();
	
	if (box.y < miny || Browser.Platform.ipod || options.openwin == '2' ) {
		var offset = calculateOffset();
	    return window.open(options.url, "popup",
	    	"location=0, status=0, directories=0, menubar=0, toolbar=0, resizable=1, scrollbars=1, width=" + (options.size.x +offset.x) + ", height=" + (options.size.y + offset.y)+""
	    );
    }
	
	// ARGH. adding exceptions for behaviour on windows.
	if (Browser.nickName() == 'chrome' && Browser.Platform.name == 'win') {
		options.size.x = options.size.x + 2;		
	}
	
	SqueezeBox.open(options.url, options);
	return true;
}

function textCounter(field, maxlimit) {
   if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit);
}



function desc(id,tab,code,ans) {
	ans = typeof(ans) != 'undefined' ? ans : 0;
	SqueezeBox.open("/isimple.php?page=descript&id="+id+"&tab="+tab+"&code="+code+"&ans="+ans+"",{handler:'sframe',size:{x:760,y:800}});
}
function desc2(id,tab,code,ans) {
	ans = typeof(ans) != 'undefined' ? ans : 0;
	window.open("/isimple.php?page=descript&id="+id+"&tab="+tab+"&code="+code+"&ans="+ans+"",id,"location=0,status=0,directories=0,menubar=0,toolbar=0,resizable=1,scrollbars=1,width=760,height=800");
}

// Copyright 2006,2007 Bontrager Connection, LLC
// http://bontragerconnection.com/ and http://www.willmaster.com/
// Version: July 28, 2007
var cX = 0; var cY = 0; var rX = 0; var rY = 0;
function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;}
function UpdateCursorPositionDocAll(e){ cX = event.clientX; cY = event.clientY;}
if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }
function AssignPosition(d, pos) {
    if(self.pageYOffset) {
        rX = self.pageXOffset;
        rY = self.pageYOffset;
    }
    else if(document.documentElement && document.documentElement.scrollTop) {
        rX = document.documentElement.scrollLeft;
        rY = document.documentElement.scrollTop;
    }
    else if(document.body) {
        rX = document.body.scrollLeft;
        rY = document.body.scrollTop;
    }
    if(document.all) {
        cX += rX;
        cY += rY;
    }

    if (pos == 1) {
        d.style.left = (cX - (document.body.clientWidth-890)/2 + 30) + "px";
        d.style.top = (cY - 150) + "px";
    } else {
        d.style.left = (cX - (document.body.clientWidth-620)/2) + "px";
        d.style.top = (cY + 10) + "px";
    }
}
function HideContent(d) {
    if(d.length < 1) { return; }
    document.getElementById(d).style.display = "none";
}
function ShowContent(d, pos) {
    if(d.length < 1) { return; }
    var dd = document.getElementById(d);
    pos = (pos == 2 ? 2 : 1);
    AssignPosition(dd, pos);
    dd.style.display = "block";
}
function ReverseContentDisplay(d) {
    if(d.length < 1) { return; }
    var dd = document.getElementById(d);
    AssignPosition(dd);
    if(dd.style.display == "none") { dd.style.display = "block"; }
    else { dd.style.display = "none"; }
}

// Validate that all radio buttons are not empty
function validateform() {
    var j = 0;
    var t = 0;
    var radio_buttons = new Array();
    var allElements = document.getElementsByTagName("input");

    for(var i=0; i<allElements.length; i++) {
        if (allElements[i].type == "radio" && allElements[i].checked) {
            radio_buttons[j] = allElements[i].value;
            j++;
        }
        if (allElements[i].type == "radio" && allElements[i].name.substring(0,2) == "q[") {
            t++;
        }
    }

    if (radio_buttons.length * 10 == t && t > 0) {
        return true;
    } else {
        alert("You have to check radio buttons.");
    }

    return false;
}

function chars_counter(field, countfield, maxlimit) {
  var textCount = field.value.length;
  var plural = "";
  if( field.value.match(/\r\n/) ) {
    // Some browsers count newline as two characters
    var numNewlines = field.value.match(/\r\n/g).length;
    textCount = field.value.length;
  }
  else if( field.value.match(/\n/) ) {
    // Some browsers count newline as one character
    var numNewlines = field.value.match(/\n/g).length;
    textCount = field.value.length + (numNewlines);
  }
  if((textCount == maxlimit-1) || (textCount == maxlimit+1)) {
    plural = "";
  }

  if( textCount > maxlimit ) {
    countfield.innerHTML = '0 tegn igjen';
    field.value = field.value.substr(0, maxlimit);
  } else {
    countfield.innerHTML = (maxlimit - textCount) + ' tegn' + plural + ' igjen';
  }
} 

startList = function() {
 if (document.all&&document.getElementById) {
  navRoot = document.getElementById("nav");
  for (i=0; i<navRoot.childNodes.length; i++) {
   node = navRoot.childNodes[i];
   if (node.nodeName=="LI") {
    node.onmouseover=function() {
     this.className+=" over";
    }
    node.onmouseout=function() {
     this.className=this.className.replace(" over", "");
    }
   }	
  }
 }
}

function keepSessionAlive(firsttime){

      setTimeout('keepSessionAlive(false)',600000);//10min
      
      if(!firsttime){
	    var myRequest = new Request({method: '', url: '/sync.php'});//eller annen fil som holder sesjonen i live
	    myRequest.send('');	    
      }
}

function notify_alert(show) {

if (show) {
document.getElementById('updater').className='updater_visable';
return true;
} else {
document.getElementById('updater').className='updater_hidden';
return true;
}

return false;      
}
