// die Base URL ausgeben
function getBaseUrl(){
	tmpLocation = document.location.host;
	
	if(document.location.protocol == 'https:'){
		tmpLocation = 'https://' + tmpLocation;
	}else{
		tmpLocation = 'http://' + tmpLocation;	
	}
	
	tmpLocation = tmpLocation + '/';	
	return tmpLocation;
}

// rechte ueberpruefen
function magazinCheckRights(articleUid, magazinType, articleLink){
	// url auslesen
	var path = $(location).attr('href');
	
	// wenn es ein fragezeichen hat, alles nach dem fragezeichen loeschen inkl. fragezeichen
	if(path.indexOf('?') != -1){
		path = path.substr(0, path.indexOf('?'));
	}
	
	path = path + '?';
	
	path += "tx_hbchmagazin_magazin[controller]=Magazin&";
	path += "tx_hbchmagazin_magazin[action]=index&";
	path += "type=8134";
	
	jQuery.ajax({
		type: 'POST',
		async: true,
		url: path,
		data: {
			'ajaxAction' : 'checkRights',
			'articleUid' : articleUid,
			'magazinType': magazinType,
			'articleLink': articleLink
		},
		success: function(data){
			result = jQuery.parseJSON(data);

			// berechtigung vorhanden
			if(result['rights'] == '1'){
				// weiterleitung auf detailseite
				baseUrl = getBaseUrl();
				document.location.href = baseUrl + result['link'];
				return false;
			}else{
				// modal code erzeugen
				modalCode = '<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" id="checkRightModal' + result['magazin'] + '"><div class="modal-dialog modal-sm"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title" id="myModalLabel">' + result['errorTitle'] + '</h4></div><div class="modal-body">' + result['error'] + '</div></div></div></div>';
				
				// modal einfuegen, falls noch nicht vorhanden
				if($('#checkRightModal' + result['magazin']).length <= 0){
					$('body').append(modalCode);
				}
				
				// fehlermeldung ausgeben
				$('#checkRightModal' + result['magazin']).modal();
				return false;
			}
		},
        error: function(error) {
            console.log(error);
        }
	});	
}

//sthi 20150320
//geklicktes ePaper anzeigen
function showSelectedEpaper(ePaperFolder){
    //aktuellen Frame auslesen und aufteilen
    actFrameLink = $('.current-epaper #currentEpaperFrame').attr('src');
    actFrameLink = actFrameLink.split("/"); 
    
    //Ordner wo das ePaper abgelegt ist neu setzten
    actFrameLink[5] = ePaperFolder;
    
    //array neu zusammensetzten
    newActFrameLink = actFrameLink.join('/'); 
    
    //zum Seitenanfang scrollen
    $("html, body").animate({ scrollTop: 0 }, "slow");
    
    //ausgewählte ePaper ausgabe anzeigen
    $('.current-epaper #currentEpaperFrame').attr('src', newActFrameLink);
}

//sthi 20150320
//Überprüfen ob eingeloggt oder nicht
function checkLogin(linkPath){
    // url auslesen
    var path = $(location).attr('href');

    // wenn es ein fragezeichen hat, alles nach dem fragezeichen loeschen inkl. fragezeichen
    if(path.indexOf('?') != -1){
            path = path.substr(0, path.indexOf('?'));
    }
    
    //MagazinType mitschicken
    magazinType = linkPath;
    magazinType = magazinType.split('/');
    
    //sthi für hbch.cyberfactory.ch ist es magazinType[0] für die Live Seite ist es magazinType[1]
    magazinType = magazinType[1];
    
    if(magazinType == 'first'){
        magazinType = 'first';
    }else{
        magazinType = 'whb';
    }
    
    path = path + '?';
    path += "type=8599";

    jQuery.ajax({
        type: 'POST',
        async: true,
        url: path,
        data: {
                'ajaxAction' : 'checkLogin',
                'redirectUrl' : linkPath,
                'magazinType' : magazinType
        },
        success: function(data){
                result = jQuery.parseJSON(data);
                
                // berechtigung vorhanden
                if(result['rights'] == '1'){
                        // weiterleitung auf detailseite
                        baseUrl = getBaseUrl();
                        document.location.href = baseUrl + linkPath;
                        return false;
                }else{
                        // modal code erzeugen
                        modalCode = '<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" id="checkLoginModalEpaper"><div class="modal-dialog modal-sm"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title" id="myModalLabel">' + result['errorTitle'] + '</h4></div><div class="modal-body">' + result['error'] + '</div></div></div></div>';

                        // modal einfuegen, falls noch nicht vorhanden
                        if($('#checkRightModal' + result['magazin']).length <= 0){
                                $('body').append(modalCode);
                        }

                        // fehlermeldung ausgeben
                        $('#checkLoginModalEpaper').modal();
                        return false;
                }
                
        },
        error: function(error) {
            console.log(error);
        }
    });
}
      
//sthi: 20150527 Funktion aufrufen wenn Seite geladen, oder wie im ajaxTabs.js sobald Ajax Content geladen ist
jQuery(document).ready(function(){
    magazinAjaxCheckRights();
});

//sthi: 20150527 Hier aus Dokument Ready eine Funktion machen, damit sie beim Ajax load geladen werden kann
function magazinAjaxCheckRights(){
    
	// rechtevergleich bei magazin artikel
	$(".checkMagazinRight").click(function() {
		articleUid  = $(this).attr('data-article-uid');
		magazinType = $(this).attr('data-magazin-type');
		articleLink = $(this).attr('href');
		
		// rechte pruefen
		magazinCheckRights(articleUid, magazinType, articleLink);
		return false;
	});
        
        //sthi: 20150320
        //per Ajax das Aktuellste ePaper darstellen
        $(".currentEpaper").click(function() {
            //Verzeichniss auslesen, wo das ePaper abgespeichert ist
            ePaperFolder = $(this).attr('id');
            
            //geklicktest ePaper anzeigen
            showSelectedEpaper(ePaperFolder);
            
            return false;
        });
        
        //sthi: 20150320
        //wenn man auf ePaper geklickt hat soll paywall kommen
        $('header.header nav.mainNav li').click(function(){
            //Nav Content auslesen
            navContent = $(this).children().html();
            linkPath = $(this).children().attr('href');
            var index = $(this).index();
            //wenn auf ePaper geklickt dan Paywall
            if(index == 1){
                checkLogin(linkPath);
                
                return false;
            }
        });
};