/*-----------------------------------------
 
 copyright 2009 by mmc
 
 Version: 1.0
 
 Author: Georg Paul
 
 Author URI: http://www.mmc-agentur.at
 
 -------------------------------------------*/


function log(msg) {
	window.console && window.console.log(msg);
}


$(document).ready(function(){
	
	
	// Profil hinzufügen 
	var profile_count = 1;
  
	$("#add_dog_profile").click(function(event){ 
		event.preventDefault();
		$(".dog_profile:last").after($(".dog_profile:last").outer());
		$(".dog_profile:last").attr("class", "dog_profile dog_profile_" + (profile_count++));
		
		var count = 0;
		
		$(".dog_profile:last").find(".prefix").each(function(){			
			var form_name = $(".dog_profile:first .prefix:eq(" + count + ")").attr("name");
			var form_ID = $(".dog_profile:first .prefix:eq(" + count + ")").attr("id"); 
			count++;			

			$(this).attr("name", form_name + "_" + (profile_count - 1)); 
			$(this).attr("id", form_ID + "_" + (profile_count - 1)); 
		})
	}); 
	
	
	
	
	// Datepicker Initialisierung
	
	if (!$.browser.msie && parseInt(jQuery.browser.version) != 6) {
		
		$(".cal-button").css("cursor", "pointer");
		
		$(function(){
			$("#datumswaehler_edit").datepicker({
				dateFormat: 'dd.mm.yy' 
			});
			$(".datumswaehler").datepicker({ 
				dateFormat: 'dd.mm.yy'
			});
			
			$('.lightbox').lightBox();
			
		});
	}
	
	// Definieren der click-Function für den Kalenderbutton
	// hier kommt .col nicht vor und muss deshalb spezifischer angesprochen werden
	$("#dog_diary .cal-button").click(function(){
		$(this).prev().focus();
	});
	$("#form_dog_diary_print .cal-button").click(function(){
		$(this).prev().focus();
	});	
	$("#form_voting_bewerbung .cal-button").click(function(){
		$(this).prev().focus();
		
		// für IE6 ausblenden
		$("#form_profil_rasse").css("visibility", "hidden");
	});
		// Select Feld onblur wieder einblenden
		$("#form_voting_bewerbung .cal-button").prev().blur(function(){
			$("#form_profil_rasse").css("visibility", "visible"); 
		});
	
	// .cal-button kommt in verschiedenen Umgebungen vor - .col ist ein Container, der viele Vorkommnisse abdeckt
	$(".col .cal-button").click(function(){
		var colTag = $(this).parent().prev();
		$(colTag).find("input").focus(); 
	});	
	
	
	
	
	
	// AGB's zeigen/verstecken
	$("#form_registrierung #agb_link").click(function(event){
		event.preventDefault();
		$("#agb").css("display", "block");		
	});
	$("#kontakt_form #agb_link").click(function(event){
		event.preventDefault();
		$("#agb").css("display", "block");
		$("#kontakt_form select").css("visibility", "hidden");
	});	
	$("#form_registrierung #close_agb_link").click(function(event){
		event.preventDefault();
		$("#agb").css("display", "none");
	});	
	$("#kontakt_form #close_agb_link").click(function(event){
		event.preventDefault();
		$("#agb").css("display", "none");
		$("#kontakt_form select").css("visibility", "visible");
	});	


	
	
	// Limits für die Inputfelder und Textareas setzen
	
	// Registrierung 
	limitChars($('#form_registrierung_vorname'), 30);
	limitChars($('#form_registrierung_nachname'), 30);
	limitChars($('#form_registrierung_strasse'), 40);
	limitChars($('#form_registrierung_email'), 30);
	limitChars($('#form_registrierung_vpasswort'), 16);
	limitChars($('#form_registrierung_passwort2'), 16);
	
	limitChars($('#form_profil_name'), 20);
	
	
	// Profil anlegen/bearbeiten
	limitChars($(".beschreibungstext"), 2000);
	
	// Hundetagebuch
	limitChars($('#email_recipient'), 30);
	limitChars($('#form_dog_diary_new_subject'), 30);
	limitChars($('#form_dog_diary_new_text'), 2000);
	
	// Hundefotoalbum
	limitChars($('#new_album_hl'), 70);
	limitChars($('#new_album_desc'), 3000);
	limitChars($('#album_single_comment_1'), 300);
	
	// Terminkalender
	limitChars($('#title_field'), 70);
	limitChars($('#datetext_field'), 2000);
	limitChars($('#date_location'), 200);
	
	// Voting
	limitChars($('#form_voting_bewerbung_name'), 20);
		




	
	// Photo Upload Tool
	$("#photo_preview_btn").click(function(event){
		event.preventDefault();	
		
		// append loadin animation
		$('#photo_selection').append('<div id="disappearing_bug_wrap"><span id="load"></span></div>');
		
		// create placeholder
		$("#page").append("<div id='user_photos_wrap'></div>");
		
		// load new content
		$("#user_photos_wrap").load("http://www.pedigree.at/index.php?id=811&mdl_all_my_fotos_ajax=1 #user_photos_preview", function(){ 
			
			// hide loading animation
			$('#disappearing_bug_wrap').remove();
			
			// hide select boxes
			$("select").css("visibility", "hidden");			
			
			$(".photo_row").find("a").each(function(){
				
				$(this).click(function(event){ 
					event.preventDefault(); 
					
					// get src-attribute of the clicked img
					$(this).find("img").each(function(){
						imgSrcThumb = $(this).attr("src");
						imgSrcHighRes = $(this).attr("alt");
					});
					
					// set hidden field
					$("#hundeprofil_bild_ajax").attr("value", imgSrcHighRes);						
																	
					// remove Popup
					$("#user_photos_wrap").remove();
														
					// preview Bild ändern 
					$("#profile_preview_img").attr("src", imgSrcThumb); 						
										
					// photo selection ausblenden / Reset Button einblenden
					$("#photo_selection").css("display", "none");				
					$("#reset_photo a").css("display", "block");										
					
					// set select boxes visible again
					$("select").css("visibility", "visible");
				});							
			});
			 
			// close button
			$("#close_user_photos a").click(function(event){ 
				event.preventDefault(); 
					// set select boxes visible again
					$("select").css("visibility", "visible");
					
					$("#user_photos_wrap").remove();
			}); 
			
		});
 
	});
	
	
	// read img source of the existing img
	if ($("#profile_preview_img").attr("src") != "") {
		profilePreviewImg = $("#profile_preview_img").attr("src"); 
	}
	
	// show upload button on change of the file-upload field
	$("#form_profil_upload").change(function(){
		if ($(this).val() != "") {			
			$("#hundeprofil_bild_preview_btn").show(); 
		}
	});
 

	$("#reset_photo").click(function(event){
		event.preventDefault();
		
		// hide loading animation
		$('#disappearing_bug_wrap').remove();	
			
		// empty hidden field
		$("#hundeprofil_bild_ajax").attr("value", "");
		
		// hide reset link, show photo selection interface
		$("#reset_photo a").css("display", "none"); 
		$("#photo_selection").css("display", "block");
		
		
		$("#profile_preview_img").attr("src", profilePreviewImg);
	}); 
 	
 	
 	
 	
 	//MDL Accordion 2011
 	$('.wrapper_2011 .accordion li .acc_content').hide();
 	$('.wrapper_2011 .accordion li .acc_hl')
 	.css('cursor', 'pointer')
 	.toggle(
 		function(){
 			$(this).parent()
 			.addClass('open')
 			.find('.acc_content')
 			.show('normal');
 		},
 		function(){
 			$(this).parent()
 			.removeClass('open')
 			.find('.acc_content')
 			.hide('normal');
 		}
 	);
 	

	
});




jQuery.fn.outer = function() {
  return $( $('<div></div>').html(this.clone()) ).html();
} 


function limitChars(object, limit) {
	$(object).keyup(function(){
		$(this).val($(this).val().substr(0,limit));
	});
	$(object).change(function(){
		$(this).val($(this).val().substr(0,limit));
	});	
}





/*jslint browser: true */ /*global jQuery: true */

/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

// TODO JsDoc

/**
* Create a cookie with the given key and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
* used when the cookie was set.
*
* @param String key The key of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given key.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String key The key of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function (key, value, options) {
    
    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        
        value = String(value);
        
        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
