/*!
* Some JavaScript items explicitely developed for www.colorneg.de and www.colorneg.com
* Copyright (c) 2007-2011 Dipl.-Medieninformatiker(FH) Christoph Oldendorf
* Not licensed for redistribution. All rights reserved.
*/

var isIE6 = (function(){
var div = document.createElement('div');
div.innerHTML = '<!--[if IE 6]><i></i><![endif]-->';
return (div.getElementsByTagName('i').length === 1);
}());

var autoId=0;
var flashReplaceBrowser=!((flashVersion>=10)&&((BrowserDetect.browser=="Explorer"&&BrowserDetect.version<=6.0)||(BrowserDetect.browser=="Firefox"&&BrowserDetect.version>=3.5)||(BrowserDetect.browser=="Safari"&&BrowserDetect.version>2.0)||(BrowserDetect.OS=="iPhone/iPod")));
var peacockBrowser=!flashReplaceBrowser&&!isIE6;

function externalLinks() {
$('a').each(function(index) {
if ($(this).get(0).href && ($(this).get(0).rel == "external"||$(this).get(0).rel == "external nofollow"||$(this).get(0).rel == "nofollow external"))
{
$(this).get(0).target = "_blank";
}
});
};

var no = new Object();
function initImage(id,count){
no[id]=''+count+'_';
};

var domain = 'http://' + window.location.hostname + '/';
var fancyProps = {centerOnScroll:true,opacity:true,hideOnContentClick:true,overlayColor:'#000000',overlayOpacity:0.7,showCloseButton:false,'onComplete': function() {$("#fancybox-outer").bind("contextmenu", function () {$.fancybox.close();return false;})}};
var otherWidth= {};
var changedTo=false;

function change(id,path,bildname) {
changedTo=arguments;
var font=(($('.scroller #change'+id.substring(6)).length>0)?10:12);
var dims=pageData[path+no[id]+bildname].split(",");
if (isIOS||isWebOS)
{
var imgWidth=''+dims[0]+'px';
var imgHeight=''+dims[1]+'px';
otherWidth[id]=new Array(''+(614-dims[0])+'px',''+(614-dims[0]-190)+'px');
}
else
{
var imgWidth=''+(dims[0]/font)+'em';
var imgHeight=''+(dims[1]/font)+'em';
otherWidth[id]=new Array(''+((614-dims[0])/12)+'em',''+((614-dims[0]+190)/12)+'em');
}
$('#'+id).css({'width' : imgWidth,'height' : imgHeight});
$('#dummy'+id).css({'width' : imgWidth,'height' : imgHeight});
var prepHTML= '';
prepHTML+= '<span id="flash'+id.substring(6)+'">';
prepHTML+= '<img src="'+domain+path+no[id]+bildname+'" alt="" class="'+(doFlash?'flash ':'')+'fit" />';
prepHTML+= '</span>';
if (($('#'+id+'_first').length>0)&&($('#'+id+'_first').html()==pageData[id+'_initial']))
$('#'+id+'_first').html(pageData[id+'_followed']);
if (isFixedPosSupported&&($('.scroller #change'+id.substring(6)).length>0)){
redefineClasses(id,"#flash"+id.substring(6));
redrawScroller(id);
}
$('#'+id).html(prepHTML);
if (doFlash) 
flashReplacement(path+no[id]+bildname,"flash"+id.substring(6));
newsZapper();
};

function fakeClick()
{
$('#'+arguments[0]).trigger('click');
$('.swf').css({visibility:'hidden'});
};

