$(document).ready(function() {

    var cache = new Array();
    var isOpera = window.opera ? true : false;

    preloadImages('images/read-more-button-over.png');

    /*
     * CUFON replacement */

    Cufon.replace('#content .main-area h1, #content .main-area h2, #content .main-area h3,' +
	'#content .main-area h4, #content .main-area h5, #content .main-area h6,'
	//	'#footer h4'
	);

    /*
     * ROUNDED CORNERS */

    $('#root ul.more-news-navbar li a').corner("round 5px");
    $('#root table, #root div.text-box').corner("round 6px");
    $('#root ul.comment-list li ').corner("round 8px");


    /*
     * Search bar initialization
     */

    $('#search').focusin(function() {
	if ( $(this).val() == 'Search term ...' ) {
	    $(this).val('');
	}
    });
    $('#search').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Search term ...');
	}
    });


    /*
     * PORTFOLIO INITIALIZATION */

    portfolioInit();

    /*
     * PAGE THUMBNAILS */

    $('#thumbnail-opener').pixelBookmark({
	menuTitle: 'Bookmarks Menu',
	baseContainer: $('#header-content'),
	bookmarkContainer: $('#root')
    });

    /*
     * MAIN MENU INITIALIZATON */

    $('#menu-bar').pixelMenu({
	moreText: ' +'
    });

    /*
     * FRONTPAGE SLIDER INITIALIZATION */

    frontpageSliderInit();
    frontpageTransitionSliderInit();


    /*
     * MULTI TABS */

    $('#root ul.multi-tabs').pixelMultiTabs({
	animationSpeed: 300
    });


    /*
     * PORTFOLIO ANIMATION - QUICKSAND */

    portfolioAnimationInit();


    /*
     * FOOTER GOTO TOP BUTTON */

    $('#sub-footer a.top-button').click(function(e) {
	e.preventDefault();
	if ( isOpera === true ) {
	    $('html').animate({
		scrollTop: '0px'
	    }, 500, 'easeInOutCubic');
	} else {
	    $('html, body').animate({
		scrollTop: '0px'
	    }, 500, 'easeInOutCubic');
	}
    });


    /*
     * CONTACT FORM */

    $('#send-email-button').click(function() {
	$.post('email.php', {
	    name: $('#contact-name').val(),
	    email: $('#contact-email').val(),
	    subject: $('#contact-subject').val(),
	    message: $('#contact-message').val()
	}, function(data) {
	    var response = '<div class="warning-box"><span>'+ data +'</span></div>';
	    $('#email-form div.warning-box').remove();
	    $('#email-form').prepend(response);
	    $('#email-form div.warning-box').hide().fadeIn(500);
	})
	return false;
    });

    /*
     * AJAX IMAGE PRELOADING */
//    ajaxImagePreload();


    /*
     * TOOLTIP */

     $('.tooltip').pixelTooltip({
	 color: 'white',
	 opacity: 1
     });


    function preloadImages() {
	var img;
	var arg = arguments.length;
	for ( i = 0; i < arg ; i++ ) {
	    img = document.createElement('img');
	    img.src = arguments[i];
	    cache.push(img);
	}
    }

});

/*
 * IMAGE PRELOADING */

function ajaxImagePreload() {
    $('#root img.ajax-preload').each(function() {

	var src = $(this).attr('src'),
	width = $(this).width(),
	height = $(this).height(),
	style = $(this).attr('class'),
	image = new Image();

	image.src = src + '?' + Math.floor(Math.random() * 1000000);

	style = style.replace('ajax-preload', '');
	$(this).wrap('<div class="'+ style +' preloader" />');
	$(this).hide();
	var container = $(this).parent();

	$(this).parent().css({
	    width: width + 10 + 'px',
	    height: height + 10 + 'px',
	    padding: 0
	});
	$(this).remove();

	$(image).load(function(){
	    container.append(image);
	    container.find('img').hide().attr('class', style).fadeIn(1500).unwrap().css({
		width: width + 'px',
		height: height + 'px'
	    });
	});
    });
}

/*
 * PORTFOLIO INIT */
function portfolioInit() {
    $("a[rel^='prettyPhoto']").prettyPhoto({
	theme: 'facebook'
    });
    $("a[rel^='prettyPhoto']").hover(
	function() {
	    $(this).find('img').stop().animate({
		opacity: 0.5
	    }, 400);
	},
	function() {
	    $(this).find('img').stop().animate({
		opacity: 1
	    }, 400);
	});
}

/*
 * PORTFOLIO QUICKSAND INTEGRATION PIXEL PLUGIN
 **/

