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

$(document).ready(function() {
    enable_cycle = true;                // Enable/Disable the Slideshow Cycle
    slide_animation_time = 260;         // Base time (t) to use on Slides
    slide_transition_duration = 8;      // Seconds for each slide
    enable_twitter = true;              // Enable Twitter
    twitter_user = 'derdesign';         // Twitter Username

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

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() {
    // Get the Theme
    theme = $('meta[name=gateway-color]').attr('content');

	// Internet Explorer 6 Fixes
	if (IE6) {
		DD_belatedPNG.fix(".png, ul.check li");
		$('ul#navigation li').dropdown();
	}
    
	// 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');
	}

    // Form Validation
	$('form.validate input[type=submit]').click(function() {
		var parent = $(this).parents('form');
		if ( parent.hasClass('validate') ) {
			parent.validate(function() {
				if (submitted_form) {alert('Your message/comment is on the way, thanks.');return false;}
				jQuery.ajax({
					type: "POST",
					url: "sendmsg.php",
					data: form_data,
					success: function(response){
						if (response=='success') {
							parent.find('.message').slideDown(400);
							submitted_form = true;
						}
					}
				});
			});
			return false;
		} else {
			return true;
		}
	});

	// 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(){},
		callback: function(){}
	});

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

}

function twitter_init() {
    // Twitter Feed
    if (enable_twitter && $('#interactive .twitter').length == 1 ) {
        $('#interactive .twitter p').text('');
        $('#interactive .twitter .loader').show();
        $('#interactive .twitter p').twitter(twitter_user, 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 cub3r_init() {
	if ( $('#cub3r').length == 0 ) {return false;}
	var flashvars = {};
	flashvars.xml = "../core/images/config.xml";
	var attributes = {};
	attributes.wmode = "transparent";
	attributes.id = "slider";
	swfobject.embedSWF("../core/js/cu3er.swf", "cub3r", "860", "332", "9", "../core/js/expressInstall.swf", flashvars, attributes);
}

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 = '../core/images/interface/slider-bg.jpg';
    if (theme != null) {slider_img = '../core/images/interface-' + theme + '/slider-bg.jpg' ;}
	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);

	});

}