function flashReplacement(imgURI, htmlContainer)
{
var dims=pageData[imgURI].split(",");
var doReplace = (flashReplaceBrowser&&$('#'+htmlContainer).children('img').hasClass('flash'));
var flashvars = {url:imgURI};
var params = {
quality: "high",
wmode: "normal",
allowscriptaccess: "always",
bgcolor: "#f8fbfe"
};
var attributes = {id:htmlContainer};
// has to be written this way for IE6 because of the "class" property name
attributes['class'] = 'swf';
var lgPath='';
if (dims[2]=='true')
{
var tmp=imgURI.split("/");
for (var i=0;i<tmp.length-1;i++)
lgPath+=tmp[i]+'/';
lgPath+='large/'+tmp[tmp.length-1];
}
autoId++;
var fancyPropsLocal=fancyProps;
if ((!doReplace)&&(lgPath!='')&&(!isIE6))
{
$('img','#'+htmlContainer).wrap('<a id="autoId'+autoId+'" href="javascript://" />');
fancyPropsLocal.href=lgPath;
fancyPropsLocal.transitionIn='elastic';
fancyPropsLocal.transitionOut='elastic';
fancyPropsLocal.easingIn='easeOutBack';
fancyPropsLocal.easingOut='easeInBack';
$("#autoId"+autoId).fancybox(fancyPropsLocal);
}
else if (doReplace)
{
if ((lgPath!='')&&(!isIE6))
{
$('body').append('<a class="hidden" id="autoId'+autoId+'" href="javascript://" />');
fancyPropsLocal.href=lgPath;
flashvars.lnk="autoId"+autoId;
$.get(domain+'css/img_dim.php?path='+lgPath+'&font=1', function(data) {
var dimsLG=data.split(',');
fancyPropsLocal.width=dimsLG[0];
fancyPropsLocal.height=dimsLG[1];
fancyPropsLocal.type='swf';
fancyPropsLocal.href="flash/img.swf";
fancyPropsLocal.swf={
'flashvars':'url='+lgPath,
'wmode': 'normal'
};
fancyPropsLocal.onClosed = function() {
$('.swf').css({visibility:'visible'});
};
$("#autoId"+autoId).fancybox(fancyPropsLocal);
});
}
swfobject.embedSWF("flash/img.swf", htmlContainer, "100%", "100%", "10", false, flashvars, params, attributes);
}
};

function changeStatic(id,path,bildname) {
var domain = 'http://' + window.location.hostname + '/';
$('#'+id).get(0).src = domain+path+bildname;
$('#'+id).get(0).src = domain+path+bildname;
if ($('#'+id+'_first').length>0) {
if ($('#'+id+'_first').html()==$('#'+id+'_initial').get(0).value)
$('#'+id+'_first').html($('#'+id+'_followed').get(0).value);
}
};
function loadURL(URL) {
location.href=URL;
};
function loadExtURL(URL) {
try{
pageTracker._trackPageview("/external/"+URL.substring(7));
}
catch(e) {}
var newWindow = window.open(URL, 'external');
};
function preloadImages(urls,i,no,dir){
var domain = 'http://' + window.location.hostname + '/';
if (i < urls.length) {
var temp = $('<img src="'+domain+dir+no+"_"+urls[i]+'" />');
i++;
temp.onLoad=preloadImages(urls,i,no,dir);
$('#preloading').append(temp);
}
};
function preload(array,no,dir){
preloadImages(array,0,no,dir);
};
function refresh(){
document.forms['myform'].submit();
};
function trackPage(url){
try{
pageTracker._anonymizeIp();
pageTracker._trackPageview(url);
} 
catch(e) {}
};
function visible(id){
$('#'+id).css({'visibility' : 'visible'});
};
function ctxNote()
{
$('#context').html('<h4 class="ctx">A note on the context of this document </h4><p class="ctx">This is the international version of an article published on the German www.ColorNeg.de web site. Some links might take you to pages that are in German. Menu entries you do understand will usually take you to other English pages though. You\'re quite welcome to look around. You can always use your browser\'s back button should you go astray and you can look at this <a class="next_page" href="sitemap.html">site map</a> to see what texts are available in English. Finally you may want to go to the international <a href="http://www.colorneg.com" rel="external" class="nextPage">ColorNeg web site</a>.</p>');
};

