/*
    GATEWAY JS/CORE
    Copyright (c) 2009, Ernesto Mendez
    http://der-design.com
*/

$ = jQuery;

$(document).ready(function() {
    enable_twitter = true;              // Enable Twitter

    initialize();
    hover_events();
    featured_init();
    twitter_init();
	init_forms();
});

function featured_cycle_init(enabled) {
	if ( $('#featured').length == 0 ) {return false;}
    if (!enabled) {$('#featured .meta a.left').css('right','30px');return false;}
    
	cycle_enabled = true;
	$('#featured .meta .trigger').run_cycle('#featured .meta .pause', '#featured .separators .pause', '#featured .progress .bar', slide_transition_duration );
}

function initialize() {
    // Meta Variables
	base_url = $('link[rel=index]').attr('href');

	// IE6/7 Fixes
	if ( IE67 && $('#footer-widgets').height() <= 20 ) {
		$('#footer-widgets').css('font-size','0px');
	}

	// Internet Explorer 6 Fixes
	if (IE6) {
		// PNG Fix
		DD_belatedPNG.fix(".png, ul.check li");
		$('ul#navigation li').dropdown();
		var color;

		// Add active page support for IE6
		switch (color_theme) {
			case 'Orange':
				$('#header ul#navigation li.current_page_item > a').css('color', '#ff6801');
				$('#header ul#navigation li ul li.current_page_item > a').css('color', '#ffffff');
				$('#page-meta .links li.current_page_item > a').css({
					'background-color' : '#ffffff',
					'color' : '#ff6801'
				});
				break;
			case 'Black':
				$('#header ul#navigation li.current_page_item > a').css('color', '#241e1e');
				$('#header ul#navigation li ul li.current_page_item > a').css('color', '#ababab');
				$('#page-meta .links li.current_page_item > a').css({
					'background-color' : '#ffffff',
					'color' : '#241e1e'
				});
				break;
			case 'Blue':
				$('#header ul#navigation li.current_page_item > a').css('color', '#018aff');
				$('#header ul#navigation li ul li.current_page_item > a').css('color', '#525d62');
				$('#page-meta .links li.current_page_item > a').css({
					'background-color' : '#ffffff',
					'color' : '#018aff'
				});
				break;
		}
		
	}

	// Remove title from navigation elements
	$('ul#navigation li a').removeAttr('title');

	// Alternating Colors for menu items
	$("ul#navigation li").find("ul:first").children("li").children("a:even").addClass('alt');

	// Add ID's to portfolio items
	var counter = 1; $('#portfolio .thumbs .thumb').each(function() {$(this).attr('id', 'thumb-' + counter);counter += 1;});
	$('#back-to-top').top_animation('easeInOutExpo', 800);

	// Setup Hover Elements
	if (!IE) {
		$('#featured .separators .pause').css({'visibility':'visible', 'display':'block'});
		$('#portfolio .wrap .thumbs .thumb .hover a img.top, #portfolio .wrap .thumbs .thumb .hover .heading, #featured .separators .pause').css('opacity','0');
	}

	// Prettyphoto Configuration
	$("a[rel^='lightbox']").prettyPhoto({
		animationSpeed: 'normal',
		padding: 40,
		opacity: 0.40,
		showTitle: true,
		allowresize: true,
		counter_separator_label: '/',
		theme: 'light_square',
		hideflash: false,
		modal: false,
		changepicturecallback: function(){
			$('#content object').css('visibility','hidden');
		},
		callback: function(){
			$('#content object').css('visibility','visible');
		}
	});

    // Support for target="_blank"
    $('a[rel=_blank]').attr('target','_blank');

	// Show tags list
	$('a.more-tags').click(function() {
		var tagslist = $(this).parents('.post').find('.tagslist').slideDown();
		return false;
	});

	// Hide tags list
	$('a.tagclose').click(function() {
		$(this).parent().slideUp();
		return false;
	});

	// Fix post images, make sure they don't go outside the layout
	fix_post_images();

}

function fix_post_images() {
	if ($('#content .posts .single, #content .posts .page, #content .page-full-width').length == 1) {

		var max_width = 524;

		if ( $('#content .wrap').hasClass('full-width') ) { max_width = 830; }

		$('#content .posts .single .post-content img, #content .posts .page img, #content .page-full-width img, #content embed').each(function() {

			var img_width = $(this).width();

			var img_height = $(this).height();

			if (img_width > max_width) {

				var new_height = (img_height * max_width) / img_width;

				$(this).width(max_width).height(new_height);

			}
			
		});

	}
}

function twitter_init(anchor) {
    // Twitter Feed
	if (twitter_user == null) { return false; }
    if (enable_twitter && $('#interactive .twitter').length == 1 ) {
        $('#interactive .twitter p').text('');
        $('#interactive .twitter .loader').show();
        $('#interactive .twitter p').twitter(twitter_user, '#interactive .twitter a.icon', function(data) {
            $('#interactive .twitter .loader').hide();
        });
    }
}

