/* extends Jquery auf exists */
jQuery.fn.exists = function(){return jQuery(this).length>0;}

$(document).ready(function() {
    
    /* INPUT VALUES  - START */
    $('input.del').focus(function(){
		
		var name = $(this).attr('name');
		var typ  = $(this).attr('type')
		var val  = this.value;
		var sval = $(this).data(name);
		
		if(val==sval && typ=='text' && typ!='radio' && typ!='submit' ){
			this.value='';
		}
		
    });
    
    $('input.del').blur(function(){
		
		var name = $(this).attr('name');
		
		if(this.value=='' && $(this).data(name)!=null){
			
			this.value = $(this).data(name);
		}
    });
    
    $('input.del').each(function() {
		
		if (this.value!='') {
			var name = $(this).attr('name');
			$(this).data(name, this.value);
		}
    });
    
    $('input._autoselect').focus();
	upload();
	checkbox();
    /*
	$('div.rahmen1 h1').css({'height':'45px'});
	$('div.rahmen1 div.bilder').css({'float':'none'});
	$('div.rahmen1 p').hide().css({'margin-left':'170px'});
	$('div.rahmen1').css({'width':'170px', 'height':'270px !important', 'overflow':'hidden', 'position':'absolute', 'margin':'5px', 'margin-right':'15px', 'background-color':'#fff'});
	$('div.rahmen1').each(function(key,val){
		var left = parseInt($(this).css('left'));
		left = 190*(key);
		$(this).css({'left':left+'px'}).attr('id',key);
	});
	
	$('div.rahmen1').hover(function(){
		var left = $(this).css('left');
		$(this).css({'z-index':'10','box-shadow':'0px 0px 4px #000', 'border-radius':'10px'});
		$(this).children().children('div.bilder').css({'float':'left'});
		
		$(this).stop(true, true).animate({
			width: '100%',
			left: '0px',
			padding: '10px'
		}, 400, function(){
			$(this).children('div.rahmen1 p').fadeIn(300);
		});
	}, function (){
		var left = parseInt($(this).attr('id'));
		left = 190*(left);
		$(this).children('div.rahmen1 p').hide();
		$(this).stop(true, true).animate({
			width: '170px',
			left: left+'px',
			padding: '0px'
		}, 400, function(){
			$(this).css({'z-index':'1','box-shadow':'0px 0px 0px #000', 'border-radius':'0px'});
		});
		
	});
	*/
	if($('.scroll_stop').exists()) {
		ScrollStop();
	}
});

Shadowbox.init({
	handleOversize: "resize"
});

$(function () {
  var msie6 = $.browser == 'msie' && $.browser.version < 7;
  if (!msie6) {
    var top = $('.scroll_stop').offset().top - parseFloat($('.scroll_stop').css('margin-top').replace(/auto/, 0));
    $(window).scroll(function (event) {
      var y = $(this).scrollTop();
      
      if (y >= top) {
		var width = $('.scroll_stop').width();
		$('.scroll_stop').addClass('fixed').width(width);
      } else {
		$('.scroll_stop').removeClass('fixed');
      }
    });
  }
});

function stopError() {
  return true;
}

window.onerror = stopError;


/* Scroll Stopper - STOP
 *
 *
 * JS - Cookies | lesen und schreiben - START */

function cookie_schreiben(wert,inhalt) {
    
   document.cookie = wert + "=" + inhalt;
   var check = document.cookie

   if (check && check.indexOf(wert) > -1) return true;
   else return false;
}

function cookie_lesen(wert) {
    
   var inhalt = document.cookie;
   var posName = inhalt.indexOf("; " + wert + "=");
   if (posName == -1) {
      if (inhalt.indexOf(wert + "=") == 0) posName = 0;
      else return null;
   }

   var wertAnfang = inhalt.indexOf("=", posName)+1;
   var wertEnde = inhalt.indexOf(";", posName+1);
   if (wertEnde == -1) wertEnde = inhalt.length;

   var wert = inhalt.substring(wertAnfang, wertEnde);
   return unescape(wert);
}
/* JS - Cookies | lesen und schreiben - STOP */

function ScrollStop () {
  var msie6 = $.browser == 'msie' && $.browser.version < 7;
  if (!msie6) {
    var top = $('.scroll_stop').offset().top - parseFloat($('.scroll_stop').css('margin-top').replace(/auto/, 0));
    $(window).scroll(function (event) {
      var y = $(this).scrollTop();
      
      if (y >= top) {
		var width = $('.scroll_stop').width();
		$('.scroll_stop').addClass('fixed').width(width);
      } else {
		$('.scroll_stop').removeClass('fixed');
      }
    });
  }
};

/* *****************************************************
 * File Upload Functionen
 * - upload() -
 * zum Starten und Optischen anpassen der Upload typen
 * *****************************************************/
function upload () {
	$('input[type=file]').each(function(){
		$(this).css({'display':'none','width':'1px'}).wrap('<span class="upload"></span>');
		$(this).parent().append('<img class="UploadImage" src="css/upload.png" onclick="FocusFile($(this));"/><span class="value"></span>');
	});
}

function FocusFile (element) {
	var input = element.parent().find('input');
	input.click();
	input.change(function(){
		$(this).parent().find('span.value').html($(this).attr('value'));
	});
}

/* *****************************************************
 * Checkboxes Functionen
 * -  checkbox() -
 * zum Starten und Optischen anpassen der Upload typen
 * *****************************************************/
function checkbox() {
	$('input[type=checkbox]').each(function(){
		$(this).css({'opacity':'0'}).wrap('<span class="checkbox"></span>');
		$(this).attr('onclick','FocusCheckbox($(this))');
		FocusCheckbox($(this));
	});
}

function FocusCheckbox(element) {
	if( element.attr('checked') === true ) {
		element.parent().css({'background-position':'0px -15px'});
	} else {
		element.parent().css({'background-position':'0px 0px'});
	}
}
