/*************************
  Coppermine Photo Gallery
  ************************
  Copyright (c) 2003-2006 Coppermine Dev Team
  v1.1 originally written by Gregory DEMAR

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  ********************************************
  Coppermine version: 1.4.8
  $Source$
  $Revision: 3116 $
  $Author: gaugau $
  $Date: 2006-06-08 00:11:54 +0200 (Do, 08 Jun 2006) $
**********************************************/

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function writeCookie(name, data, noDays){
  var cookieStr = name + "="+ data
  if (writeCookie.arguments.length > 2){
    cookieStr += "; expires=" + getCookieExpireDate(noDays)
    }
  document.cookie = cookieStr
}

function readCookie(cookieName){
   var searchName = cookieName + "="
   var cookies = document.cookie
   var start = cookies.indexOf(cookieName)
   if (start == -1){ // cookie not found
     return ""
     }
   start += searchName.length //start of the cookie data
   var end = cookies.indexOf(";", start)
   if (end == -1){
     end = cookies.length
     }
   return cookies.substring(start, end)
}

function blocking(nr, cookie, vis_state)
{
        if (document.layers)
        {
                current = (document.layers[nr].display == 'none') ? vis_state : 'none';
                if (cookie != '')
                        writeCookie(nr, current);
                document.layers[nr].display = current;
        }
        else if (document.all)
        {
                current = (document.all[nr].style.display == 'none') ? vis_state : 'none';
                if (cookie != '')
                        writeCookie(nr, current);
                document.all[nr].style.display = current;
        }
        else if (document.getElementById)
        {
                display = (document.getElementById(nr).style.display == 'none') ? vis_state : 'none';
                if (cookie != '')
                        writeCookie(nr, display);
                document.getElementById(nr).style.display = display;
        }
}


function adjust_popup()
{
        var w, h, fixedW, fixedH, diffW, diffH;
        if (document.documentElement && document.body.clientHeight==0) {     // Catches IE6 and FF in DOCMODE
                fixedW = document.documentElement.clientWidth;
                fixedH = document.documentElement.clientHeight;
                window.resizeTo(fixedW, fixedH);
                diffW = fixedW - document.documentElement.clientWidth;
                diffH = fixedH - document.documentElement.clientHeight;
                w = fixedW + diffW + 16; // Vert Scrollbar Always On in DOCMODE.
                h = fixedH + diffH;
                if (w >= screen.availWidth) h += 16;
        } else if (document.all) {
                fixedW = document.body.clientWidth;
                fixedH = document.body.clientHeight;
                window.resizeTo(fixedW, fixedH);
                diffW = fixedW - document.body.clientWidth;
                diffH = fixedH - document.body.clientHeight;
                w = fixedW + diffW;
                h = fixedH + diffH;
                if (h >= screen.availHeight) w += 16;
                if (w >= screen.availWidth)  h += 16;
        } else {
                fixedW = window.innerWidth;
                fixedH = window.innerHeight;
                window.resizeTo(fixedW, fixedH);
                diffW = fixedW - window.innerWidth;
                diffH = fixedH - window.innerHeight;
                w = fixedW + diffW;
                h = fixedH + diffH;
                if (w >= screen.availWidth)  h += 16;
                if (h >= screen.availHeight) w += 16;
        }
        w = Math.min(w,screen.availWidth);
        h = Math.min(h,screen.availHeight);
        window.resizeTo(w,h);
        window.moveTo((screen.availWidth-w)/2, (screen.availHeight-h)/2);
}

function show_section(e) {
    if (document.getElementById(e).style.display == 'none') {
        document.getElementById(e).style.display = 'block';
    } else {
        document.getElementById(e).style.display = 'none';
    }
}


function expand()
{
        var Nodes = document.getElementsByTagName("table")
        var max = Nodes.length
        for(var i = 0;i < max;i++) {
                var nodeObj = Nodes.item(i)
                var str = nodeObj.id
                if (str.match("section")) {
                        nodeObj.style.display = 'block';
                }
        }
}

