// --------------------------------------------------------------
// UNOBTRISUVE INITIALIZATION
// Author: Stefan Rausch
// Company: Skygate GmbH
// Created: 2009-12-23
// --------------------------------------------------------------

$(document).ready(function() {
	
  $.ajaxSetup ({
    // Disable caching of AJAX responses */
    cache: false
  });
  
  initjAlertConfirm();

});

function showhiw () {
	$('#hiw').show();
}

function hidehiw () {
	$('#hiw').hide();
}

function showis () {
	$('#is').show();
}

function hideis () {
	$('#is').hide();
}


//jAlert --------------------------------------------------- 
confirmed = false;
confirmed2 = false;
function initjAlertConfirm()
{

  
  // trigger a confirm whenever links of class confirm are pressed.
  $('a[rel=confirm]').click(function() { 
    link = $(this);
    jConfirm(link.attr('alt'), link.attr('title'), function(r) { if (r) { window.location.href = link.attr('href'); } });
    return false;
  });
  
  // trigger a confirm whenever input buttons of class confirm are pressed.
  $('input[rel=confirm]').click(function() { 
	button = $(this);
	if (!confirmed) {
      jConfirm($(this).attr('alt'), 'Attention', function(r) { if (r) { confirmed = r; button.trigger('click'); } });
      return false;
 	} else {
 	  return true;
 	}
    });
    
    // trigger a confirm whenever input buttons of class confirm are pressed.
  $('input[rel=confirm_action]').click(function() { 
	button = $(this);
	if (!confirmed) {
      jConfirm($(this).attr('alt'), 'Attention', function(r) { confirmed = true; if (!r) {confirmed2 = true}; button.trigger('click'); });
      return false;
    } else if (confirmed2) {
    	return true;
 	} else {
 	  var form = $(this).parents('form:first');
 	  form.append('<input type="hidden" name="confirmed" value="true" />');
 	  return true;
 	}
    });
}



//eMail authentication
function EMail(s)
{
 var a = false;
 var res = false;
 if(typeof(RegExp) == 'function')
 {
  var b = new RegExp('abc');
  if(b.test('abc') == true){a = true;}
  }

 if(a == true)
 {
  reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                   '(\\.)([a-zA-Z]{2,4})$');
  res = (reg.test(s));
 }
 else
 {
  res = (s.search('@') >= 1 &&
         s.lastIndexOf('.') > s.search('@') &&
         s.lastIndexOf('.') >= s.length-5)
 }
 return(res);
} 

function checkEmail (value, node) {
	
	if (value != "") {
		if (EMail(value)) {
			node.className = "";
		} else {
			node.className = "red";
		}
	} else {
		node.className = "";
	}
}


function showcharsleft (charstyped, nodeid, charsmax, nodeerror) {
	charstyped = charstyped * 1;
	charsmax = charsmax * 1;
	
	charsleft = charsmax - charstyped;
	document.getElementById(nodeid).innerHTML = charsleft;
	
	if (charsleft < 0) {
		document.getElementById(nodeid).className = "fontred";	
		document.getElementById(nodeerror).style.display = "";
	} else {
		document.getElementById(nodeid).className = "";
		document.getElementById(nodeerror).style.display = "none";
	}
}



//Slideshow
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });

    // Navigation
    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
    
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

};

function itemVisibleIn(carousel, li, index, state)
{
    $(".jcarousel-control a").css("color", "#333333");
    $(".jcarousel-control a:nth-child(" + index + ")").css("color", "#FF6600");
}

jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        auto: 8,
        wrap: 'last',
        scroll: 1,
        animation: 'slow',
        buttonNextHTML: null,
        buttonPrevHTML: null,
        itemVisibleInCallback: itemVisibleIn,
        initCallback: mycarousel_initCallback
    });
});

//Old Slideshow
function slideSwitch($position) {
	
	if ($('#slideoption').html() == 'true' || $position > 0) {
		
		if ($position > 0) {
			$('#slideshow DIV:nth-child').removeClass('active last-active');
			$('#slideshownav SPAN:nth-child').removeClass('active last-active');
			
			if ($position == 1) {
				$('#slideshow DIV:first').addClass('active');
				$('#slideshownav SPAN:first').addClass('active');
			} else {
				$position = $position -1;
				
				$('#slideshow DIV:eq(' + $position + ')').addClass('active');
				$('#slideshownav SPAN:eq(' + $position + ')').addClass('active');
			}
		} else {
			slideSwitchDo();
		}
	}
}

function slideSwitchDo () {
    var $active = $('#slideshow DIV.active');
    var $spanactive = $('#slideshownav SPAN.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
    if ( $spanactive.length == 0 ) $spanactive = $('#slideshownav SPAN:last');

    var $next = $active.next().length ? $active.next()
        : $('#slideshow DIV:first');
    
    var $spannext = $spanactive.next().length ? $spanactive.next()
        : $('#slideshownav SPAN:first');    

    $active.addClass('last-active');
    $spanactive.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
    $spannext.css({opacity: 1.0})
	    .addClass('active')
	    .animate({opacity: 1.0}, 1000, function() {
	    	$spanactive.removeClass('active last-active');
	    });
}

$(function() {
    setInterval( "slideSwitch()", 10000 );
});