var isWideImage={};
// three functions to be used in imgScroller() and in change()
function setMargin(changeId)
{
if (isWideImage[changeId])
{
$('#'+changeId).css({ 'border-left' : '1.2em solid #F8FBFE',
'border-top' : '0.4em solid #F8FBFE',
'border-bottom' : '0.2em solid #F8FBFE',
'border-right' : '1.2em solid #F8FBFE',
'padding' : 0});
$('#dummy'+changeId).css({'padding-bottom' : 0, 'padding-left' : $('body').hasClass('wide')?'2.4em':'1.2em'});
}
var txtSize =$('body').css('font-size');
txtSize=txtSize.substring(0, txtSize.length - 2);
if (isWideImage[changeId])
var left = $('body').hasClass('wide')?22:0;
else
var left = $('#'+changeId).parent().width()-$('#'+changeId).width();
if ($('body').hasClass('wide')) 
{
left-=10;
if (isIE6) 
$('#'+changeId).css({'margin-right' : '1em'});
}
else if (isIE6) 
$('#'+changeId).css({'margin-right' : ''});
if (arguments.length>1&&arguments[1])
{
left+=$('#'+changeId).parent().offset().left-$(document).scrollLeft();
//parseInt($(...).css('property').replace(/[^-\d\.]/g, '')
if (isIE6) 
$('#'+changeId).addClass('absolute');
else
$('#'+changeId).addClass('fixed');
left=''+left+'px';
$('#'+changeId).css({'margin-left' : left});
}
else
{
if (isIE6) 
{
$('#'+changeId).removeClass('absolute');
$('#'+changeId).css({'margin-left' : ''});
}
else
{
$('#'+changeId).removeClass('fixed');
left=''+left+'px';
$('#'+changeId).css({'margin-left' : left});
}
}
};

