var $j = jQuery.noConflict();
function modul_box()
{
    var iSerial;
    var iDisplay;
    var iCount = 0;
    iSerial = $j.cookie('iphonecustomOrder');
    iDisplay = $j.cookie('iphonecustomDisplay');
    
    if (iSerial && iDisplay)
    {
        iSerial = iSerial.split(',');
        iDisplay = iDisplay.split(',');
        
        $j(iSerial).each(function(i)
        {
            $j('#leftcolumn').append($j("#" + this));
            $j("#" + this).find('.modul-box-content').css('display', iDisplay[iCount]);
            
            if(iDisplay[iCount] == 'none')
            {
               $j("#" + this).find(".modul-box-header").addClass('active');
            }
            
            iCount = iCount + 1;
        });
    }
    $j("#leftcolumn").sortable(
    {
        scroll : true, axis : "y", stop : orderChanged, scrollSpeed : 50, cursor : "NS-resize", handle : '.move', 
        containment : "parent", opacity : 0.7, forcePlaceholderSize: true, placeholder : "ui-selected"
    });
    $j('#leftcolumn .modul-box .expand').click(toggleDisplay)
}
function orderChanged(el, ui)
{
    updateCookie()
}
function updateCookie()
{
    var iSerial;
    iSerial = $j("#leftcolumn").sortable('toArray');
    $j.cookie('iphonecustomOrder', iSerial, {
        path : "/", expires : 365
    });
    var iDisplay;
    var strCookie = "";
    var iCount = 1;
    $j("#leftcolumn .modul-box").each(function (i)
    {
        if (iCount > 1) {
            strCookie += ",";
        }
        strCookie += $j(this).find('.modul-box-content').css('display');
        iCount++
    });
    $j.cookie('iphonecustomDisplay', strCookie, {
        path : "/", expires : 365
    })
}

function toggleDisplay()
{
	var headerEl = $j(this).parent();
    var el = $j('.modul-box-content', headerEl.parent()); // find();
    var alpha = el.css('opacity');
    var animateSetting = {height: 'toggle'};
	animateSetting.opacity = (el.css('display') == 'none') ? 1 : 0;
	headerEl.toggleClass('active');
	el.animate(animateSetting, 250, "easeOutQuad", updateCookie);
}

var news_timeout = null;
var current_item = 1;
var blnPlay = true;
var blnFirstPlay = true;
var blnJump = false;
var nextID = 0;
var blnTween = false;

function news_box(){
    $j('#boxcontent div').css({
        opacity : 0
    });
    
    $j('#photos .item').siblings(':first-child').css({left : '0px'});
    $j('#boxcontent div').siblings(':first-child').css({opacity : 1, top : 0});
    $j('#interface span').click(doJump);
    $j('#interface #play').click(togglePlay);
    $j('#interface #next').click(doNext);
    
    $j('#photos .item h2').each(function (){$j(this).transBGdraw({'z-index' : 1});});
    
    $j('#photos .item div').each(function (){
        if ($j(this).html() != "") {
            $j(this).css({opacity : 0});
        }
    });
    doFadeTitle()
}

