whichtodo = null;
userID = null;
user_permission = null;
sso_profil = null;
jQuery(document).ready(function()
{
	
	
    jQuery('.paid2').bind('click', function( e )
    {
    	whichtodo = 'paid2';
		// Link erstmal deaktivieren, damit die �berpr�fung, ob der User eingeloggt ist, ausgef�hrt werden kann
        e.preventDefault();
        scrollTo(0,0);
        // Checken, ob die User eingeloggt ist

   
        jQuery.ez( 'ezjscajax::getUserID', {}, function( data )
	    {
        	_set_interface();
	        if ( data.error_text ) {
	            jQuery( '#error' ).html( data.error_text );
		        } else {
		        	if(data.content.userid != 0) {
		        	   userID = data.content.userid;
		           } 
		        	 info_abos();
		        }
		    });
    });
    
    jQuery('.paid').bind('click', function( e )
    {
    	/*
    	whichtodo = 'paid';

    	if(user_permission == 1) {
    		return true;
    	} else {
    		e.preventDefault();
    		
        	var link = null;
        	if(e.target.nodeName.toLowerCase() == "img")
        		link = jQuery(e.target).parent();
        	else
        		link = jQuery(e.target);
        	
        	var tr = jQuery(link).closest('tr');
        	var div = tr.find('div.paid-teaser');
        	
        	var htmlTeaser = div.html().replace(/<a(.*)\">/g, '<h1>');
        	htmlTeaser = htmlTeaser.replace(/<\/a>/g, '</h1>');
        	htmlTeaser = htmlTeaser.replace(/\(.*\%\)/g, '');
        	htmlTeaser = htmlTeaser.replace(/<br>/g, '<p>');
        	htmlTeaser = htmlTeaser + '</p>';
    		
    		scrollTo(0,0);
    		_set_interface();
    		open_layer_suche(htmlTeaser);
    		return false;
    	}
    	*/
    	//e.preventDefault();
    
    	e.preventDefault();	
    	jQuery.ez( 'ezjscajax::checkPermission', {}, function( data ) {
    		
    		if(data.content.permission == 1) {
    			user_permission = 1;
    			//document.location = e.target.href;
    			document.location = jQuery(e.currentTarget).attr('href');
    			return true;
    		} else {        		
            	var link = null;
            	if(e.target.nodeName.toLowerCase() == "img")
            		link = jQuery(e.target).parent();
            	else
            		link = jQuery(e.target);
            	
            	var tr = jQuery(link).closest('tr');
            	var div = tr.find('div.paid-teaser');
            	var htmlTeaser = div.html().replace(/<a(.*)\">/ig, '<h1>');
            	htmlTeaser = htmlTeaser.replace(/<\/a>/ig, '</h1>');
            	htmlTeaser = htmlTeaser.replace(/\(.*\%\)/ig, '');
            	htmlTeaser = htmlTeaser.replace(/<br>/ig, '<p>');
            	htmlTeaser = htmlTeaser + '</p>';
        		
        		scrollTo(0,0);
        		_set_interface();
        		open_layer_suche(htmlTeaser);
        		return false;
        	}
    		
    	});		
    });
   
    
    jQuery( '#content-passwort_vergessen' ).click( function( e ) {
    	passwort_vergessen();
    });
    jQuery( '.paid_login' ).click( function( e ) {
    	check_user_login_detail();
    });
    
});



function paid_content_profil() {
	sso_profil = true;
	jQuery.ez( 'ezjscajax::getUserID', {}, function( data )
    {
    	_set_interface();
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
	        } else {
	        	if(data.content.userid != 0) {
	        	   userID = data.content.userid;
	           } 
	        	 info_abos();
	        }
	 });
}