function redefineClasses(changeId,objId)
{
if (!isWideImage[changeId])
{
if ((objId.substring(1,6)=='flash')&&($(objId).length==0))
objId='#noflash'+objId.substring(6);
$('#'+changeId).parent().children(".scroller p, .wrapper").each(function(index) 
{
if ($(this).offset().top<=$(objId).offset().top+$(objId).height())
{ 
$(this).addClass("other_"+changeId);
if (otherWidth[changeId])
{
if ($('body').hasClass('wide'))
$(this).css({'width' : otherWidth[changeId][1]});
else
$(this).css({'width' : otherWidth[changeId][0]});
}
}
});
if ('#'+changeId==objId) 
setMargin(changeId);
$('#'+changeId).parent().children(".scroller p, .wrapper").each(function(index) {
if ($(this).offset().top>$(objId).offset().top+$(objId).height())
{
$(this).removeClass("other_"+changeId);
if (otherWidth[changeId])
$(this).css({'width' : ''});
$(this).addClass("after");
}
});
}
};
function redrawScroller(changeId)
{
var y = $(this).scrollTop();
var end = $('#'+changeId).parent().height()+$('#'+changeId).parent().offset().top-24;
var h = $('#'+changeId).height();
var top = $('#'+changeId).parent().children(".scroller p, .scroller .wrapper").first().offset().top;
// if we have scrolled past our image
if ((y >= top)&&(y+h<end))
{
setMargin(changeId,true);
if (!isWideImage[changeId]) $('#'+changeId).css({'padding-top' : ''});
}
else 
{
setMargin(changeId);
if (!isWideImage[changeId])
{
if (y < top)
$('#'+changeId).css({'padding-top' : ''});
else
$('#'+changeId).css({'padding-top' : $('#'+changeId).parent().height()-h-18});
}
}
redefineClasses(changeId,"#flash"+changeId.substring(6));
$('#fancybox-overlay').css({height:$('body').height()+10,overflow:'hidden'});
};
function imgScroller(changeId)
{
isWideImage[changeId]=$('#'+changeId).hasClass('b');
if (isIE6)
{
$('#'+changeId).parent().css({'position' : 'static'});
$('#dummy'+changeId).addClass("hide");
}
if ((isFixedPosSupported)&&(!(isIE6&&isWideImage[changeId])))
{
if ($('#'+changeId).parent().hasClass('scroller'))
{
$('#'+changeId).parent().children('p').addClass("other_"+changeId);
$('#'+changeId).parent().children(".scroller h1,.scroller h2,.scroller ul").each(function(index) {
$(this).wrap('<div class="wrapper" style="font-size:1.2em;"/>').wrap('<div style="font-size:0.83333em;"/>');
});
if (!isIE6) 
$('#'+changeId).addClass("absolute");
redefineClasses(changeId,'#'+changeId);
function redrawOnScroll (event) {
redrawScroller(changeId);
};
if (!isIE6) 
$(window).scroll($.throttle( 125, redrawOnScroll));
else
document.getElementById('dok').attachEvent("onscroll", $.throttle( 125, redrawOnScroll));
function resizeWindow(event) {
supportWideScreens();
redefineClasses(changeId,'#'+changeId);
var newWindowWidth = $(window).width();
if ($.browser.webkit) $('body').addClass('safariFix');
redrawScroller(changeId);
if ($.browser.webkit) $('body').removeClass('safariFix');
if (changedTo)
{
change(changedTo[0],changedTo[1],changedTo[2]);
}
};
$(window).bind("resize", resizeWindow);
redrawScroller(changeId);
}
}
else
{
$('#'+changeId).unwrap().wrap('<p />');
}
};
/*
function doSIFR()
{
var apex = {src: 'flash/apex.swf',ratios: [7, 1.47, 10, 1.43, 15, 1.36, 16, 1.34, 23, 1.32, 27, 1.31, 28, 1.3, 31, 1.29, 34, 1.3, 39, 1.28, 40, 1.29, 52, 1.28, 55, 1.27, 56, 1.28, 84, 1.27, 85, 1.26, 88, 1.27, 91, 1.26, 92, 1.27, 95, 1.26, 96, 1.27, 1.26]};
$('#content h1').each(function(index) {
if (!$(this).hasClass('categorize'))
$(this).addClass('nc');
});
var rgbString = $('#content h1,#content h2').first().css('color');
if (rgbString.substring(0,3)=='rgb')
{
var parts = rgbString.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
delete (parts[0]);
for (var i = 1; i <= 3; ++i) 
{
parts[i] = parseInt(parts[i]).toString(16);
if (parts[i].length == 1) parts[i] = '0' + parts[i];
}
var hexString = '#'+parts.join('');
}
else
var hexString = rgbString;
//sIFR.debug.ratios({ src: '/flash/apex.swf', selector: 'h1' });
sIFR.useStyleCheck = true;
// wmode: ($.browser.msie)?'window':'transparent' | background-color: #F8FBFE;
var hexString = "#262D34";
sIFR.replace(apex, {selector: '#content h1.nc, #content h2,#content h3,#content h4', wmode:'transparent', css: ['.sIFR-root { color:'+hexString+'; kerning:true; letter-spacing:0.5;}', 'a { color: #4E84AE; }', 'a:link { color: #4E84AE; }', 'a:hover { color: #4E84AE; }']});
sIFR.activate(apex);
};
*/
function supportWideScreens()
{
if(($(document).width()>1150)&&($('.scroller').length>0)&&(typeof(keepNarrow)=='undefined'))
$('body').addClass('wide');
else if($(document).width()>1150)
$('body').addClass('wideNotUsed');
else
{
$('body').removeClass('wideNotUsed');
$('body').removeClass('wide');
}
if (!flashReplaceBrowser&&!isIE6)
$("#pc4").css({width:''+(($(window).width()-$('.inhalt').outerWidth()-$('.inhalt').offset().left-2)/10)+'em',height:'71.3em',position:'absolute',overflow:'hidden',top:$("#pc1").offset().top,left:$(".inhalt").offset().left+$(".inhalt").outerWidth()});
};

function removeEmptyNodes(selector)
{
$(selector).each(function() {
if ($(this).html().replace(/\s|&nbsp;/g, '').length == 0)
$(this).remove();
});
};

