$.ajaxSetup ({
    // Disable caching of AJAX responses */
    cache: false
});

$(document).ready(function() {

var theHtml = '<a class="tooltipLinkFB" style="margin-right:7px;" href="#"><img src="images/button_facebook.gif"></a><div id="fbTooltip" class="tooltipIndex" style="text-align:left;position:absolute;top:1500px;left:178px;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.dubistderboss.de&layout=button_count&show_faces=false&width=200&action=like&font=arial&colorscheme=dark&height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:20px;" allowTransparency="false"></iframe><a name="fb_share" type="button_count" href="http://www.facebook.com/sharer.php">Teilen</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div><a class="tooltipLinkTW" href="#"><img src="images/button_twitter.gif"></a><div id="twTooltip" class="tooltipIndex" style="text-align:left;position:absolute;top:1500px;left:220px;"><div style="text-align:left;height:30px;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div>';
$('#socialBookmarks').html(theHtml);

//// Tooltips
// Facebook
$('.tooltipLinkFB').bind('mouseover', function() {
    $('#fbTooltip').css({"opacity" : "1", "top" : "500px" , "left" : "178px"});
});
$('#fbTooltip').bind('mouseover', function() {
    $('#fbTooltip').css({"opacity" : "1", "top" : "500px" , "left" : "178px"});
});
$('#fbTooltip').bind('mouseout', function() {
    $('#fbTooltip').css({"opacity" : "0", "top" : "1500px", "left" : "178px"});
});

// Twitter
$('.tooltipLinkTW').bind('mouseover', function() {
    $('#twTooltip').css({"opacity" : "1", "top" : "500px" , "left" : "220px"});
});
$('#twTooltip').bind('mouseover', function() {
    $('#twTooltip').css({"opacity" : "1", "top" : "500px" , "left" : "220px"});
});
$('#twTooltip').bind('mouseout', function() {
    $('#twTooltip').css({"opacity" : "0", "top" : "1500px", "left" : "220px"});
});



//// Tooltips
//$(".tooltipLink").tooltip({
//    effect: 'slide',
//    position: 'top center',
//    delay:250,
//    relative:true,
//    offset: [0,0]
//    });



	// if the function argument is given to overlay,
	// it is assumed to be the onBeforeLoad event listener
	$("a[rel]").overlay({

		mask: 'darkred',
		effect: 'apple',

		onBeforeLoad: function() {

			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".contentWrap");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}

	});



//loopLasche();
loopGlow();

// Scrollen einschalten
//$(".scrollable").scrollable({
//    next: '.xyz',
//    prev: 'zyx',
//    speed:800,
//    vertical: true,
//    mousewheel: true
//    }).navigator({
//	// ul.tabs now functions as our navigator
//	navi:'ul#mainNavi'
//});

//$('BODY').localScroll();

if ($.browser.opera) {
    // do nothing
}
else {
$($.localScroll({
hash: true
}));
}

// Contenthöhe anpassen
var documentHeight = $(document).height();
var viewportHeight = $(window).height();
//$('#rightCol').height(viewportHeight);
$('#shadowRight').height(documentHeight + 40);

var hoeheLinks = viewportHeight - 359 - 229;
$('#leftFill').height(hoeheLinks);


$(window).bind("resize", resizeWindow);
function resizeWindow( e ) {
    var viewportHeight = $(window).height();
    var hoeheLinks = viewportHeight - 359 - 229;
    $('#leftFill').height(hoeheLinks);
}

// Aktuelle Einsendungen

$(".scrollableData").scrollable({
    speed:1500,
    items: '.itemsData'
    });


//$('.juryInner').bind('click', function() {
//    var theId = this.id;
//    alert(theId);
//});


$('.juryImg').hover(
  function () {
    if($(this).hasClass('noOpacity') == false) {
        $(this).css({opacity:1});
    }
  },
  function () {
    if($(this).hasClass('noOpacity') == false) {
        $(this).css({opacity:0.5});
    }
  }
);


/*
$('.juryImg').hover(
  function () {
    if($(this).hasClass('noOpacity') == false) {
        $(this).animate({opacity:1});
    }
  },
  function () {
    if($(this).hasClass('noOpacity') == false) {
        $(this).animate({opacity:0.5});
    }
  }
);
*/

// MouseOver Lasche
$('#lasche').hover(
  function () {
    $(this).stop().animate({top:0}, 100);
  },
  function () {
    $(this).stop().animate({top:-15}, 100);
    //loopLasche();
  }
);

// MouseOver bewerbungLasche
$('#bewerbungLasche').hover(
  function () {
    $(this).stop().animate({bottom:-56}, 100);
  },
  function () {
    $(this).stop().animate({bottom:-51}, 100);
  }
);

// Bewerbungsformular einblenden
$('#lasche').bind('click', function() {
    //showBewerbung();
});

$('#bewerbungLasche').bind('click', function() {
  $('#bewerbung').animate({ top: -610 }, 500, function() {$('#lasche').show();});
});


// Hightlight Navi
$('#mainNavi A').bind('click', function() {
    $('#mainNavi A').removeClass('active');
    $(this).addClass('active');
});

function highlight(section) {
    $('#mainNavi A').removeClass('active');
    $('#a' + section).addClass('active');
}

// Scrollen überwachen
$(window).scroll(function () { 
    var winPos = $(window).scrollTop();
    if (winPos > 0 && winPos <= 315) highlight('Home');
    if (winPos > 315 && winPos <= 573) highlight('Einsendungen');
    if (winPos > 573 && winPos <= 1184) highlight('Aktion');
    if (winPos > 1184 && winPos <= 1526) highlight('Anlass');
    // if (winPos > 1526 && winPos <= 2153) highlight('Regularien');
    // if (winPos > 2153 && winPos <= 2800) highlight('Jury');
    if (winPos > 1700 && winPos <= 2236) highlight('Jury');
    //if (winPos > 2704 && winPos <= 3250) highlight('Treffpunkt');
    if (winPos > 2130) highlight('Presse');
});











});// OnLoad