function reloadpaid() {
	window.location.reload();
}
function open_layer_suche(teaser) {
	jQuery.ez( 'ezjscajax::open_layer_suche', {'teaser': teaser}, function( data ) {
		if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
        	jQuery( '#lightbox-details' ).html( data.content.template ).show();
        }
		
		jQuery( '#lightbox-passwort_vergessen' ).click( function( e ) {
	    	passwort_vergessen();
	    });
	
		jQuery('.paid2').bind('click', function( e ){
	        e.preventDefault();
	        scrollTo(0,0);
	        jQuery.ez( 'ezjscajax::getUserID', {}, function( data ) {
		        if ( data.error_text ) {
		            jQuery( '#error' ).html( data.error_text );
		        } else {
		        	if(data.content.userid != 0) {
		        	   userID = data.content.userid;
		            } 
		        	info_abos();
		        }
			});
	    });
	});	
}

function check_user_login_detail() 
{
	//scrollTo(0,0);
	jQuery.ez( 'ezjscajax::postUserLoginDetail', {'username': jQuery('#username').val(), 'password': jQuery('#password').val()}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
        	if(data.content.logedin == 1 ){
        		if (whichtodo == 'paid2')
        		{
        			window.location.reload();
        		} else{
        			window.location.reload();
        			//user_permission = 1;
        			//return true;
        		// alert('davor');
        		// return false;
        		}
        		//jQuery.ez( 'ezjscajax::hookLogin', {}, function( data ) {
        		//alert('drin');
        		
        		
        		//});
        		//alert('danach');
        	} else if(data.content.logedin == 2){
                jQuery.ez( 'ezjscajax::getUserID', {}, function( data ) {
   		         if ( data.error_text ) {
   		            jQuery( '#error' ).html( data.error_text );
   			     } else {
   			        if(data.content.userid != 0) {
   			        	userID = data.content.userid;
   			        } 
   			    }
   		       
               });
              jQuery( '#container_not_logged_in_replace' ).html( data.content.template ).show();
        	} else if(data.content.errors) {
        		jQuery( '#formError' ).html( data.content.errors ).show();
        	}
        }
    });
}

/*
function check_user_login_detail() 
{
	
	//scrollTo(0,0);
	jQuery.ez( 'ezjscajax::postUserLoginDetail', {'username': jQuery('#username').val(), 'password': jQuery('#password').val()}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
        	if(data.content.logedin == 1){
        		 window.location.reload();
        	} else if(data.content.logedin == 2){
                 jQuery.ez( 'ezjscajax::getUserID', {}, function( data ) {
    		         if ( data.error_text ) {
    		            jQuery( '#error' ).html( data.error_text );
    			     } else {
    			        if(data.content.userid != 0) {
    			        	userID = data.content.userid;
    			        } 
    			    }
                });
        	} 
        	jQuery( '#container_not_logged_in_replace' ).html( data.content.template ).show();
        }
    });
}
*/
function passwort_vergessen()
{
	scrollTo(0,0)
	_set_interface_pw();
    jQuery.ez( 'ezjscajax::getPasswortVergessen', {}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
            jQuery( '#lightbox-details-pw' ).html( data.content.template ).show();
        }
        jQuery( '#lightbox-close-pw' ).click( function() 
	    {
	        _finish_pw();
	        return false;
	    });
    });
    
}

function passwort_vergessen_post()
{
	scrollTo(0,0);
	jQuery.ez( 'ezjscajax::postPasswortVergessen', {'email': jQuery('#email').val()}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
        	if(data.content.error) {
        		jQuery( '#formErrorPW' ).html( data.content.error ).show();
        	} else if(data.content.message){
        		jQuery( '#formPostPW' ).html( data.content.message ).show();
        	}
        }
    });
}


function info_abos()
{
	scrollTo(0,0);
    jQuery.ez( 'ezjscajax::getInfoAbos', {}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
            jQuery( '#lightbox-details' ).html( data.content.template ).show();
            
            jQuery( '#lightbox-bestellung-monatsabo' ).click( function( e ) {
            	getBestellformular("Monatsabo",0);
            });
            jQuery( '#lightbox-bestellung-jahresabo' ).click( function( e ) {
            	getBestellformular("Jahresabo",0);
            });
       }
    });
}