function hover_events() {
	$.fn.toggleThumb = function(animList, duration) {
		var id = $(this).attr('id');
		var selector = '#' + id + ' img.top, #' + id + ' .heading';
		$(selector).stop().animate(animList, duration);
	}

	$.fn.toggleThumbIE = function(show) {
		var id = $(this).attr('id');
		var selector = '#' + id + ' img.top, #' + id + ' .heading';
		if (show) {$(selector).show()} else {$(selector).hide()}
	}

	// Portfolio Thumbs
	if (!IE) {
		$('#portfolio .thumbs .thumb .hover, #portfolio .thumbs img.top, #portfolio .thumbs .heading').css('display','block');
		$('#portfolio .thumbs .thumb').hover(function() {
			$(this).toggleThumb({'opacity':'1'},500);
		}, function() {
			$(this).toggleThumb({'opacity':'0'},300);
		});
	} else {
        $('#portfolio .thumbs .thumb .hover').show();
		$('#portfolio .thumbs .thumb').hover(function() {
			$(this).toggleThumbIE(true);
		}, function() {
			$(this).toggleThumbIE(false);
		});
	}

	// Portfolio Sidebar Widget
	if (!IE) {
		$('#portfolio-widget .thumbs .thumb a.hover').css({'opacity':'0', 'display':'block'}).hover(function() {
			$(this).stop().animate({'opacity':'1'},500);
		},function() {
			$(this).stop().animate({'opacity':'0'},300);
		});
	} else {
		$('#portfolio-widget .thumbs .thumb li').hover(function() {
            $(this).parent().find('.hover').show();
		}, function() {
			$(this).parent().find('.hover').hide();
		});
	}

	// Dropdown Menu
	$('ul#navigation li').hover(function() {
		$(this).find('ul:first').css({visibility: 'visible',display: 'none'}).slideDown(400,'easeOutQuart');
	}, function() {
		$(this).find('ul:first').css({visibility: 'hidden'});
	});

}

function featured_init() {
	if ( $('#featured').length == 0 ) {return false;}

	// Set global objects
	left_slide = $('#featured .slides .s1');
	middle_slide = $('#featured .slides .s2');
	right_slide = $('#featured .slides .s3');
	info_link = $('#featured .meta a.info');

	// Populate cached_data
	$('.featured-cache li a').each(function() {
		cached_data.push( $(this) );
	});

	// Define Extra Images
	

	extra_images = [];
    var slider_img = $('#featured .slides li.s1 img').attr('src');
	extra_images.push(slider_img);

	// Preload Images
	preload(featured_images, extra_images, function() {
		// Before Preload
		$('#featured .loader').show();
	}, function() {
		// After Preload
		$('#featured .loader').hide();
		$('#featured .meta a.left, #featured .meta a.right').show();
		featured_cycle_init(enable_cycle);
		if (cycle_enabled) { 
			$('#featured .meta a.pause').show();
			$('#featured .progress .bar').animate({'width':'860px'}, run_cycle.time);
		}
	});

	// Slides Animation
	$('#featured .meta a.left, #featured .meta a.right, #featured .meta a.trigger').transition(featured_images, function(data) {
		t = slide_animation_time;
		var easing = 'easeOutSine';
		var params = {'top':'0'};
		var new_link = cached_data[current_transition-1];

		// Display Slides and collapse progress bar
		$('#featured .slides').show();
		$('#featured .progress .bar').stop().animate({'width':'0'});

		// Slide Down
		if (current_increment > 0) {
			left_slide.animate(params,t,easing);
			setTimeout(function() {
				middle_slide.animate(params,t,easing);
			}, 0.8*t );
			setTimeout(function() {
				right_slide.animate(params,t,easing);
			}, 1.5*t );
		} else {
			right_slide.animate(params,t,easing);
			setTimeout(function() {
				middle_slide.animate(params,t,easing);
			}, 0.8*t );
			setTimeout(function() {
				left_slide.animate(params,t,easing);
			}, 1.5*t );
		}
		info_link.animate({'opacity':'0'}, 1.5*t);

		// Slide Up
		setTimeout(function() {
			current_image.width="860";current_image.height="332";
			$('#featured .trans li a img').remove();
			$('#featured .trans li a').append(current_image).attr('href', new_link.attr('href') );

			var params = {'top':'-332px'};
			var easing = 'easeInSine';
			var time = t;

			$('#featured .progress .bar').css('width','0');
			if (current_increment > 0) {
				left_slide.animate(params,time,easing);
				setTimeout(function() {
					middle_slide.animate(params,time,easing);
				}, 0.8*t);
				setTimeout(function() {
					right_slide.animate(params,time,easing);
				},1.5*t);
				setTimeout(function() {
					$('#featured .slides').hide();
				},1.5*t + time + 10);
			} else {
				right_slide.animate(params,time,easing);
				setTimeout(function() {
					middle_slide.animate(params,time,easing);
				}, 0.8*t);
				setTimeout(function() {
					left_slide.animate(params,time,easing);
				},1.5*t);
				setTimeout(function() {
					$('#featured .slides').hide();
				},1.5*t + time + 10);
			}


			// Change link information
			info_link.attr('href', new_link.attr('href') );
			info_link.text( new_link.text() );
			setTimeout(function() {
				info_link.animate({'opacity':'1'}, 1.5*t );
			}, 0.7*t);

		}, 3*t );

		// Unlock UI
		end_busy(function() {
			if (cycle_enabled) {
				run_cycle.loop();
				$('#featured .progress .bar').animate({'width':'860px'}, run_cycle.time);
			}
		}, 2*3*t + 50);

	});

}