/**
 * Common Stylesheet
 *
 * @author 		Sam Loomes
 * @email		samloomes@gmail.com
 * @url 		http://www.samloomes.com
 * @copyright 	Copyright (c) 2011, samloomes.com. All rights reserved.
 */

// Load Background
$(document).ready(function() {
	$('body').css('background-image', 'url("wp-content/themes/mjmedia/img/common/bg.jpg")');
});

// Get Querystring
function getQuerystring(key, url, default_) {
	if (default_ == null) {
		default_="";
	} 
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = ''
	if (url != null) {
		qs = regex.exec(url);
	} else {
		qs = regex.exec(window.location.href);
	}
	if (qs == null) {
		return default_;
	} else {
		return qs[1];
	}
}

// Get Next/First
function nextOrFirst(selector) {
	var next = $(selector).next();
	return (next.length) ? next : $(selector).siblings().first();
}

// Main Scrollbars
function setMainScrollbars(listItem) {
	if ($(listItem).find('div.detail').length > 0) {
		$(listItem).find('div.detail').children('div.container').jScrollPane({
			showArrows: true
		});
	}
	if ($(listItem).find('div.clients').length > 0) {
		var clientList = $(listItem).find('div.clients').children('ul');
		var listHeight = $(clientList).height();
		var listItemMargin = parseInt($(clientList).children('li:first-child').css('margin-bottom'));
		var parentHeight =  $(clientList).parent().height();
		if ((listHeight - listItemMargin - parentHeight) > listItemMargin) {
			$(clientList).parent().addClass('clients-scroll');
			$(listItem).find('div.clients').jScrollPane({
				showArrows: true
			});
		}
	}
}
$(document).ready(function() {
	var listItem = $('div.main ul li.active');
	setMainScrollbars(listItem);
});


// Tab Handling
$('div.main ul li span.tab').live('click', function() {
	var listItem = $(this).parent();
	if (!$(listItem).hasClass('active')) {
		$(listItem).siblings('li').removeClass('active');
		$(listItem).addClass('active');
		setMainScrollbars(listItem);
	}
});

// Work Scroller
var scrollInterval;
var canSetTimer = false;
$(document).ready(function() {
	var workList = $('#primary div.main div.work ul');
	if ($(workList).children('li').length > 1) {
		$(workList).before('<span class="button left"><span>Left</span></span>');
		$(workList).after('<span class="button right"><span>Right</span></span>');
		$(workList).wrap('<div class="work-container"></div>');
		var listWidth = 0;
		$(workList).children('li').each(function() {
			listWidth += $(this).width();
		});
		$(workList).width(listWidth);
		$(workList).parent().siblings('span.button').bind('click', scrollWorkHandler);
		$(workList).children('li').click(function() {
			$(this).find('a.modal-link').click();
		});
		scrollInterval = setInterval('scrollWork()', 4000);
		$('#primary div.main div.work ul > li').hover(function() {
			clearInterval(scrollInterval);
			canSetTimer = true;
		}, function() {
			if (canSetTimer) {
				canSetTimer = false;
				clearInterval(scrollInterval);
				scrollInterval = setInterval('scrollWork()', 4000);
			}
		});
	}
});

function scrollWork() {
	$('#primary div.main div.work span.right').click();
}

function scrollWorkHandler() {
	var workList = $('#primary div.main div.work ul');
	var itemWidth = $('li:first-child', workList).width();
	$(workList).parent().siblings('span.button').unbind('click', scrollWorkHandler);
	/*$('li div', workList).animate({
		height: 0
	}, 100, "swing", function() {
		
	});*/
	$('li div', workList).fadeOut(200, function() {});
	if ($(this).hasClass('left')) {
		$('li:last-child', workList).prependTo(workList);
		$(workList).animate({
			left: '-=' + itemWidth
		}, 0, function() {
		});
		$(workList).animate({
			left: '+=' + itemWidth
		}, 500, 'swing', function() {
			/*$('li div', workList).animate({
				height: 72
			}, 100, "swing", function() {
			});*/
			$('li div', workList).fadeIn(200, function() {});
			$(workList).parent().siblings('span.button').bind('click', scrollWorkHandler);
		});
	} else {
		$(workList).animate({
			left: '-=' + itemWidth
		}, 500, 'swing', function() {
			$('li:first-child', workList).appendTo(workList);
			$(workList).animate({
				left: '+=' + itemWidth
			}, 0, function() {
				/*$('li div', workList).animate({
					height: 72
				}, 100, "swing", function() {
				});*/
				$('li div', workList).fadeIn(100, function() {});
				$(workList).parent().siblings('span.button').bind('click', scrollWorkHandler);
			});
		});
	}
	clearInterval(scrollInterval);
	scrollInterval = setInterval('scrollWork()', 4000);
}