// Position anzeigen
function whereAmI () {
    var winPos = $(window).scrollTop();
    alert(winPos);
}

// Laschenloop
function loopLasche() {
    $('#lasche').animate({
    top: -10
  }, 300, function() {
    $('#lasche').animate({
    top: -15
  }, 300, function() {
    $('#lasche').animate({
    top: -10
  }, 300, function() {
    $('#lasche').animate({
    top: -15
  }, 300, function() {
    $('#lasche').animate({
    top: -15
  }, 5000, function() {
    loopLasche();
  });
  });
  });
  });
  });
}

// kopierter Laschenloop als Farbloop umgebaut
function loopGlow() {
    $('#glow').animate({
    color: "#88ae1f"
  }, 200, function() {
    $('#glow').animate({
    color: "white"
  }, 200, function() {
    $('#glow').animate({
    color: "#88ae1f"
  }, 200, function() {
    $('#glow').animate({
    color: "white"
  }, 200, function() {
    $('#glow').animate({
    color: "#88ae1f"
  }, 3000, function() {
    loopGlow();
  });
  });
  });
  });
  });
}

// bewerbung
function showBewerbung() {
  $('#lasche').hide();
  $('#bewerbung').animate({ top: 0 }, 500, function() {
        getContent('bewerbungsformular.php', '#bewerbungContent');
    });
}

// Jury
function showJury(jury, newPosition) {
    $('.juryImg').removeClass('noOpacity');
    $('.juryImg').removeClass('activated');
    $('.juryText').hide();
    var newContent = $('#juryText' + jury).html();
    //$('#juryText' + jury).fadeIn("slow");
    $('#juryText0').html(newContent);
    $('#juryText0').show();
    $('#klammer').animate({
    left: newPosition
  }, 500, function() {
    // Animation complete.
  });
    $('#juryImg' + jury).addClass('activated');
}

/*
 *	Holt den Inhalt vom Server
 */
function getContent(theUrl, container) {
	$.ajax({
	url: theUrl,
	success: function(html) {
		//Inhalt reinschreiben und anzeigen
		$(container)
			.html(html)
	}
	});
}

 function limitChars(textid, limit, infodiv) {
    var text = $('#'+textid).val(); 
    var textlength = text.length;
    if(textlength > limit) {
        $('#' + infodiv).addClass('overLimit');
        $('#'+textid).val(text.substr(0,limit));
        return false;
    }
    else {
        $('#' + infodiv).removeClass('overLimit');
        $('#' + infodiv).html((limit - textlength));
        return true;
    }
}

function ObjectPosition(obj) {
    var curleft = 0;
      var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curleft += obj.offsetLeft;
                  curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
      }
      return [curleft,curtop];
}

function showHint(theId, theText, kurztext) {
    if(kurztext != null) {
        theText = kurztext;
    }
    var positionOfElement = $(theId).offset();
    $('BODY').prepend('<div id="inputHint">' + theText + '</div>');
    $('#inputHint').offset(positionOfElement);
    var oldPosition = ObjectPosition(document.getElementById('inputHint'));
    var newPosition = oldPosition[1] - 20;
    $('#inputHint').css("top" , newPosition);
}

// Vorbelegung Input-Felder
$.fn.extend({
    toggleValue : function (defaultText, kurztext) {
        return this.each(function() {
            $(this).focus(function() {
                showHint(this, defaultText, kurztext);
                if ($(this).val() == defaultText) {
                    $(this).val('');
                    $(this).addClass('active');
                }
            $(this).blur(function () {
                $('#inputHint').remove();
                if ($.trim($(this).val()) == '') {
                    $(this).val(defaultText);
                    $(this).removeClass('active');
                }
            });
        });
    });
}
});

