google.load("jquery", "1.4");

var sixxInitOnload = function() {
    sixx.TeasImg.Init();
    sixx.GalleryImg.Init();
    trakken_bind_links();
}
if (window.addEventListener) {
    // Standard, or IE with explicitly defined method
    window.addEventListener('load', sixxInitOnload, false);
}
else if (window.attachEvent) {
    // IE
    window.attachEvent('onload', sixxInitOnload);
}

if (typeof(sixx) == 'undefined') {
	var sixx = {
		TeasImg : function () {},
		GalleryImg : function () {}
	}
}

sixx.TeasImg = {
    _play : 1,
	_idle : 1,
	_teasArray : new Array(),
	
	Init : function () {
		
		if ($('.teaserImg').length > 1){
			$('.teaserImg').each(function(i1){
				var nav = '';
				$('.teaserImg').each(function(i2){
					if (i1 == i2) {
						nav += '<img src="/fileadmin/img/nav_teas_img_act.png" alt="" width="22" height="22" />';
					} else {
						nav += '<img onClick="sixx.TeasImg.To(\'' + $(this).attr('id') + '\');" src="/fileadmin/img/nav_teas_img_no.png" alt="" width="22" height="22" />';
					}
				});
				
				sixx.TeasImg._teasArray.push($(this).attr('id'));
				$(this).append('<div class="nav">' + nav + '</div>');
			});
			
			var timeout = window.setTimeout('sixx.TeasImg.Run(\'1\')', 2000);
		}
	},
	
	To : function (uid) {
		sixx.TeasImg._play = 0;
		sixx.TeasImg._to(uid);
	}, 
	
	Run : function (i) {
		//alert ('run: ' + i + ' id: ' + sixx.TeasImg._teasArray[i]);
		if (sixx.TeasImg._play) {
			if (sixx.TeasImg._teasArray[i]) {
				sixx.TeasImg._to(sixx.TeasImg._teasArray[i]);
			} else {
				i = 0;
				sixx.TeasImg._to(sixx.TeasImg._teasArray[i]);
			}
			i++;
			var timeout = window.setTimeout('sixx.TeasImg.Run(\'' + i + '\')', 5000);
		}
	},
	
	_to : function (uid) {
		if (sixx.TeasImg._idle) {
			sixx.TeasImg._idle = 0
			$('.teaserImg:visible').fadeOut('fast', function () {
				$('#' + uid).fadeIn('fast', function () {
					sixx.TeasImg._idle = 1;
				});
			});
		}
	}
}