function hideall()
{
        var Nodes = document.getElementsByTagName("table")
        var max = Nodes.length
        for(var i = 0;i < max;i++) {
                var nodeObj = Nodes.item(i)
                var str = nodeObj.id
                if (str.match("section")) {
                        nodeObj.style.display = 'none';
                }
        }
}

function jsInclude(xUrl,xId) {

  var xmlhttp = false;

  /*@cc_on @*/

  /*@if (@_jscript_version >= 5)

  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.

  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
   }

  /*@end @*/

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  xmlhttp.open("GET", xUrl,true);
  xmlhttp.onreadystatechange=function() {

          if (xmlhttp.readyState==4) {
                document.getElementById(xId).innerHTML = xmlhttp.responseText;
          }
  }
  xmlhttp.send(null)

}

function findPos(obj) {
	var curleft		= curtop = 0;
	var objwidth	= obj.offsetWidth;
	var objheight	= obj.offsetHeight;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}

   		curright	= curleft + objwidth;
		curbot		= curtop + objheight;
		var pointsstar = document.getElementById('points_scored');
		pointsstar.style.top = (curbot - 57) + 'px';
 		pointsstar.style.left = (curright - 57) + 'px';
		pointsstar.style.display = 'block';
	}
}

function findLbPos() {
	//var elements = $('lb_content').getElementsByTagName('img');
	var elements = document.getElementsByClassName('lb_img');

	for (i=0;i<elements.length;i++) {

        var obj 		= elements[i];
		var curleft		= curtop = 0;
		var objwidth	= obj.offsetWidth;
		var objheight	= obj.offsetHeight;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}

       		curright = curleft + objwidth;
			//console.log('trying lb_points_' + i);
            var pointsstar = $('lb_points_' + i);
			if (pointsstar) {
				pointsstar.style.top = (curtop - 10) + 'px';
		 		pointsstar.style.left = (curright - 25) + 'px';
				pointsstar.style.display = 'block';
			}
        }
	}
}

function lbLoading() {
	$('lb_content').innerHTML = '<img class="lb_loading" src="/themes/fantastic/img/lb_loading.gif" alt="Please wait">';
}

function highlight(lbcat) {
	var elementList = $('lb_menu').getElementsByTagName("a");

	for (i=0;i<elementList.length;i++) {
    	Element.removeClassName(elementList[i], 'lb_active');
	}

	Element.addClassName('lba_' + lbcat, 'lb_active');
}

function leaderboard(menu, lbcat, page, cat, album) {
	var postData = 'lbcat=' + lbcat + '&menu=' + menu + '&page=' + page + '&cat=' + cat + '&album=' + album;
	new Ajax.Updater('lb_content', '/include/leaderboard.inc.php', {evalScripts:true, postBody:postData, onLoading:lbLoading, onComplete:highlight(lbcat)});
}


function vote(id, rating) {
	var params = 'rate=' + rating + '&pic=' + id + '&ajax=1';
	new Effect.Opacity('rater', {duration:0.5, from:1.0, to:0});
   	new Ajax.Updater('rater', 'ratepic.php', {method: 'get', evalScripts: true, onFailure: showPoll, parameters: params});
}

function showPoll(id, good) {
	var id2 = id;
	setTimeout("new Effect.Opacity('rater', {duration:0.5, from:0.0, to:1.0})", 1000);
	setTimeout("updatePoints(" + id2 + ", " + good + ")", 2000);
}

function updatePoints(id, good) {
	if (good == 1) {
	 	var id2 = id;
		new Effect.Opacity('rater', {duration:0.5, from:1.0, to:0});
		new Effect.Opacity('points_scored', {duration:0.5, from:1.0, to:0});
		var params = 'type=points&pid=' + id2;
	   	new Ajax.Updater('points_scored', 'update_points.php', {method: 'get', evalScripts: true, parameters: params});
		var params = 'type=rating&pid=' + id2;
	   	new Ajax.Updater('rater', 'update_points.php', {method: 'get', evalScripts: true, parameters: params});
	}

	else {
	 	var id2 = id;
		new Effect.Opacity('rater', {duration:0.5, from:1.0, to:0});
		var params = 'type=rating&pid=' + id2;
	   	new Ajax.Updater('rater', 'update_points.php', {method: 'get', evalScripts: true, parameters: params});
	}

}