// Work Scroller
var modalScrollInterval;
var canSetModalTimer;
function initModalWorkScroller(selectedWork) {
	var workList = $('div.modal-work div.list div.list-container > ul');
	if ($(workList).children('li').length > 4) {
		$(workList).parent().before('<span class="button up"><span>Up</span></span>');
		$(workList).parent().after('<span class="button down"><span>Down</span></span>');
		$(workList).parent().css('paddingTop','0');
		$(workList).parent().siblings('span.button').bind('click', scrollModalWorkHandler);
	}
	$(workList).children('li').bind('click', scrollModalClickHandler);
	if (selectedWork != '') {
		$('#work-' + selectedWork).click();
	}
	var workGallery = $('div.modal-work div.detail ul.images');
	if ($(workGallery).children('li').length > 1) {
		initScrollModalGallery();
	}
}

function initScrollModalGallery() {
	var workGallery = $('div.modal-work div.detail ul.images');
	$(workGallery).children('li:first').addClass('active');
	$(workGallery).children('li.active').next().addClass('preactive');
	clearInterval(modalScrollInterval);
	modalScrollInterval = setInterval('scrollModalGallery()', 2500);
	$('div.modal-work div.detail ul li').hover(function() {
		clearInterval(modalScrollInterval);
		canSetModalTimer = true;
	}, function() {
		if (canSetModalTimer) {
			canSetModalTimer = false;
			clearInterval(modalScrollInterval);
			modalScrollInterval = setInterval('scrollModalGallery()', 2500);
		}
	});
}

function scrollModalGallery() {
	var workGallery = $('div.modal-work > div.detail > ul.images');
	if ($(workGallery).children('li').length > 1) {
		var active = $(workGallery).children('li.active');
		$(workGallery).children('li.active').fadeOut('slow', function() {
			$(workGallery).children('li').removeClass('active');
			$(workGallery).children('li').removeClass('preactive');
			nextOrFirst($(active)).addClass('active');
			nextOrFirst($(workGallery).children('li.active')).addClass('preactive');
			$(workGallery).children('li').css('display','block');
		});
	}
}

function scrollModalClickHandler() {
	var detailBox = $(this).parents('div.list').siblings('div.detail');
	var ul = $(this).children('div.detail').children('ul.images').clone();
	var text = $(this).children('div.detail').children('div.text').clone();
	$(detailBox).children('ul.images').remove();
	$(detailBox).children('div.text').remove();
	$(detailBox).html(text);
	$(detailBox).children('div.text').before(ul);
	initScrollModalGallery();
	return false;
}

function scrollModalWorkHandler() {
	var workList = $('div.modal-work div.list div.list-container > ul');
	$(workList).parent().siblings('span.button').unbind('click', scrollModalWorkHandler);
	if ($(this).hasClass('up')) {
		$(workList).children('li:last-child').prependTo(workList);
		$(workList).animate({
			top: '-=103'
		}, 0, function() {
		});
		$(workList).animate({
			top: '+=103'
		}, 300, 'swing', function() {
			$(workList).parent().siblings('span.button').bind('click', scrollModalWorkHandler);
		});
	} else {
		$(workList).animate({
			top: '-=103'
		}, 300, 'swing', function() {
			$(workList).children('li:first-child').appendTo(workList);
			$(workList).animate({
				top: '+=103'
			}, 0, function() {
				$(workList).parent().siblings('span.button').bind('click', scrollModalWorkHandler);
			});
		});
	}
}

// Signup Form
function initSignupForm() {
	$('div.modal-signup input.submit').after('<span class="submit" name="subscribe">Send Request</span>');
	$('div.modal-signup input.submit').remove();
	$('div.modal-signup span.submit').click(function() {
		$('div.modal-signup p.error').remove();
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if ($('#signup-name').val() == '' || $('#signup-email').val() == '') {
			$(this).parents('form').find('label.name-label').before('<p class="error">Please ensure you have entered all of the required information.</p>');
			$.colorbox.resize();
		} else if (!emailReg.test($('#signup-email').val())) {
			$(this).parents('form').find('label.name-label').before('<p class="error">Please ensure you have entered a valid email address.</p>');
			$.colorbox.resize();
		} else {
			$(this).parents('form').submit();
		}
	});
}