function highlight(field) {
    $('#' + field).addClass('warning');
}

function unHighlight(field) {
    $('#' + field).removeClass('warning');
}

function formValid() {
    var fehler;
    // Vorname
    if($('#vorname').val() == 'Vorname') {
        highlight('vorname');
        var fehler = true;
    }
    else {
        unHighlight('vorname');
    }
    // Nachname
    if($('#nachname').val() == 'Nachname') {
        highlight('nachname');
        var fehler = true;
    }
    else {
        unHighlight('nachname');
    }
    // Telefon
    if($('#telefon').val() == 'Telefon') {
        highlight('telefon');
        var fehler = true;
    }
    else {
        unHighlight('telefon');
    }
    // E-Mail
    if($('#email').val() == 'E-Mail' || checkEmail($('#email').val()) == false) {
        highlight('email');
        var fehler = true;
    }
    else {
        unHighlight('email');
    }
    // Geburtsdatum
    if($('#geburtsdatum').val() == 'Geburtsdatum') {
        highlight('geburtsdatum');
        var fehler = true;
    }
    else {
        unHighlight('geburtsdatum');
    }
    // Firmenname
    if($('#firmenname').val() == 'Vollständiger Firmenname') {
        highlight('firmenname');
        var fehler = true;
    }
    else {
        unHighlight('firmenname');
    }
    // Mitarbeiter
    if($('#mitarbeiter').val() == 'Anzahl der Mitarbeiter') {
        highlight('mitarbeiter');
        var fehler = true;
    }
    else {
        unHighlight('mitarbeiter');
    }
    // Schlagworte
    if($('#schlagworte').val() == 'Fünf Schlagworte die Deine Firma beschreiben') {
        highlight('schlagworte');
        var fehler = true;
    }
    else {
        unHighlight('schlagworte');
    }
    // Firmenadresse
    if($('#firmenadresse').val() == 'Firmenadresse') {
        highlight('firmenadresse');
        var fehler = true;
    }
    else {
        unHighlight('firmenadresse');
    }
    // Branche
    if($('#branche').val() == 'Branche') {
        highlight('branche');
        var fehler = true;
    }
    else {
        unHighlight('branche');
    }
    // Gründungsdatum
    if($('#gruendungsdatum').val() == 'Gründungsdatum') {
        highlight('gruendungsdatum');
        var fehler = true;
    }
    else {
        unHighlight('gruendungsdatum');
    }
    // PLZ, Stadt
    if($('#ort').val() == 'PLZ, Stadt') {
        highlight('ort');
        var fehler = true;
    }
    else {
        unHighlight('ort');
    }
    // Kurzbeschreinbung
    if($('#kurzbeschreibung').val() == 'Kurzbeschreibung Deines Geschäftsmodells') {
        highlight('kurzbeschreibung');
        var fehler = true;
    }
    else {
        unHighlight('kurzbeschreibung');
    }
    // Warum?
    if($('#warum').val() == 'Warum sollten wir Dich auswählen?') {
        highlight('warum');
        var fehler = true;
    }
    else {
        unHighlight('warum');
    }
    // Bild 1
    if($('#value_upload_area1').val() == '') {
        $('#upload_area1').html('<img src="images/warning.png">');
        var fehler = true;
    }
    // Bild 4
    if($('#value_upload_area4').val() == '') {
        $('#upload_area4').html('<img src="images/warning.png">');
        var fehler = true;
    }
    // Teilnahmebedingunge
    if($('#checkBedingungen').hasClass('active') == false) {
        $('#checkWarning').show();
        var fehler = true;
    }
    if(fehler) {
        return false;
    }
    else {
        return true;
    }
}

// E-Mail-Adresse checken
function checkEmail(x) {
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    return filter.test(x);
}

