var textBackup = new Object();

countVinNumber = function(element) {   
   $('.vin-search-character-info span.value', this.parentNode)[0].innerHTML = 17 - this.value.length;
}

shootmyrideMove = function (direction) {
	var content = $('#shootmyride_gallery_content');
	var contentWidth = content.width();
	var containerWidth = $('#shootmyride_gallery_container').width();
	var currentPos = jQuery.data(content.get(0), 'paginationLeft');
	var pageWidth = $('#shootmyride_gallery_content .shootmyride_gallery_page').width();
	
	var bLeft, bRight;
	if (contentWidth > containerWidth) {
		bLeft = (currentPos < 0);
		bRight = (currentPos > -contentWidth + containerWidth);
	} else {
		bLeft = false;
		bRight = false;
	}
	
	if (bLeft && direction < 0) {
		currentPos += containerWidth;
		content.animate({left: "+=" + containerWidth + "px"}, 1000);
	} else if (bRight && direction > 0) {
		currentPos -= containerWidth;
		content.animate({left: "-=" + containerWidth + "px"}, 1000);
	}
	
	if (bLeft || bRight) {
		bLeft = (currentPos < 0);
		bRight = (currentPos > -contentWidth + containerWidth);
	}
	
	jQuery.data(content.get(0), 'paginationLeft', currentPos)
}

shootmyrideLeft = function (event) {
	shootmyrideMove(-1);
}

shootmyrideRight = function (event) {
	shootmyrideMove(1);
}


$(document).ready( function() {   
   $('.page-action :text, .page-action :password, #loginbox :text, #loginbox :password, .vin-search-form-container :text').bind('focus', function() {
                                                   if (textBackup[this.name] == null) {
                                                      textBackup[this.name] = this.value;
                                                   }
                                                   this.value=''

                                               }
                                                );
   $('.page-action :text, .page-action :password, #loginbox :text, #loginbox :password, .vin-search-form-container :text').bind('blur', function() {
                                                  if (this.value == '') {
                                                     this.value = textBackup[this.name];                                                     
                                                  }
                                             }
                                                );
   $('input.vin-search-text').bind('keyup', countVinNumber);

   $('.view-box-foldable img.open').show();
   $('.view-box-foldable div.openable').hide();

   $('.view-box-foldable .open').bind('click', function() {
      var img = $('img.open', this.parentNode)[0];
      if (img.opened) {
         img.src = '/image/arrow_closed.gif';
         img.opened = false;
         $('.openable', this.parentNode).slideUp("fast");
      }
      else {         
         img.src = '/image/arrow_opened.gif';
         img.opened = true;
         $('div.openable', this.parentNode).slideDown("fast");
      }
   });



   /*
   $('div.social-bookmark').bind('mouseenter', function() {      
      $('.dot', this).hide();
      $(this).animate({width: '260px'}, 300);
   }
   );

   $('div.social-bookmark').bind('mouseleave', function() {
      $(this).animate({width: '150px'}, 300);
      $('.dot', this).show();
   }
   );
   */

   $('#shootmyride_gallery_left').click(shootmyrideLeft);
   $('#shootmyride_gallery_right').click(shootmyrideRight);
   var content = $('#shootmyride_gallery_content');
   if (content.length > 0) {
	jQuery.data($('#shootmyride_gallery_content').get(0), 'paginationLeft', 0);
	}
});
showHide = function(event) {
   $('#loginbox').toggle('normal');
   return false;
};

$(document).ready( function() {
   $('.block1 .login').bind('click', showHide);
});
function ZoomPicture(PictureID, Language, Width, Height) {

   if ((Width/Height > 4/3 && Width > window.screen.availWidth - 35) || (Width/Height < 4/3 && Height > window.screen.availHeight - 150)) {

      NewWindowWidth = window.screen.availWidth - 20;

      NewWindowHeight = window.screen.availHeight - 150;

   } else {

      NewWindowWidth = Width + 50;

      NewWindowHeight = Height + 80;

   }

   NewWindowFeatures = "top=20,left=10,width="+NewWindowWidth+",height="+NewWindowHeight+",location=0,directories=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1";

   var PictureZoom = window.open("http://sommer.silvretta-montafon.at/generalpurpose/zoompicture.php?PictureID=" + PictureID + "&Language=" + Language, "PictureZoom", NewWindowFeatures);

   PictureZoom.focus();
}