// Newsletters Signup Form
function initNewslettersSignupForm() {
	$('form.alo_easymail_widget_form_public table.alo_easymail_form_table tr').each(function() {
		$(this).children('td:first-child').addClass('label');
	});
	$('div.modal-signup input.input-submit').after('<span class="submit" name="subscribe">Send Request</span>');
	$('div.modal-signup input.input-submit').hide();
	$('div.modal-signup span.submit').click(function() {
		$(this).siblings('input.input-submit').click();
		$.colorbox.resize();
	});
}

// Colorbox
$(document).ready(function() {
	$('#footer').after('<div id="ajax-content"></div>');
	
	$('a.modal-link, #team a, #music a, #contact a:not(.mail)').click(function() {
		var url = $(this).attr('href');
		$.ajax({
			url: url,
			cache: false,
			success: function(data) {
				var modal = $(data).find('div.modal');
				$('#ajax-content').html(modal);
				if ($('#ajax-content div.modal').hasClass('modal-work')) {
					initModalWorkScroller(getQuerystring('work', url));
				}
				if ($('#ajax-content div.modal').hasClass('modal-signup')) {
					initNewslettersSignupForm();
				}
				// IE6 Or IE7
				if ($.browser.msie && ($.browser.version == 6 || $.browser.version == 7)) {
					if ($('#ajax-content div.modal').hasClass('modal-work')) {
						$.colorbox({inline:true, href:'#ajax-content div.modal', innerWidth:'848px'});
					} else if ($('#ajax-content div.modal').hasClass('modal-signup')) {
						$.colorbox({inline:true, href:'#ajax-content div.modal', innerWidth:'480px'});
					} else if ($('#ajax-content div.modal').hasClass('modal-map')) {
						$.colorbox({inline:true, href:'#ajax-content div.modal', innerWidth:'625px'});
					} else if ($('#ajax-content div.modal').hasClass('modal-music')) {
						$.colorbox({inline:true, href:'#ajax-content div.modal', innerWidth:'705px'});
						if ($.browser.msie && $.browser.version == 6) {
							DD_belatedPNG.fix('div.modal-music div.logos ul li img');
						}
					} else if ($('#ajax-content div.modal').hasClass('modal-team')) {
						$.colorbox({inline:true, href:'#ajax-content div.modal', innerWidth:'698px'});
					} else {
						$.colorbox({inline:true, href:'#ajax-content div.modal', innerWidth:'445px'});
					}
				} else {
					$.colorbox({inline:true, href:'#ajax-content div.modal'});
				}
				/* Google Analytics */
				_gaq.push(['_trackPageview', url]);
				clicky.log(url);
			}
		});
		return false;
	});
});

// Live News
$(document).ready(function() {
	var newsContainer = $('#news div.panel div.content div.news-container');
	getTweets(newsContainer);
});

function getTweets(newsContainer) {
	var url = "http://api.twitter.com/1/statuses/user_timeline/MJMedia97.json?include_rts=true&callback=?";
	var list = '';
	$.getJSON(url, function(data) {
		list = '<ul>';
		for (var i = 0; i < 5; i++) {
			tweettext = data[i].text;
		    tweettext = tweettext.replace(/(http\:\/\/[A-Za-z0-9\/\.\?\=\-]*)/g,'<a href="$1" target="_blank">$1</a>');
			tweettext = tweettext.replace(/@([A-Za-z0-9\/_]*)/g,'<a href="http://twitter.com/$1" target="_blank">@$1</a>');
			tweettext = tweettext.replace(/#([A-Za-z0-9\/\.]*)/g,'<a href="http://twitter.com/search?q=$1" target="_blank">#$1</a>');
			list += '<li><p>' + tweettext + '</p></li>';
		}
		list += '</ul>';
		$(newsContainer).html(list);
		$(newsContainer).jScrollPane({
			showArrows: true
		});
	});
}

// IE6
if ($.browser.msie && $.browser.version == 6) {
	$(document).ready(function() {
		$('div.main ul li span.tab').hover(
			function() {
				$(this).addClass('tab-hover');
			},
			function() {
				$(this).removeClass('tab-hover');
			}
		);
	});
}