function info_abos_aendern()
{
	scrollTo(0,0);
    jQuery.ez( 'ezjscajax::getInfoAbosAendern', {}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
            jQuery( '#lightbox-details' ).html( data.content.template ).show();
            
            jQuery( '#lightbox-bestellung-monatsabo' ).click( function( e ) {
            	getBestellformular("Monatsabo", 2);
            });
            jQuery( '#lightbox-bestellung-jahresabo' ).click( function( e ) {
            	getBestellformular("Jahresabo", 2);
            });
       }
    });
}

function getBestellformular(aboform, step) {
	jQuery.ez( 'ezjscajax::writeAboform', {'aboform': aboform}, function( data ) {
		 jQuery.ez( 'ezjscajax::getUserID', {}, function( data ) {
		        if ( data.error_text ) {
		            jQuery( '#error' ).html( data.error_text );
		        } else {
		        	if(data.content.userid != 0 || step == 2) {
		        		bestellung_step2();
		            } else {
		    	 		bestellung_step1();
		    	 	}
		        }
			});
	});
}

function bestellung_step1()
{
	scrollTo(0,0);
    jQuery.ez( 'ezjscajax::getBestellungStep1', {}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
            //jQuery( '#login' ).hide();
            jQuery( '#lightbox-details' ).html( data.content.template ).show();
            jQuery( '#lightbox-passwort_vergessen' ).click( function( e ) {
            	passwort_vergessen();
            });
        }
    });
}

function bestellung_step1_post()
{
	scrollTo(0,0);
	jQuery.ez( 'ezjscajax::postBestellungStep1', {'todo': jQuery('input:radio[name=sso]:checked').val(), 'username': jQuery('#login_username').val(), 'passwort': jQuery('#login_passwort').val()}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
        	if(data.content.step) {
        		if(data.content.step == 1) {
        			if(data.content.error) {
                		jQuery( '#formError2' ).html( data.content.error ).show();
                	}
        		} else if(data.content.step == 2) {
        			jQuery.ez( 'ezjscajax::hookBestellungStep1', {}, function( data ) {
        				bestellung_step2();
        				});
        			}
        	} 
        	if(data.content.error) {
        		jQuery( '#formError2' ).html( data.content.error ).show();
        	} 
        }
        if(data.content.error2) {
    		jQuery( '#formError2' ).html( data.content.error2 ).show();
    	} else {
    		jQuery( '#formError2' ).hide();
    	}
    });
}

function bestellung_step2()
{
	scrollTo(0,0);
    jQuery.ez( 'ezjscajax::getBestellungStep2', {}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
        	jQuery( '#jquery-lightbox' ).css(
		    {
		        width:              600,
		        height:             1250,
		        top:                100,
		        left:               100
		    }).show();
            jQuery( '#lightbox-details' ).html( data.content.template ).show();
            jQuery( '#lightbox-bestellung-step3' ).click( function( e ) {
            	bestellung_step3();
            });
        }
    });
}

function bestellung_step2_post()
{
	scrollTo(0,0);
	jQuery.ez( 'ezjscajax::postBestellungStep2', {'printabonnent': jQuery('input:radio[name=printabonnent]:checked').val(), 
												  'abonr': jQuery('#abonr').val(),
												  'company': jQuery('#company').val(),
												  'anrede': jQuery('#anrede').val(),
												  'vorname': jQuery('#vorname').val(),
												  'nachname': jQuery('#nachname').val(),
												  'strasse': jQuery('#strasse').val(),
												  'plz': jQuery('#plz').val(),
												  'ort': jQuery('#ort').val(),
												  'country_id': jQuery('#country_id').val(),
												  'email': jQuery('#email').val(),
												  'telephone': jQuery('#telephone').val(),
												  'fax': jQuery('#fax').val(),
												  'reg_username': jQuery('#reg_username').val(),
												  'reg_passwort': jQuery('#reg_passwort').val(),
												  'reg_passwort2': jQuery('#reg_passwort2').val()
												 }, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
        	if(data.content.step) {
        		if(data.content.step == 3) {
        			bestellung_step3();
        		}
        	} else {
        		
	        	jQuery( '#lightbox-details' ).html( data.content.template ).show();
        	}
        }
    });
}