function assignIndents()
{
var brLooksLike=$('<p><br /></p>').first().html();
$('#content p:not(.noIndents), #content .scroller p:not(.noIndents)').each(function(){ 
$('br',this).each(function(){ 
if ($(this).next().next('p').hasClass('chg'))
$(this).next().next().after('<br/>');
});
});
$('#content p:not(.noIndents), #content .scroller p:not(.noIndents)').each(function(){ 
var html = $(this).html();
var htmlArray = html.split(brLooksLike);
if (htmlArray.length>1)
{
var new_html = '<p class="br'+($(this).hasClass('indent')?' indent':'')+'">'+htmlArray[0]+"</p>"; 
for(var i = 1; i < htmlArray.length; i++)
new_html += '<p class="'+(((i<htmlArray.length-1)||($(this).hasClass('br')))?'br ':'')+'indent">'+htmlArray[i]+"</p>";
$(this).replaceWith(new_html);
}
});
$('#content p .chg, #content .scroller p .chg').each(function(){
$(this).parent().removeClass('br');
$(this).parent().removeClass('indent');
});
removeEmptyNodes('#content p, #content .scroller p');
$('#content .indent, #content .scroller .indent').each(function(){ 
if ($(this).prev().children().first().next().hasClass('chg'))
$(this).prev().addClass('br');
});
};

function newsZapper(){
var cutBelow=$('#content').offset().top+$('#content').height();
$('.oldnews').each(function(index) {
$(this).removeClass('hide');
var currentBottom=$(this).offset().top+$(this).height();
if (currentBottom>cutBelow)
$(this).addClass('hide');
});
}

var isFixedPosSupported=false;
var isIOS=false;
var isWebOS=false;
$(document).ready(function() {
//detect font smoothing
//break out of any iframe this page is put into
if (top.location!= self.location)
top.location = self.location.href;
$('body').append('<div id="preloading" class="hide" />');
var deviceAgent = navigator.userAgent.toLowerCase();
isIOS = deviceAgent.match(/(iphone|ipod|ipad)/);
isWebOS = deviceAgent.match(/(webos|hpwos)/);
if (isWebOS) flashReplaceBrowser=false;
if (isIOS)
$('.fit').removeClass('fit');
$('textarea').css({resize: 'none'});
isFixedPosSupported=fixedPosSupported();
if (isFixedPosSupported)
{
assignIndents();
$('body').addClass('scrollable');
supportWideScreens();
if ($('.scroller').length==0)
$(window).bind("resize", supportWideScreens);
}
if (isIE6)
{
$('.box_rechts').each(function(index) {
$(this).wrap('<div style="float:right" />').wrap('<div style="clear:both" />');
});
}
if (flashReplaceBrowser)
{
if ($.cookie('flashWorks')!='true')
{
flashReplaceBrowser=false;
$('body').append('<div id="temp"><div id="putInitialSWF" /></div>'); 
var flashvars = {};
var params = {allowscriptaccess: "always",
allowFullScreen: "true",
bgcolor: "FFFFFF"
};
var attributes = {id:"initialFlash"};
swfobject.embedSWF("flash/doesFlashWork.swf", "putInitialSWF", "1", "1", "8.0", "", flashvars, params, attributes);
}
else
doFlash();
}
//doFlash() is also used for fancybox initialization
else
doFlash(); 
doOnLoad();
newsZapper();
/*if ((isFixedPosSupported)&&(!flashReplaceBrowser))
doSIFR();
*/
// WebFonts must initially be used for the font smoothing detection to work
$('html').addClass('fontSmoothing-true');
});

var gotoOpacity=1;
var interval=0;
var timeDelay=50;
function opChange(){
clearInterval(interval);
timeDelay=1500;
$("#pc4").fadeTo(1000,gotoOpacity,doneFading);
};
function doneFading(){
if (gotoOpacity<1) timeDelay=50;
};