// Preview
function gotoPreview() {
    if(formValid()) {
        $('#bewerbungSubmit').append('<p id="previewGenerating" style="margin-top:10px;">Vorschau wird generiert...</p>');
        $.ajax({
          type: "POST",
          url: "bewerbungsformular_send.php",
          data: "vorname=" + $('#vorname').val() + "&nachname=" + $('#nachname').val() + "&telefon=" + $('#telefon').val() + "&email=" + $('#email').val() + "&geburtsdatum=" + $('#geburtsdatum').val() + "&website=" + $('#website').val() + "&firmenname=" + $('#firmenname').val() + "&schlagworte=" + $('#schlagworte').val() + "&firmenadresse=" + $('#firmenadresse').val() + "&branche=" + $('#branche').val() + "&gruendungsdatum=" + $('#gruendungsdatum').val() + "&ort=" + $('#ort').val() + "&kurzbeschreibung=" + $('#kurzbeschreibung').val() + "&warum=" + $('#warum').val() + "&video1=" + $('#value_upload_areaVideo').val() + "&pic1=" + $('#value_upload_area1').val() + "&pic2=" + $('#value_upload_area2').val() + "&pic3=" + $('#value_upload_area3').val() + "&personal_pic=" + $('#value_upload_area4').val() + "&user=" + $('#user').val() + "&mitarbeiter=" + $('#mitarbeiter').val(),
          success: function(msg){
              if(msg == 'OK') {
                  showPreview($('#user').val());
              }
          }
        });
    }
}

// Abspeichern
function saveProject() {
    var user = $('#previewUser').val();
    $.ajax({
      type: "POST",
      url: "bewerbungsformular_save.php",
      data: "user=" + user,
      success: function(msg){
          if(msg == 'OK') {
              getContent('bewerbungsformular_danke.php', '#bewerbungPreview');
              $('#bewerbungLasche').html('Schließen');
          }
      }
    });
}

function showPreview(userId) {
    $('#bewerbung').append('<div id="bewerbungPreview"></div>');
    getContent('bewerbungsformular_preview.php?user=' + userId, '#bewerbungPreview');
    $('#bewerbungPreview').fadeIn('slow');
    $('#bewerbungContent').hide();
    $('#previewGenerating').remove();
}

// Checkbox | Teilnahmebedingungen
function toggleCheckbox() {
    if ($('#teilnahmebedingungen').val() == '') {
        $('#teilnahmebedingungen').val('yes');
        $('#checkBedingungen').addClass('active');
        $('#checkWarning').hide();
    }
    else {
        $('#teilnahmebedingungen').val('');
        $('#checkBedingungen').removeClass('active');
    }
}

function previewInfo() {
    $('#previewBilder').hide();
    $('#previewText').show();
    $('#btnInfo').addClass('active');
    $('#btnPics').removeClass('active');
}

function previewPics() {
    $('#previewText').hide();
    $('#previewBilder').show();
    $('#btnPics').addClass('active');
    $('#btnInfo').removeClass('active');
}

function showSingleProject(projectId, offset) {
    getContent('einsendungen.php?id=' + projectId + '&offset=' + offset, '#overlayContent');
}

function backToList(offset) {
    getContent('alle_einsendungen.php?offset=' + offset, '#overlayContent');
}

function jumpTo(offset) {
    getContent('alle_einsendungen.php?offset=' + offset, '#overlayContent');
}

function createUploader(){      
    var uploader = new qq.FileUploader({
        element: document.getElementById('videoUploader'),
        action: 'video/server/perl.cgi',
            params: {
                dateiname: 'test.avi'
    }

    });           
}

function showVotes() {
        $('.voteWrap').html('');
	$("#facebox").remove();
	$('#overlayContent').append('<div id="facebox"><div class="voteWrap"></div></div>');
	$("#facebox").overlay({
	    left: 400,
	    top: 100,
	    oneInstance: false,
	    load: true,
		onBeforeLoad: function() {
		        $('.voteWrap').html('');
			$('.voteWrap').load('votes.php');
		}
	});
	
}

function vote(project) {
        $('.voteWrap').html('');
	$("#facebox").remove();
	$('#overlayContent').append('<div id="facebox"><div class="voteWrap"></div></div>');
	$("#facebox").overlay({
	    left: 400,
	    top: 100,
	    oneInstance: false,
	    load: true,
		onBeforeLoad: function() {
		        $('.voteWrap').html('');
			$('.voteWrap').load('vote.php?id=' + project);
		}
	});
	
}

function sendVote() {
        var theProject = $('#project').val();
        var captchaText = $('#captchaText').val();
       $.ajax({
         type: "POST",
         url: "vote_send.php",
         data: "id=" + theProject + "&captchaText=" + captchaText,
         success: function(msg){
//               $('.voteWrap').prepend(msg);
             if(msg == 'OK') {
                   //$('.voteWrap').html('<h2>Deine Stimme wurde gezählt</h2><p>Vielen Dank!</p>');
                   $('.voteWrap').load('votes.php?voted=true&votedFor=' + theProject);
             }
             else {
                   $('.voteError').html('<p>Leider ist die von Dir eingegebene Zeichenfolge falsch.<br>Bitte versuche es noch einmal.</p>');
             }
             if (msg == 'IP') {
                   $('.voteError').html('<p>Über Deine IP-Adresse wurde bereits für diesen Kandidaten gevotet.<br>Bitte versuche es in ein paar Minuten noch einmal.</p>');
             }
         }
       });
}