sixx.GalleryImg = {
    _play : 1,
	_idle : 1,
	_galleryArray : new Array(),
	_galleryNavArray : new Array(),
	_galleryNrArray : new Array(),
	
	Init : function () {
		
		if ($('.gImg').length > 1){
			var count = 0;
			$('.gImg').each(function(i1){
				sixx.GalleryImg._galleryArray.push($(this).attr('id'));
				count++;
			});
			
			$('.gImg').each(function(i2){
				var thisIndex = jQuery.inArray($(this).attr('id'), sixx.GalleryImg._galleryArray);
				
				if (thisIndex == 0) {
					sixx.GalleryImg._galleryNavArray[$(this).attr('id')] = '<a href="javaScript:sixx.GalleryImg.To(\'' + sixx.GalleryImg._galleryArray[(count-1)] + '\');"><img src="/fileadmin/img/nav_gallery_back.png" alt="Back" width="34" height="35" /></a>&nbsp;<a href="javaScript:sixx.GalleryImg.To(\'' + sixx.GalleryImg._galleryArray[(thisIndex+1)] + '\');"><img src="/fileadmin/img/nav_gallery_next.png" alt="Next" width="34" height="35" /></a>';
					$('img',this).click(function () {
						sixx.GalleryImg.To(sixx.GalleryImg._galleryArray[(thisIndex+1)]);
					});
				} else if (thisIndex == (count-1)) {
					sixx.GalleryImg._galleryNavArray[$(this).attr('id')] = '<a href="javaScript:sixx.GalleryImg.To(\'' + sixx.GalleryImg._galleryArray[(thisIndex-1)] + '\');"><img src="/fileadmin/img/nav_gallery_back.png" alt="Back" width="34" height="35" /></a>&nbsp;<a href="javaScript:sixx.GalleryImg.To(\'' + sixx.GalleryImg._galleryArray[0] + '\');"><img src="/fileadmin/img/nav_gallery_next.png" alt="Next" width="34" height="35" /></a>';
					$('img',this).click(function () {
						sixx.GalleryImg.To(sixx.GalleryImg._galleryArray[0]);
					});
				} else {
					sixx.GalleryImg._galleryNavArray[$(this).attr('id')] = '<a href="javaScript:sixx.GalleryImg.To(\'' + sixx.GalleryImg._galleryArray[(thisIndex-1)] + '\');"><img src="/fileadmin/img/nav_gallery_back.png" alt="Back" width="34" height="35" /></a>&nbsp;<a href="javaScript:sixx.GalleryImg.To(\'' + sixx.GalleryImg._galleryArray[(thisIndex+1)] + '\');"><img src="/fileadmin/img/nav_gallery_next.png" alt="Next" width="34" height="35" /></a>';
					$('img',this).click(function () {
						sixx.GalleryImg.To(sixx.GalleryImg._galleryArray[(thisIndex+1)]);
					});
				}
				
				sixx.GalleryImg._galleryNrArray[$(this).attr('id')] = '<b>' + (thisIndex+1) + '</b> von ' + count;
			
			});
			
			$('#gNav').html(sixx.GalleryImg._galleryNavArray[sixx.GalleryImg._galleryArray[0]]);
			$('#gCount').html(sixx.GalleryImg._galleryNrArray[sixx.GalleryImg._galleryArray[0]]);
			
			//var timeout = window.setTimeout('sixx.GalleryImg.Run(\'1\')', 2000);
		}
	},
	
	To : function (uid) {
		sixx.GalleryImg._play = 0;
		sixx.GalleryImg._to(uid);
	}, 
	
	Run : function (i) {
		//alert ('run: ' + i + ' id: ' + sixx.GalleryImg._galleryArray[i]);
		if (sixx.GalleryImg._play) {
			if (sixx.GalleryImg._galleryArray[i]) {
				sixx.GalleryImg._to(sixx.GalleryImg._galleryArray[i]);
			} else {
				i = 0;
				sixx.GalleryImg._to(sixx.GalleryImg._galleryArray[i]);
			}
			i++;
			var timeout = window.setTimeout('sixx.GalleryImg.Run(\'' + i + '\')', 5000);
		}
	},
	
	_to : function (uid) {
		if (sixx.GalleryImg._idle) {
			sixx.GalleryImg._idle = 0
			$('.gImg:visible').fadeOut('fast', function () {
				$('#' + uid).fadeIn('fast', function () {
					sixx.GalleryImg._idle = 1;
				});
			});
			$('#gNav').html(sixx.GalleryImg._galleryNavArray[uid]);
			$('#gCount').html(sixx.GalleryImg._galleryNrArray[uid]);
		}
	}
}

function trakken_bind_links() {        
  
    var a_tags  = document.body.getElementsByTagName("a");        
  
    for (var i = 0; i < a_tags.length; i++) {        
  
        var href = a_tags[i].href;      
        var target = a_tags[i].target;      
  
        try {  
  
            if (href == "") { continue; }      
  
  
            if( href.match(/^mailto\:/i) ){      
                var track = '/email/' + href.substring(7);      
            }      
            else if( href.match(/\.(pdf|doc|zip|xls|exe)$/i) ){      
                var track = '/download/' + href.replace(/^http:\/\//i, '');      
            }      
            else if( href.match(/(adserv.quality-channel.de)/i) ){      
                continue;      
            }      
            else if( href.match('//') ){  
  
                var doc_hostname = document.location.hostname.split('.').slice(-2).join('.');  
                var link_hostname = href.split('//')[1].split('/')[0].split('.').slice(-2).join('.');  
  
                if( doc_hostname != link_hostname ){  
                    var track = '/outbound/' + href.replace(/^http:\/\//i, '');   
                }  
                else{ continue; }  
            }      
            else{ continue; }  
  
//            var on_click = '_gaq.push(["_trackPageview","' + track + '"]);window.setTimeout(function(){window.location="' + href + '";},200);return false;';    
              var on_click = '_gaq.push(["_trackPageview","' + track + '"]);window.setTimeout(function(){window.open("'+  href +'","'+ target +'");},200);return false;';    

  
            if(a_tags[i].onclick != null && a_tags[i].onclick != undefined){      
                
                on_click    = a_tags[i].onclick.toString().split('{',2)[1].split(/\}$/i)[0]     + on_click;       
            }      
  
           a_tags[i].onclick = new Function(on_click);      
           //a_tags[i].onclick = new Function("alert('" + on_click + "')");      
        }      
        catch(e){}      
    }      
}