function SwapImage(ImageName, ImageFileName) {

   document.images[ImageName].src = ImageFileName;

}

function SwitchDisplay(id) {
    obj = document.getElementsByTagName("div");
    if (obj[id].style.display == 'block') {
    	obj[id].style.display = 'none';
    } else { 
    	obj[id].style.display = 'block';
    }
}

function OpenURLInNewWindow(URL) {

   if (URL) {

      NewWindowWidth = (window.screen.availWidth - 20);
      NewWindowHeight = (window.screen.availHeight - 150);

      NewWindowFeatures = "top=20,left=10,width="+NewWindowWidth+",height="+NewWindowHeight+",location=1,directories=0,menubar=1,toolbar=1,status=1,scrollbars=1,resizable=1";

      var NewWindow = window.open("http://sommer.silvretta-montafon.at/index/referer.php?URL="+URL+"&S=", "PreviewWindow", NewWindowFeatures);

      NewWindow.focus();
   }
}

function OpenURLInNewWindowSized(URL, NewWindowWidth, NewWindowHeight) {

   if (URL) {

      //NewWindowWidth = (window.screen.availWidth - 20);
      //NewWindowHeight = (window.screen.availHeight - 150);

      NewWindowFeatures = "top=20,left=10,width="+NewWindowWidth+",height="+NewWindowHeight+",location=1,directories=0,menubar=1,toolbar=1,status=1,scrollbars=1,resizable=1";

      var NewWindow = window.open("http://sommer.silvretta-montafon.at/index/referer.php?URL="+URL+"&S=", "PreviewWindow", NewWindowFeatures);

      NewWindow.focus();
   }
}

function RoundToDecimal(ValueToRound, NoOfDecimal) {

   ValueToRound = Math.round(ValueToRound*Math.pow(10,2))/Math.pow(10,2);
   ValueToRoundString = "" + ValueToRound;
   CommaPos = ValueToRoundString.length - ValueToRoundString.indexOf(".");
   if (CommaPos == 2) {
      ValueToRoundString += "0";
   } else {
      if (CommaPos == ValueToRoundString.length + 1) {
         ValueToRoundString += ".00";
      }
   }
   return ValueToRoundString;
}



var marked_row = new Array;

function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;

    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 5. Sets the new color...
   newColor = thePointerColor;
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
                theCells[c].style.backgroundColor = newColor;
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

var Base64 = {
   _key : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
   
   decode : function (a) {
      var l=a.length;
      var out='';
      for (var i=0; i<l; i+=4) {
         var e1=this._key.indexOf(a.charAt(i));
         var e2=this._key.indexOf(a.charAt(i+1));
         var e3=this._key.indexOf(a.charAt(i+2));
         var e4=this._key.indexOf(a.charAt(i+3));
         var d1=(e1<<2)|(e2>>4);
         var d2=((e2&15)<<4)|(e3>>2);
         var d3=((e3&3)<<6)|e4;
         out+=String.fromCharCode(d1);
         if (e3!=64) { 
            out+=String.fromCharCode(d2);
            if (e4!=64) out+=String.fromCharCode(d3);
         }
      }
      return out;
   }
};

function malCdYetiPer(a,b) {
   a=Base64.decode(a);
   var i=a.charCodeAt(0);
   var al=a.length;
   var bl=b.length;
   var kp=0;
   var o='';
   for (var e=1;e<al;e++) {
      var c=a.charCodeAt(e);
      o+=String.fromCharCode(c^b.charCodeAt(kp++)^i);
      i=c;
      if (kp>=bl) kp=0;
   }
   return o;
}

function maarCelleLidTyp2(e) {
   var c=malCdYetiPer(e.value,'a2qq5fGSCWmr1i4p');
   var e1=document.createElement('a');
   e1.innerHTML = c;
   e1.href='mailto:'+c;
   e.parentNode.replaceChild(e1,e);
}

function maarCelleLidTyp() {
   var el=document.getElementsByName('m-box');
   while (el.length>0) {
      maarCelleLidTyp2(el[0]);
   }
}