function bestellung_step3()
{
	scrollTo(0,0);
    jQuery.ez( 'ezjscajax::getBestellungStep3', {}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
            //jQuery( '#login' ).hide();
        	jQuery( '#jquery-lightbox' ).css(
		    {
		        width:              600,
		        height:             500,
		        top:                100,
		        left:               100
		    }).show();
            jQuery( '#lightbox-details' ).html( data.content.template ).show();
            jQuery( '#lightbox-aboform-aendern' ).click( function( e ) {
            	info_abos_aendern();
            });
            jQuery( '#lightbox-bestellung-aendern' ).click( function( e ) {
            	bestellung_step2();
            });
        }
    });
}

function bestellung_step3_post()
{
	scrollTo(0,0);
	jQuery.ez( 'ezjscajax::postBestellungStep3', {'abo_bedingungen': jQuery('#abo_bedingungen:checked').val(), 
												  'abo_datenschutz': jQuery('#abo_datenschutz:checked').val()												
												 }, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
        	if(data.content.step) {
        		if(data.content.step == 4) {
        			
        			// Checken, ob die User eingeloggt ist
        			 jQuery.ez( 'ezjscajax::getUserID', {}, function( data )
				    {
				        if ( data.error_text ) {
				            jQuery( '#error' ).html( data.error_text );
					        } else {
					        	if(data.content.userid != 0) {
					        	   userID = data.content.userid;
			        	           bestellung_abgeschlossen();
					           } else {
					        	   bestellung_danke();
					           }
					        }
					    });
        		}
        	} else {
	        	jQuery( '#lightbox-details' ).html( data.content.template ).show();
        	}
        }
    });
}


function bestellung_abgeschlossen () 
{
	 scrollTo(0,0);
	 jQuery.ez( 'ezjscajax::getBestellungAbgeschlossen', {}, function( data ) {
	        if ( data.error_text ) {
	            jQuery( '#error' ).html( data.error_text );
	        } else {
	            jQuery( '#lightbox-secNav-btnClose' ).show();
	            jQuery( '#lightbox-secNav-btnClose' ).click( function() 
        	    {
	            	
        	        _finish();
        	        window.location.reload();
        	        return false;
        	    });
	            jQuery( '#lightbox-details' ).html( data.content.template ).show();
	        }
	    });
}

bestellungstimer = null;

function bestellung_danke()
{
	scrollTo(0,0);
    jQuery.ez( 'ezjscajax::getBestellungDanke', {}, function( data ) {
        if ( data.error_text ) {
            jQuery( '#error' ).html( data.error_text );
        } else {
        	if(data.content.clicked) {
        		clearInterval(bestellungstimer);
        		bestellung_abgeschlossen();
        	} else {
	            jQuery( '#lightbox-secNav-btnClose' ).hide();
	            jQuery( '#lightbox-details' ).html( data.content.template ).show();
        	}
        	
        }
    });
   if(bestellungstimer == null) { 
	   bestellungstimer = setInterval("bestellung_danke()", 5000);
   }
}

function showForm(radioValue) {
	if(radioValue == "LoginFRM") {
		document.getElementById("LoginFRM").style.display = "block";
		document.getElementById("login").checked = true;
		document.getElementById("register").checked = false;
		//document.getElementById("RegisterFRM").style.display = "none";
	} else if(radioValue == "RegisterFRM") {
		document.getElementById("login").checked = false;
		document.getElementById("register").checked = true;
		document.getElementById("LoginFRM").style.display = "none";
	}
	
}

/**
 * Eine div-Box erstellen
 *
 *
 */