function portfolioAnimationInit() {

    var navItems = 0;
    var navbar = '<ul class="portfolio-animation-navbar">';

    $('#root ul.portfolio-animation-source').each(function(i) {
	if ( i === 0 ) {
	    navbar += '<li class="selected"><a href="#"><span>'+ $(this).attr('title') + '</span></a></li>';
	} else {
	    navbar += '<li><a href="#"><span>'+ $(this).attr('title') + '</span></a></li>';
	}
	navItems++;
    });

    navbar += '</ul>';

    $('#root ul.portfolio-animation-destination').wrap('<div/>');

    /*
     * NO AJAX */

    if ( navItems > 0 ) {
	$('#root ul.portfolio-animation-destination').parent().prepend(navbar);
	$("#root ul.portfolio-animation-navbar li a").click(function(e) {
	    e.preventDefault();
	    var index = $("#root ul.portfolio-animation-navbar li a").index($(this));
	    $("#root ul.portfolio-animation-navbar li").removeAttr('class');
	    $(this).parent().attr('class', 'selected');

	    $('#root ul.portfolio-animation-destination').quicksand(
		$('#root ul.portfolio-animation-source').eq(index).find('li'), {
		    attribute: 'title',
		    easing: 'easeInOutQuad',
		    adjustHeight: 'dynamic'
		}, function() {
		    portfolioInit();
		}
	    );
	});
    } else {

	/*
	 * AJAX */

	$("#root ul.portfolio-animation-navbar li a").click(function(e) {

	    $(".loading").hide().html('Loading gallery ...').fadeIn(300);

	    var url = $(this).attr('href'),
	    selector = $(this).attr('rel');

	    $("#root ul.portfolio-animation-navbar li").removeAttr('class');
	    $(this).parent().attr('class', 'selected');

	    $.post(url,
	    {
		selector: selector
	    },
	    function(data) {
		$('#root ul.portfolio-animation-destination').quicksand(
		    $(data).find('li'), {
			attribute: 'title',
			easing: 'easeInOutQuad',
			adjustHeight: 'dynamic'
		    }, function() {
			portfolioInit();
			$(".loading").fadeOut(300);
		    }
		    );
	    });
	    e.preventDefault();
	});
    }
}

/*
 *
 * FRONTPAGE SLIDERS INITIALIZATIONS */

function frontpageTransitionSliderInit() {

    var DELAY = 30;

    if ( window.SLIDER_TYPE == undefined ) {
	SLIDER_TYPE = 1;
    }

    if ( SLIDER_TYPE === 4 ) {
	DELAY = 80;
    }

    $("#transition-slider").transitionSlider({
	slideWidth: 920,
	slideHeight: 280,
	selSlide: 0,
	delay: DELAY,
	animationStyle : SLIDER_TYPE,
	animationSpeed: 500,
	autoScrollTimer: 5,
	pieceV: 1,
	pieceH: 28,
	hasNavBar: true
    });
}

/*
 * FRONTPAGE SLIDER INIT */

function frontpageSliderInit() {
    $("ul#slider").pixelFadeSlider({
	slideWidth: 940,
	slideSpeed: 1000,
	autoScrollTimer: 0,
	hasNavBar: true
    });
}

/*
 * PIXEL TOOLTIP */

(function($) {

    $.pixelTooltip = {
	defaults: {
	    color: 'white',
	    speed: 400,
	    opacity: 1
	}
    }

    $.fn.extend({
	pixelTooltip: function(config) {
	    var config = $.extend({}, $.pixelTooltip.defaults, config);
	    config.selector = $(this);
	    initialize(config);
	}
    });

    function initialize(config) {
	config.selector.each(function() {

	    $(this).hover(
		function() {

		    var image = 'images/tooltip-bkg.gif';

		    if ( config.color === 'black' ) {
			image = 'images/tooltip-black-bkg.gif';
		    }

		    var tooltipText = $(this).attr('title'),
		    tooltipBase = '<span class="tooltip-base"><strong class="'+ config.color +'">'+ tooltipText + '</strong><img src="'+ image +'" alt="" class="pointer" /></span>';

		    $(this).parent().prepend(tooltipBase);
		    $(this).parent().css({
			position: 'relative'
		    });

		    $(this).parent().find('.tooltip-base').css({
			display: 'block',
			opacity: 0,
			top: -62 + 'px'
		    }).show().stop().animate({
			top: -58 + 'px',
			opacity: config.opacity
		    }, config.speed);
		},
		function() {
		    $(this).parent().find('.tooltip-base').stop().animate({
			top: -62 + 'px',
			opacity: 0
		    }, config.speed - 100, function() {
			$(this).remove();
		    });
		});
	});
    }
    
}(jQuery));

/*
 *
 * PIXEL MULTI TAB PLUGIN */