function doJump(){
    nextID = parseInt($j(this).attr('id').replace('boxitem', ''));
    if (current_item != nextID && !blnTween){
        if (news_timeout) {
            clearTimeout(news_timeout);
        }
        if (current_item + 1 == nextID || (nextID == 1 && current_item == 5)) {
            doNext();
            return
        }
        var posToActivate = (nextID - current_item) + 1;
        if (posToActivate < 1) {
            posToActivate += 5;
        }
        if (!blnFirstPlay){
            $j('#boxcontent').append($j('#boxcontent div').siblings(':first-child'));
            $j('#boxcontent div').siblings(':last-child').css({opacity : 0, top : '192px'});

            $j('#photos').append($j('#photos .item').siblings(':first-child'));
            $j('#photos .item').siblings(':last-child').css({left : '272px', position : 'absolute'});
            $j('#photos .item').siblings(':last-child').children('div').css({opacity : 0});
        }
        $j('#boxcontent').append($j('#boxcontent div').siblings(':nth-child(1)').clone());
        $j('#boxcontent div').siblings(':last-child').css({opacity : 0, top : '192px'});
        
        $j('#photos').append($j('#photos .item').siblings(':first-child').clone());
        $j('#photos .item').siblings(':last-child').css({left : '272px', position : 'absolute'});
        $j('#photos .item').siblings(':last-child').children('div').css({opacity : 0});

        var i = 2;
        while (i < posToActivate){
            $j('#boxcontent').append($j('#boxcontent div').siblings(':nth-child(2)'));
            $j('#boxcontent div').siblings(':last-child').css({top : '192px'});
            $j('#photos').append($j('#photos .item').siblings(':nth-child(2)'));
            $j('#photos .item').siblings(':last-child').children('div').css({opacity : 0});
            i++
        }
        blnJump = true;
        
        $j('#boxitem' + current_item).removeClass('active');
        current_item = nextID;
        
        $j('#boxitem' + current_item).addClass('active');
        
        $j('#boxcontent div').siblings(':first-child').animate({opacity : 0},250, "easeOutQuad");
        $j('#boxcontent div').siblings(':nth-child(2)').css({top : 0});
        $j('#boxcontent div').siblings(':nth-child(2)').animate({opacity : 1}, 500, "easeOutQuad");
        
        $j('#photos .item').siblings(':first-child').children('div').animate({ opacity : 0},250, "easeOutQuad");
        $j('#photos .item').siblings(':first-child').next().animate({left : 0},500, "easeOutQuad", doResetJump);
        blnTween = true;
    }
}

function togglePlay(){
    if (blnPlay){
        $j('#interface #play').removeClass("pause");
        $j('#interface #play').addClass("play");
        if (news_timeout) {
            clearTimeout(news_timeout)
        }
    }else{
        $j('#interface #play').removeClass("play");
        $j('#interface #play').addClass("pause");
        doNext()
    }
    blnPlay = !blnPlay
}

function doFadeTitle(){
    if (!blnFirstPlay){
        $j('#boxcontent div').siblings(':first-child').css({opacity : 0, top : '192px'});
    }
    news_timeout = setTimeout(doSetNextTimeout, 500);
}

function doSetNextTimeout(){
    blnTween = false;
    if (blnFirstPlay){
        $j('#photos .item').siblings(':first-child').children('div').animate({opacity : 1},500, "easeOutQuad");
    }else{
        $j('#photos .item').siblings(':first-child').next().children('div').animate({opacity : 1},500, "easeOutQuad");
    }
    
    if (blnPlay) {
        news_timeout = setTimeout(doNext, 7000);
    }
}
function doResetJump(){
    $j('#boxcontent div').siblings(':first-child').remove();
    $j('#photos .item').siblings(':first-child').remove();
    blnFirstPlay = true;
    doFadeTitle();
}