function _set_interface() {
	var iframeID = 'iFrameIE6Fix';
    var container = '<iframe id="'+iframeID+'" frameborder="0"></iframe><div id="jquery-overlay"></div>' +
                    '<div id="jquery-lightbox">' +
                        '<div id="lightbox-container-data-box">' +
                            '<div id="lightbox-container-data">' +
                                '<div id="lightbox-details">' +
                                '</div>' +
                                '<div id="lightbox-secNav">' +
                                    '<a href="#" id="lightbox-secNav-btnClose">' +
                                        '<img src="/extension/wuv/design/wuv/images/lightbox/lightbox-btn-close.gif">' +
                                    '</a>' +
                                '</div>' +
                            '</div>' +
                        '</div>' +
                    '</div>';

    jQuery( 'body' ).append( container );
    jQuery( '#jquery-overlay' ).css(
    {
        backgroundColor:    '#fff',
        opacity:            0,
        width:              1680,
        height:             1050
    }).fadeIn();
    jQuery( '#jquery-lightbox' ).css(
    {
        width:              600,
        height:             500,
        top:                100,
        left:               100
    }).show();
    
    jQuery('#'+iframeID).css({
    	position:	 'absolute',
	    display: 		'block',
	    width:              600,
        height:             500,
        top:                100,
        left:               100,
        zIndex:			50
    }).show();
    

    
    // Schlie�en des overlays durch Klicken des X-Buttons
    jQuery( '#lightbox-secNav-btnClose' ).click( function() 
    {
        _finish();
        return false;
    });
}

function _set_interface_pw() {
	var iframeID = 'iFrameIE6Fix-pw';
    var container = '<iframe id="'+iframeID+'" frameborder="0"></iframe><div id="jquery-overlay-pw"></div>' +
                    '<div id="jquery-lightbox-pw">' +
                        '<div id="lightbox-container-data-box-pw">' +
                            '<div id="lightbox-container-data-pw">' +
                                '<div id="lightbox-details-pw">' +
                                '</div>' +
                                '<div id="lightbox-secNav-pw">' +
                                    '<a href="#" id="lightbox-secNav-btnClose-pw">' +
                                        '<img src="/extension/wuv/design/wuv/images/lightbox/lightbox-btn-close.gif">' +
                                    '</a>' +
                                '</div>' +
                            '</div>' +
                        '</div>' +
                    '</div>';

    jQuery( 'body' ).append( container );
    jQuery( '#jquery-overlay-pw' ).css(
    {
        backgroundColor:    '#fff',
        opacity:            0,
        width:              1680,
        height:             1050
    }).fadeIn();
    jQuery( '#jquery-lightbox-pw' ).css(
    {
        width:              600,
        height:             500,
        top:                100,
        left:               100
    }).show();
    
    jQuery('#'+iframeID).css({
    	position:	 'absolute',
	    display: 		'block',
	    width:              600,
        height:             500,
        top:                100,
        left:               100,
        zIndex:			50
    }).show();
    
    // Schlie�en des overlays durch Klicken des X-Buttons
    jQuery( '#lightbox-secNav-btnClose-pw' ).click( function() 
    {
        _finish_pw();
        return false;
    });
    
   
}

/**
 * Entfernen der Lightbox
 *
 */
function _finish()
{
	jQuery('#iFrameIE6Fix').remove();
    jQuery('#jquery-lightbox').remove();
    jQuery('#jquery-overlay').fadeOut( function() {
        jQuery('#jquery-overlay').remove();
    });
}

function _finish_pw()
{
	jQuery('#iFrameIE6Fix-pw').remove();
    jQuery('#jquery-lightbox-pw').remove();
    jQuery('#jquery-overlay-pw').fadeOut( function() {
        jQuery('#jquery-overlay-pw').remove();
    });
}

/**
/ THIRD FUNCTION
* getPageSize() by quirksmode.com
*
* @return Array Return an array with page width, height and window width, height
*/
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
/**
/ THIRD FUNCTION
* getPageScroll() by quirksmode.com
*
* @return Array Return an array with x,y page scroll values.
*/
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