(function($) {

    $.pixelMultiTabs = {
	defaults: {
	    animationSpeed: 600
	}
    };

    $.fn.extend({
	pixelMultiTabs: function(config) {
	    var config = $.extend({}, $.pixelMultiTabs.defaults, config);
	    config.container = $(this);
	    initialize(config);
	}
    });

    function initialize(config) {
	config.container.wrap('<div/>');
	createNavBar(config);
	config.container.css({
	    overflow: 'hidden',
	    marginBottom: '20px'
	});
	config.container.find('li').css({
	    display: 'none'
	});
	config.container.find('li').eq(0).css({
	    display: 'block'
	}).attr('class', 'selected');
    }
    function createNavBar(config) {
	var index = 0;
	var navBar = '<ul class="multi-tabs-navbar">';
	config.container.find('li').each(function() {
	    if ( index === 0 ) {
		navBar += '<li class="selected"><a href="#">'+ $(this).attr('title') +'</a></li>';
	    } else {
		navBar += '<li><a href="#">'+ $(this).attr('title') +'</a></li>';
	    }
	    index++;
	});
	navBar += '</ul>';
	config.container.parent().prepend(navBar);
	navBarAction(config);
    }
    function navBarAction(config) {
	config.container.parent().find('ul.multi-tabs-navbar li a').click(function(e) {
	    e.preventDefault();
	    config.container.parent().find('ul.multi-tabs-navbar li.selected').removeAttr('class');
	    $(this).parent().attr('class', 'selected');
	    var selIndex = config.container.parent().find('ul.multi-tabs-navbar li a').index($(this));
	    config.container.find('li.selected').fadeOut(config.animationSpeed, function() {
		config.container.find('li').removeAttr('class');
		config.container.find('li').eq(selIndex).fadeIn(config.animationSpeed).attr('class', 'selected');
	    });
	})
    }
})(jQuery);


/*
 *
 * PIXEL BOOKMARK PLUGIN */

(function($){

    $.pixelBookmark = {
	defaults: {
	    menuTitle: 'Page Bookmarks',
	    baseContainer: null,
	    bookmarkContainer: null,
	    animationType: 'easeInOutCubic',
	    animationSpeed: 800
	}
    };

    $.fn.extend({
	pixelBookmark: function(config) {
	    var config = $.extend({}, $.pixelBookmark.defaults, config);
	    config.container = $(this); // this should be a hyperlink
	    initialize(config);
	}
    });

    function initialize(config) {
	var menu = createThumbMenu(config);
	var left = config.baseContainer.offset().left;
	var width = config.baseContainer.width();
	$('body').prepend(menu);
	$('ul#bookmarks-menu').css({
	    left: width + left - 220 + 'px'
	});
	addThumbnailEvent(config);
	animate(config);
    }

    function createThumbMenu(config) {
	var thumbList = config.bookmarkContainer.find('a.page-bookmark'),
	menu = '', title = '', name = '', index = 0;

	if ( thumbList.length > 0 ) {
	    menu += '<ul id="bookmarks-menu">';
	    menu += '<li class="bookmark-top">&nbsp;</li>';
	    menu += '<li class="headline"><h5>'+ config.menuTitle +'</h5></li>';
	    thumbList.each(function() {
		index++;
		title = $(this).attr('title');
		name = $(this).attr('name');
		menu += '<li><a href="#'+ name +'"> [+] '+ title +'</a></li>';
	    });
	    menu += '<li class="close-button"><a href="#">Close</a></li>';
	    menu += '<li class="bookmark-bottom">&nbsp;</li>';
	    menu += '</ul>';
	}
	return menu;
    }

    function addThumbnailEvent(config) {
	$('ul#bookmarks-menu li[class!=close-button] a').click(function(e) {
	    e.preventDefault();
	    var offset = 0, offsetElem = null, name = '', isOpera = window.opera ? true : false;

	    name = $(this).attr('href');
	    name = name.substr(name.indexOf('#', 0) + 1);	    

	    offsetElem = config.bookmarkContainer.find('a.page-bookmark[name='+ name +']');
	    offset = offsetElem.offset();

	    if ( isOpera ) {
		$('html').animate({
		    scrollTop: offset.top - 20 + 'px'
		}, config.animationSpeed, config.animationType);
	    } else {
		$('html, body').animate({
		    scrollTop: offset.top - 20 + 'px'
		}, config.animationSpeed, config.animationType);
	    }

	    $('ul#bookmarks-menu').animate({
		top: offset.top + 20 + 'px'
	    }, 1000, config.animationType);
	});
    }

    function animate(config) {

	var isOpen = false;
	var h = $('ul#bookmarks-menu').height();

	config.container.click(function(e) {
	    e.preventDefault();
	    if ( !isOpen ) {
		$('ul#bookmarks-menu').css({
		    top: -h - 2 + 'px',
		    display: 'block'
		});
		$('ul#bookmarks-menu').animate({
		    top: '50px'
		}, 500, config.animationType);
		isOpen = true;
	    }
	});
	$('ul#bookmarks-menu li.close-button a').click(function(e) {
	    e.preventDefault();
	    $('ul#bookmarks-menu').animate({
		top: -h - 2 + 'px'
	    }, 500, config.animationType);
	    isOpen = false;
	});
    }
})(jQuery);


String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};
