(function(){
    if (typeof(_sspmc) == "undefined") {
        _sspmc = window._sspmc = new function(){
            this.version = "1.0";
            this.currentPage = 1;
            this.device = $('body').attr('data-device-name');
            this.browser = $('body').attr('data-browser-name');
            this.baseFeedUrl = '/singles/servlet/user/myhome/recentactivities';
            this.baseServicesUrl = '/singles/servlet/user/myhome/premiumservices';
            this.helpers = {
                increaseFont: function(){
//                	alert('inc');
                	$element = $('.font-resize');
                	currentFontSize = parseInt($element.css('fontSize').replace(/px/,''));
//                	alert('currentFontSize:' + currentFontSize);
                	newFontSize = (currentFontSize + 2) + 'px';
//                	alert('newFontSize: ' + newFontSize);
                	$element.css('fontSize', newFontSize);
                },
                decreaseFont: function(){
                	$element = $('.font-resize');
                	currentFontSize = parseInt($element.css('fontSize').replace(/px/,''));
                	newFontSize = (currentFontSize - 2) + 'px';
                	$element.css('fontSize', newFontSize);
                }
            },
            this.processLogin = {
                showFromNav: function(){
					$loginLi = $('#nav-login');
					$offset = $loginLi.offset();
					linkLeft = $offset.left;
					linkTop = $offset.top - 10;
					linkWidth = $loginLi.find('.nav-primary').outerWidth();
					loginLeft = linkLeft + linkWidth;
					loginLeft = loginLeft + 'px';
					loginTop = linkTop;
					loginTop = loginTop + 'px';
					$('#login-content').addClass('login-nav-open').css({left:loginLeft, top: loginTop, paddingRight:'20px'}).show('blind', { direction: 'horizontal' }, 1000);
                },
                hideFromNav: function(){
					$('#login-content').hide('blind', { direction: 'horizontal' }, 1000, function(){ _sspmc.processLogin.showFromHeader(); }).removeClass('login-nav-open');
                },
                showFromHeader: function(){
					$('#login-content').addClass('login-header-open').css({left:'', right:'0px', top: '150px', paddingRight:'0px'}).show('blind', { direction: 'vertical' }, 1000);
                },
                hideFromHeader: function(){
					$('#login-content').hide('blind', { direction: 'vertical' }, 1000, function(){ _sspmc.processLogin.showFromNav(); }).removeClass('login-header-open');
                },
                validateLogin: function(input){
					$this = input;
					if($('#login-username').val()=='' || $('#login-password').val()==''){
						
					}
					else{
						_sspmc.processLogin.submitLoginForm();
					}
                },
				submitLoginForm: function(input){
					$('#login-form').submit();
                }
            };
            this.changePage = {
                collapseCurrentPage: function(url){
					if($('.jScrollPaneContainer').length>0){
						$content = $('.jScrollPaneContainer');
					}
					else{
						$content = $('.content-area');
					}
					$content.hide('blind', { direction: 'vertical' }, 1000, function(){ _sspmc.changePage.goToNewPage(url); });
                },
                goToNewPage: function(url){
					window.location=url;
                },
                loadNewPage: function(url){
					if($('.jScrollPaneContainer').length>0){
						$content = $('.jScrollPaneContainer');
					}
					else{
						$content = $('.content-area');
					}
					$content.show('blind', { direction: 'vertical' }, 1000)
                }
            };
            this.processNewsletter = {
				submitNewsletterForm: function(input){
					_sspmc.processForm.validateEmail($('input[name=email-newsletter]'));
					if(!$('#footer ul.primary li.primary.last').hasClass('error')){
						_sspmc.processNewsletter.adjustHeights();
					}
                },
				adjustHeights: function(){
					newsletterHeight = $('#newsletter-content').outerHeight();
					iconsHeight = $('#footer .primary.first').outerHeight();
					if(newsletterHeight > iconsHeight){
						$('#footer .primary.first').css('height', newsletterHeight + 'px')
					}
					else if(newsletterHeight < iconsHeight){
						$('#newsletter-content').css('height', iconsHeight + 'px')
					}
					$.post('newsletter_form.php', $('#form-newsletter').serialize());
					_sspmc.processNewsletter.hideForm();
				},
				hideForm: function(){
					$('#form-newsletter').hide('blind', { direction: 'vertical' }, 1000, function(){ $('#footer ul.primary li.primary.last').addClass('success'); _sspmc.processNewsletter.showSuccess(); });
				},
				showSuccess: function(){
					$('.submit-complete').show('blind', { direction: 'horizontal' }, 500);
				}
            };
            this.processForm = {
				errorState: false,
                setupForm: function(submitElm){
					$form = submitElm.parents('form');
					_sspmc.processForm.setupSubmit(submitElm, $form);
                },
                setupSubmit: function(submitElm){
					submitElm.click(function(){
						_sspmc.processForm.validateForm(submitElm, $form);
						return false;
					});
                },
                validateForm: function(submitElm, $form){
					_sspmc.processForm.clearErrorMessages($form);
					$form.find('input.required, textarea.required').each(function(){
						if($(this).hasClass('email')){
							_sspmc.processForm.validateEmail($(this));
						}
						else{
							_sspmc.processForm.validateText($(this));
						}
					});
					if (_sspmc.processForm.errorState){
						_sspmc.processForm.showError();
					}
					else{
						_sspmc.processForm.submitForm();
					}
                },
                validateText : function(input){
					$this = input;
					if($this.val()==''){
						_sspmc.processForm.catchError($this);
					}
                },
                validateEmail: function(input){
					$this = input;
					emailVal = $this.val();
					if(emailVal==''){
						_sspmc.processForm.catchError($this);
					}
					else{
						if(emailVal.indexOf('@')>0){
							if(emailVal.indexOf('.')>emailVal.indexOf('@')){
							}
							else{
								_sspmc.processForm.catchError($this, 'bademailformat');
							}
						}
						else{
							_sspmc.processForm.catchError($this, 'bademailformat');
						}
					}
                },
                catchError: function(input, type){
					parent = input.parents('li:eq(0)');
					
					if (type=='bademailformat'){
						errorText = 'invalid format';
					}
					else{
						errorText = 'required field';
					}
					errorMarkup = '<span class="error-message">' + errorText + '</span>';
					parent.find('label').append(errorMarkup);
					parent.addClass('error');
					_sspmc.processForm.errorState = true;
                },
                clearErrorMessages: function(form){
					_sspmc.processForm.errorState = false;
					form.find('.error-message').remove().end().find('.error').removeClass('error');
                },
                showError: function(){
                },
                submitForm: function(){
					$.post('contact_form.php', $('#form-contact-us').serialize());
					_sspmc.processForm.hideForm();
                },
				hideForm: function(){
					$('#form-contact-us').hide('blind', { direction: 'vertical' }, 1000, function(){ _sspmc.processForm.showSuccess(); });
				},
				showSuccess: function(){
					$('.submit-complete').show('blind', { direction: 'vertical' }, 500);
				}
            };
            this.createVideo = {
                createVideoElm: function(filename){
					if ($.browser.msie) {
						_sspmc.createVideo.createFlowplayer(filename);
					}
					else	{
						_sspmc.createVideo.removeVideoTag(filename, 'show');
					}
                },
                removeVideoTag: function(filename, type){

					if ($.browser.msie) {
						$('#video-content object').remove();
					}
					else	{
						$('#movie').remove();
					}
					if (type=='show'){
						videoTag = '<video id="movie" controls autoplay="true" style="display:none;"></video>';
						$('#video-content').append(videoTag);
	//					$('#video-content').dialog({ width:320, height:240, autoOpen:false, modal: true, resizable:false, draggable: false });
						_sspmc.createVideo.createOgg(filename);
					}
                },
                createOgg: function(filename){
					oggHtml = '<source src="video/' + filename + '.ogg" type=\'video/ogg; codecs="theora, vorbis"\' />';
					$('#movie').append(oggHtml);
					_sspmc.createVideo.createM4v(filename);
                },
                createM4v: function(filename){
					m4vHtml = '<source src="video/' + filename + '.m4v" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />';
					$('#movie').append(m4vHtml);
					_sspmc.createVideo.showVideo();
                },
                createFlowplayer: function(filename){
					filepath = 'video/flv/' + filename + '.flv';
					flowplayer(
						"video-content", {
							src: "video/flv/flowplayer/flowplayer-3.2.4.swf",
							version: [9, 115],
							onFail: function()  {}
						}, {
						clip: filepath
						}
					);
					_sspmc.createVideo.showVideo();
                },
                showVideo: function(filename){

					//$('#video-content').dialog({ height:180 }).find('#movie').css('height', '180px');


					$('#video-content').dialog('open');
					$('.ui-widget-overlay').click(function(){
						_sspmc.createVideo.closeVideoDialog();
						return false;
					});
                },
				closeVideoDialog: function(){
					$('#video-content').dialog('close');
					_sspmc.createVideo.removeVideoTag('', 'hide');
				}
            };
        };
    }
})();