function doNext(){
    if (!blnTween){
        if (news_timeout) {
            clearTimeout(news_timeout);
        }
        $j('#boxitem' + current_item).removeClass('active');

        current_item++;
        if (current_item == 6) {
            current_item = 1
        }

        $j('#boxitem' + current_item).addClass('active');
        if (!blnFirstPlay){
            $j('#boxcontent').append($j('#boxcontent div').siblings(':first-child'));
            $j('#boxcontent div').siblings(':last-child').css({top : '192px'});
        }
        
        $j('#boxcontent div').siblings(':first-child').animate({opacity: 0},250, "easeOutQuad");
        $j('#boxcontent div').siblings(':nth-child(2)').css({top: 0});
        $j('#boxcontent div').siblings(':nth-child(2)').animate({opacity: 1},500, "easeOutQuad");
        if (!blnFirstPlay) {
            $j('#photos').append($j('#photos .item').siblings(':first-child'));
        }
        $j('#photos .item').siblings(':first-child').children('div').animate({opacity: 0}, 250, "easeOutQuad");
        $j('#photos .item').siblings(':last-child').css({left: '272px', position: 'absolute'});
        $j('#photos .item').siblings(':last-child').children('div').css({opacity: 0});
        $j('#photos .item').siblings(':first-child').next().animate({left: 0}, 500, "easeOutQuad", doFadeTitle);
        blnFirstPlay = false;
        blnTween = true;
    }
}
function navmenu()
//Navigatiebalk, via http://users.tpg.com.au/j_birch/plugins/superfish/
{
    $j("ul.sf-menu").supersubs({
        minWidth : 12, maxWidth : 27, extraWidth : 1
    }).superfish({
        delay : 1000, speed : 'fast', dropShadows : false
    })
	$j("ul.sf-menu li a").hover(
	 function() {
	  $j(this).attr("title","");
	 })
}
function kiekeboe()
{
    $j('.kiekeboe').hide();
    $j('.kiekeboeToggle').click(function ()
    {
        $j('.kiekeboe').toggle('fast');
		return false;
    })
}
function lightbox()
{
    $j('a[rel*=lightbox]').lightBox();
}
function tableHide()
{
	$j('#table-specs').hide();
}
function initMenus() {
	//Accordion voor iPhone Tips (http://www.iphoneclub.nl/iphone-tips/), via http://www.i-marco.nl/weblog/jquery-accordion-menu-redux2/
    $j('ul.menu ul').hide();
    $j.each($j('ul.menu'),
    function() {
        var cookie = $j.cookie(this.id);
        if (cookie === null || String(cookie).length < 1) {
            $j('#' + this.id + '.expandfirst ul:first').show();
        }
        else {
            $j('#' + this.id + ' .' + cookie).next().show();
        }
    });
    $j('ul.menu li a').click(
    function() {
        var checkElement = $j(this).next();
        var parent = this.parentNode.parentNode.id;
        if ($j('#' + parent).hasClass('noaccordion')) {
            if ((String(parent).length > 0) && (String(this.className).length > 0)) {
                if ($j(this).next().is(':visible')) {
                    $j.cookie(parent, null);
                }
                else {
                    $j.cookie(parent, this.className);
                }
                $j(this).next().slideToggle('normal');
            }
        }
        if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            if ($j('#' + parent).hasClass('collapsible')) {
                $j('#' + parent + ' ul:visible').slideUp('normal');
            }
            return false;
        }
        if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            $j('#' + parent + ' ul:visible').slideUp('normal');
            if ((String(parent).length > 0) && (String(this.className).length > 0)) {
                $j.cookie(parent, this.className);
            }
            checkElement.slideDown('normal');
            return false;
        }
    }
    );
}

function tabMain() {  

	$j('#tabvanilla,#tabs-tagcat,#shoptabs,#postswidget').tabs({
		cookie: {path:"/", expires: 30}
		})
}

function tabPost() {
	
	$j('#posttabs,#posttabs-1,#posttabs-2,#posttabs-3,#posttabs-4,#posttabs-5').tabs({
		cookie: {name: 'post-tabs', expires: 30}
	})
}

function iPcAccordion()
{
	$j("#accordion").accordion({active:0},{header:'div>li'},{autoHeight:false},{navigation:true});
}
function toggleAuthor(divid) {
	if (document.getElementById(divid).style.display == 'none') {
        document.getElementById(divid).style.display = 'block';
    } else {
        document.getElementById(divid).style.display = 'none';
    }
}

function toolTip() {
$j("#sociable2 a[title]").qtip({
	style: { 
	   width: 100,
	   padding: 1,
	   background: '#302D26',
	   color: 'white',
	   textAlign: 'center',
	   border: {
	      width: 1,
	      radius: 8,
	      color: '#302D26'
		},
		tip: { // Now an object instead of a string
		   corner: 'bottomMiddle', // We declare our corner within the object using the corner sub-option
		   color: '#302D26',
		   size: {
		      x: 16, // Be careful that the x and y values refer to coordinates on screen, not height or width.
		      y : 8 // Depending on which corner your tooltip is at, x and y could mean either height or width!
			}
		}
	},
	position: {
		corner: {
		   target: 'topMiddle',
		   tooltip: 'bottomMiddle'
		},
		adjust: { 
			x: 0, 
			y: -15 
		}
	}
});
//$j("#sociable2 img[title]").attr("title","");
}

$j(document).ready(launch);
function launch()
{
	navmenu();
	tabMain();
	tabPost();
	modul_box();
	news_box();
	initMenus();
	lightbox();
	kiekeboe();
	iPcAccordion();
	toolTip();
	tableHide();
}