/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
// jQuery.cookie = function(name, value, options) {
//     if (typeof value != 'undefined') { // name and value given, set cookie
//         options = options || {};
//         if (value === null) {
//             value = '';
//             options.expires = -1;
//         }
//         var expires = '';
//         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
//             var date;
//             if (typeof options.expires == 'number') {
//                 date = new Date();
//                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
//             } else {
//                 date = options.expires;
//             }
//             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
//         }
//         // CAUTION: Needed to parenthesize options.path and options.domain
//         // in the following expressions, otherwise they evaluate to undefined
//         // in the packed version for some reason...
//         var path = options.path ? '; path=' + (options.path) : '';
//         var domain = options.domain ? '; domain=' + (options.domain) : '';
//         var secure = options.secure ? '; secure' : '';
//         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
//     } else { // only name given, get cookie
//         var cookieValue = null;
//         if (document.cookie && document.cookie != '') {
//             var cookies = document.cookie.split(';');
//             for (var i = 0; i < cookies.length; i++) {
//                 var cookie = jQuery.trim(cookies[i]);
//                 // Does this cookie string begin with the name we want?
//                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
//                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
//                     break;
//                 }
//             }
//         }
//         return cookieValue;
//     }
// };






/*
  Molton House
  by plan5net@gmail.com, 
  2009 plan5.net
*/

$(document).ready(function(){
  
// SoundCloud Player
  
  // if( ! $.cookie('mhdjmix')){
  //   var mhplayer = window.open('http://moltonhouse.com/player.html', 'mhplayer', 'width=400,height=100,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
  //   $.cookie('mhdjmix', '1');
  // }
  
// INIT
// datepicker
	$('#startDate').datepicker({ dateFormat: 'dd/mm/yy' });

// position wrapper on window resize
	$(window).bind('resize', function() {
		wrapPos();
	});	

// initial position
	wrapPos();
  	

// PRESS	
// initial Press page vars
	var currentArticle = 0;
	var articles = $('#articlesList li').size()-1;
	var page = 1;
	var perPage = 15;
	$('#articlesList li:eq(0)').show();
	$('#articlesListText li:gt('+(perPage-1)+')').hide();
	if(articles>=14){
		$('#nav1').show();
	}

// Press page arrows
	$('#button_left, #button_right').bind('click', function(){
    if($(this).attr('id')=='button_left'){
      currentArticle=currentArticle==0?articles:currentArticle-1;
    }else{
      currentArticle=currentArticle==articles?0:currentArticle+1;
    }
    $('#articlesList li').hide();
    $('#articlesList li:eq('+currentArticle+')').show();
	});
	
// Press list
	$('#articlesListText a').bind('click', function(){
		return false;
	});
	$('#articlesListText a').hover(function(){
		currentArticle = parseFloat($(this).attr('rel'));
	    $('#articlesList li').hide();
	    $('#articlesList li:eq('+currentArticle+')').show();		
	}, function(){
		
	});
	
// Press navigation
	$('#nav1, #nav2').bind('click', function(){
		var x = Math.ceil($('#articlesListText li').length/perPage);
		if(x>1){
			// page num
			if(this.id=='nav1'){
				page++;
			}else{
				page--;
			}
// show/hide navigation	
			if(x==page){
				$('#nav1').hide();
			}else{
				$('#nav1').show();
			}
			if(page!=1){
				$('#nav2').show();
			}else{
				$('#nav2').hide();
			}
// hide actual articles
			$('#articlesListText li').show();
			$('#articlesListText li:gt('+(perPage*page-1)+')').hide();
			$('#articlesListText li:lt('+(perPage*page-perPage)+')').hide();
		}
	});



// GALLERY
// gallery image hover
	$('#gallery li').hover(function(){ 
    var clone = $(this).children('p').clone();
    $('body').append(clone);
    clone.addClass('dummyP');


    var hOffset = $(window).height()-615;
    hOffset=hOffset>1?Math.ceil(hOffset/2):0;
    var hPage = Math.ceil((615-$(this).children('p').height())/2)+hOffset;
    $('.dummyP').css('top', hPage+'px');

    var wOffset = $(window).width()-$(this).offset().left-$(this).children('img').width();
    if($(this).children('img').attr('id')=='middle_image1'){wOffset=wOffset-255;}else if($(this).children('img').attr('id')=='middle_image2'){wOffset=wOffset-230;}
    var wOffset2 = $(window).width()-wOffset-clone.width()-$(this).children('img').width()-38;
    var wOffset3 = $(window).width()-wOffset+20;
    if(wOffset-100<clone.width()){
      $('.dummyP').css('left', wOffset2+'px');
    }else{
      $('.dummyP').css('left', wOffset3+'px');
    }

    if(clone.children('img').attr('src')!=''){clone.show();}

	}, function(){
    $('.dummyP').remove();
  });
  

});











// position wrapper in the center of a page
function wrapPos()
{
	var x = $(window).height() - 615;
	var y = $(window).width() - 1172;
	x = x < 2 ? 2 : x;
	y = y < 2 ? 2 : y;
	$('#wrapper').css('margin-top', ((x/2)-1) + 'px');
	$('#wrapper').css('margin-left', (y/2) + 'px');	
}