jQuery(function(){
		$('#video-content').dialog({ dialogClass: 'dialog-video', width:320, height:240, autoOpen:false, modal: true, resizable:false, draggable: false, close: function(){_sspmc.createVideo.removeVideoTag('', 'hide')} });

		$('li.audio a.thumbnail').click(function(){
			return false;	
		});
	$('li.video a, .video.album-featured.active a.thumbnail').click(function(){
		/*
		id = $(this).attr('data-id');
		$('#video-content').dialog('open');
		*/
		$this = $(this);
		filename = $(this).attr('data-id');
		_sspmc.createVideo.createVideoElm(filename);
		/*
		if(typeof($this.attr('data-y'))!='undefined' && typeof($this.attr('data-x'))!='undefined'){
			console.log('y && x not und');
			xPos = parseInt($this.attr('data-x')) + 'px';
			yPos = parseInt($this.attr('data-y')) + 'px';
			mediaWidth = parseInt($this.attr('data-width'));
			mediaHeight = parseInt($this.attr('data-height'));
			var thumbnailOffset = $this.offset();
			offsetLeft = thumbnailOffset.left;
			offsetTop = thumbnailOffset.top;
			elmWidth = $this.outerWidth();
			elmHeight = $this.outerHeight();
			differenceX = elmWidth - mediaWidth;
			differenceY = elmHeight - mediaHeight;
			differenceOffsetX = (differenceX/2);
			differenceOffsetY = (differenceY/2);
			newLeft =  offsetLeft + differenceOffsetX;
			newTop =  offsetTop + differenceOffsetY;
			xPos = newLeft + 'px';
			yPos = newTop + 'px';
			yPos = '200px';
			var output = '';
			output += 'mediaWidth: ' + mediaWidth + '\n';
			output += 'mediaHeight: ' + mediaHeight + '\n';
			output += 'thumbnailOffset: ' + thumbnailOffset + '\n';
			output += 'offsetLeft: ' + offsetLeft + '\n';
			output += 'offsetTop: ' + offsetTop + '\n';
			output += 'elmWidth: ' + elmWidth + '\n';
			output += 'elmHeight: ' + elmHeight + '\n';
			output += 'differenceX: ' + differenceX + '\n';
			output += 'differenceOffsetX: ' + differenceOffsetX + '\n';
			
			output += 'newLeft: ' + newLeft + '\n';

			//console.log(output);
			alert(output);
			$('#video-content').dialog({ position:[0, 0] });
			$('#video-content, #movie').css({'display': 'block', 'width': mediaWidth + 'px', 'height': mediaHeight + 'px'});
			
			$('.dialog-video').css({'position':'absolute', 'left': xPos, 'top': yPos, 'width': mediaWidth + 'px', 'height': mediaHeight + 'px'});
			$('#video-content').css({'width': mediaWidth + 'px', 'height': mediaHeight + 'px'});
			
			$('#video-content').bind( "dialogopen", function(event, ui) {
				
			});
		}
		else{
			$('#video-content, #movie').css({'display': 'block'});
		}
		*/
		$('#video-content, #movie').css({'display': 'block'});
		return false;
	});
	
	

	tunecastHtml = '<object width="400" height="160"><param name="movie" value="http://www.tunecore.com/swf/tc_run_h_v2.swf?widget_id=41838"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.tunecore.com/swf/tc_run_h_v2.swf?widget_id=41838" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="160"></embed></object>';
	$('#tunecast-widget').append(tunecastHtml);


	$('.form-submit').each(function(){
		_sspmc.processForm.setupForm($(this));
	});
	
	if($('body').attr('id')=='contact'){
		$('#contact-map').dialog({ dialogClass: 'dialog-map', width:640, height:480, autoOpen:false, modal: true, resizable:false, draggable: false });
		$('.contact-info').click(function(){
			$('#contact-map').dialog('open');

			$('.ui-widget-overlay').click(function(){
				$('#contact-map').dialog('close');
				return false;
			});


			return false;
		});
	}

	

/*
	var paddingTop = parseInt($('#container').css('paddingTop').replace(/px/, ''));
	var paddingBottom = parseInt($('#container').css('paddingBottom').replace(/px/, ''));
	windowHeight = $(window).height();
	newHeight = windowHeight - (paddingTop + paddingBottom);
	$('#container').css('height', newHeight + 'px');
	$('#footer').show();
	console.log('newHeight: ' + newHeight);
	pageId = $('body').attr('data-page');

*/
	$('.nav-primary.selected').click(function(){
		return false;
	});
	$('.nav-primary, h1 a').not('.selected, #nav-login .nav-primary').click(function(){
		$this = $(this);
		url = $this.attr('href');
		if(_sspmc.device!='iphone' && $('body').attr('id')!='home'){
			_sspmc.changePage.collapseCurrentPage(url);
		}
		else{
			_sspmc.changePage.goToNewPage(url);
		}
		return false;
	});

	if(_sspmc.device!='iphone'){
		$('.scrollpane').jScrollPane({dragMaxHeight:45});
		$('#work-content').jScrollPane({dragMaxHeight:145});
		$('.scrollpane').show();
		if($('body').attr('id')!='work'){
			_sspmc.changePage.loadNewPage();
		}
	}

	$('#nav-login a.nav-primary').click(function(e){
		e.preventDefault();
		if(!$('#login-content').hasClass('login-nav-open')){
			if($('#login-content').hasClass('login-header-open')){
				_sspmc.processLogin.hideFromHeader();
			}
			else{
				_sspmc.processLogin.showFromNav();
			}
		}
		return false;
	});
	$('#newsletter-content button').click(function(e){
		e.preventDefault();
		_sspmc.processNewsletter.submitNewsletterForm($(this));

		return false;
	});
	
	$('#increase-font a').click(function(e){
		_sspmc.helpers.increaseFont();
		return false;
	});
	$('#decrease-font a').click(function(e){
		_sspmc.helpers.decreaseFont();
		return false;
	});
	/*
	$('#online-db').click(function(e){
		e.preventDefault();
		if(!$('#login-content').hasClass('login-header-open')){
			if($('#login-content').hasClass('login-nav-open')){
				_sspmc.processLogin.hideFromNav();
			}
			else{
				_sspmc.processLogin.showFromHeader();
			}
		}
		return false;
	});
	*/
	$('#login-submit').click(function(){
//		_sspmc.processLogin.validateLogin();
		$('#login-form').submit();
		return false;
	});

/*
	$.get('json.php', function(data) {
		console.log(data);
	});
*/


});