// based upon: http://www.useragentman.com/blog/2009/11/29/how-to-detect-font-smoothing-using-javascript/
function detectFontSmoothing() 
{
// IE has screen.fontSmoothingEnabled - sweet! 
if (typeof(screen.fontSmoothingEnabled) != "undefined") 
return screen.fontSmoothingEnabled; 
else 
{
try {
// THE INITIAL TEST ON WHETHER ANY FONT SMOOTHING IS ON
// Create a 20x20 Canvas block.
var canvasNode = document.createElement("canvas");
canvasNode.width = "20";
canvasNode.height = "20";
// We must put this node into the body, otherwise 
// Safari Windows does not report correctly.
canvasNode.style.display = "block";
$("body").append(canvasNode);
var ctx = canvasNode.getContext("2d");
// draw a black letter "O", 20px 'Frutiger Serif LT W01'.
ctx.textBaseline = "top";
ctx.font = "20px Arial";
ctx.fillStyle = "black";
ctx.strokeStyle = "black";
ctx.fillText("O", 0, 0);
// start at (1,1) and search the canvas from left to right,
// top to bottom to see if we can find a non-black pixel. If
// so we return true.
var count = 0;
for (var j = 1; j <= 20; j++) 
{
for (var i = 1; i <= 20; i++) 
{
var imageData = ctx.getImageData(i, j, 1, 1).data;
var alpha = imageData[3];
if (alpha != 255 && alpha != 0)
count++;
}
}
// If any trasparent areas were found some kind of font smoothing must be on
// THE SECOND TEST - FINDING OUT IF THE UGLY FONT SMOOTHING IN FIREFOX IS USED
if (count>0)
{
count=0;
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvasNode.width, canvasNode.height);
ctx.font = "10px 'Frutiger Serif LT W01'";
ctx.fillText("O", 0, 0);
for (var j = 1; j <= 20; j++) 
{
for (var i = 1; i <= 20; i++) 
{
var imageData = ctx.getImageData(i, j, 1, 1).data;
var alpha = imageData[3];
if (alpha == 255)
count++;
}
}
$("canvas").remove();
$(".fontPreload").remove();
if (count>3) return true;
}
// didn't find any non-black pixels - return false.
return false;
}
catch (ex) {
// Something went wrong (for example, Opera cannot use the
// canvas fillText() method. Return null (unknown).
return null;
}
}
};

$(window).load(function() {
doOnAllLoaded();
if (peacockBrowser)
{
$("#pc1").css('background-image','url(grafik/pfau/tl.jpg)');
$("#pc1").css('background-position','bottom left');
$("#pc1").css('background-repeat','no-repeat');
$("#pc1 .box_rechts_unten").css('background','none');
$("#pc2").css('background-image','url(grafik/pfau/ml.jpg)');
$("#pc2").css('background-position','bottom left');
$("#pc2").css('background-repeat','no-repeat');
$("#pc2 .box_rechts_unten").css('background','none');
$("#pc3").css('background-image','url(grafik/pfau/bl.jpg)');
$("#pc3").css('background-position','bottom left');
$("#pc3").css('background-repeat','no-repeat');
$("#pc3 .box_rechts_unten").css('background','none'); 
$("body").append('<div id="pc4"></div>');
$("#pc4").css({width:''+(($(document).width()-$('.inhalt').outerWidth()-$('.inhalt').offset().left-2)/10)+'em',height:'71.3em',position:'absolute',overflow:'hidden',top:$("#pc1").offset().top,left:$(".inhalt").offset().left+$(".inhalt").outerWidth()+($('body').hasClass('wide')?1:0)});
$("#pc4").css('background-image','url(grafik/pfau/rf.jpg)');
$("#pc4").css('background-repeat','no-repeat');
$("#pc4").fadeTo(0,0.08);
$("#pc2,#pc3").hover(function () { 
clearInterval(interval);
interval = setInterval ( "opChange()", timeDelay );
gotoOpacity=1;
},function(){
clearInterval(interval);
interval = setInterval ( "opChange()", timeDelay );
gotoOpacity=0.08;
});
}
var test=detectFontSmoothing();
if (!test)
$('html').removeClass('fontSmoothing-true');
});

function flashIsWorking()
{
flashReplaceBrowser=true;
doFlash();
$('#temp').remove();
$.cookie('flashWorks','true');
};
