var df_speed = 500;
var $ul_navfish;
var $headerlogo;
var $errorTimer;
var intervalArray = new Array();
function clear_err(){$('form .errmsg').slideUp(function(){$(this).remove();});}
function setImage(myssp){
	if(myssp.data('i')===-1){myssp.data('i',1);}
	myssp.find('div.img').fadeOut(function(){
		$(this).html('<img src="'+myssp.data('images')[myssp.data('i')]+'" />').fadeIn();
	});
	if(myssp.data('i')===myssp.data('images').length-1){
		myssp.data('i',0);
	}else{
		myssp.data('i',myssp.data('i')+1);
	}
}
$(function() {

		$ul_navfish = $('ul.navfish');
		if($ul_navfish.length){
			$('ul.navfish').fishMenu();
		}
		$headerlogo = $('#headerlogo');
		$headerlogo.hide();
		
		IE7 = navigator.appVersion.indexOf("MSIE 7.") != -1;
		if(IE7){
			$('div a.local').css({background:'#212121'});
		}
		
});

function loadssp(myssp,id,sspsize){
if(id){
	var imgArr = new Array();
	$.getJSON('/data/albums/?size='+ sspsize+'&aid='+id, function(json) {
		if(json){
			
			for (i=0;i<json.length;i++){
				src = json[i].thumb[0];
				if(src){imgArr.push(src);}	
			}
			
			if(imgArr.length >1){
				myssp.data('images',imgArr);
				intervalArray[id] = setInterval(function() { setImage(myssp); },7500);
			}
			myssp.data('i',-1).find('div.img').html('<img src="'+imgArr[0]+'" />');
		}
    });

}
}



$(window).load(function(){
	$('#z_nav .minitag, #headerlogo').css({cursor:'pointer'}).click(function(){
		window.location = "./";
		return false;
	});
	
	$pricebeat = $('#pricebeat');
	$pricebeat.css({cursor:'pointer'}).click(function(){window.location = $(this).find('a').attr('href');});
	
	$serviceboxes = $('body.home div.service .img');
	$serviceboxes.click(function(){window.location = $(this).parent().find('a').attr('href');});
	
	$slideshows = $('div.slideshow');
	if ($slideshows.length){
		$slideshows.each(function(){
			var myid = $(this).attr('id').split('-');
			loadssp($(this),myid[1],myid[2])
		});
		
	}
	
	/*
	$lrgslideshows = $('div.slideshow-lrg');
	if ($lrgslideshows.length){
		$lrgslideshows.each(function(){
			var myid = $(this).attr('id').split('-');
			loadssp($(this),myid[1],'lrg')
		});
	}
	*/
	
	var ie8 = $.browser.msie && +$.browser.version == 8;
	var ie7 = $.browser.msie && +$.browser.version == 7;
	if(ie8 || ie7){$('a.local').css({background:'#212121',padding:'3px 10px'});}
	
	var zflashvars = {};var zparams = {wmode:'transparent'};var zattributes = {};	
	swfobject.embedSWF("/flash/logo.swf", 'logobox', "375px", "230px", "9.0.0", null ,zflashvars, zparams, zattributes, outputStatus);
		
	function outputStatus(e) {
			if(e.success){
				
			}else{
				$headerlogo.show();	
			};
	}
	
	
	$z_headertext = $('#z_headertext');
	$taglines = $('#taglines');
	$diff = $z_headertext.height() - $taglines.height();
	$diff+='px'; 
	$taglines.css({marginTop:$diff});
	

	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){
				$(this).bind('selectstart',function(){return false;});
			}else{
				$(this).mousedown(function(){return false;});
			}
		});
	});
	$('.button').disableTextSelect();
	$pricebeat.disableTextSelect();
	
	
	/* ===================================== */
	
	btn_sendfeedback = $('form .btn_sendfeedback');
	btn_sendfeedback.css({cursor:'pointer'}).live('click',function(){
		$(this).blur().hide();
		
		$form_feedback = $('form#feedback');
		$form_feedback.find('.errmsg').slideUp(function(){$(this).remove();});

		if($form_feedback.size()==1){
		
			$str = $form_feedback.serialize();
			
			$.post('/data/forms/feedback/',$str,function(data){
				
				if(data === 'success'){
					$form_feedback.slideUp(function(){
						$(this).html('<div class="msg"><div class="pad"><strong>Success! </strong>Your feedback has been sent.</div></div>').slideDown();});
				}else{
					clearTimeout($errorTimer);$errorTimer = setTimeout ('clear_err()', 5000 );
					$form_feedback.append('<div class="errmsg"><div class="pad">' + data + '</div></div>');
					btn_sendfeedback.show();
				}
			});
		}
		return false;
	});
	
	/* ===================================== */

	btnSendmail = $('form .btn_sendmail');
	btnSendmail.css({cursor:'pointer'}).live('click',function(){
		$(this).blur().hide();
		
		$form_email = $('form#generalemail');
		$form_email.find('.errmsg').slideUp(function(){$(this).remove();});

		if($form_email.size()==1){
		
			$str = $form_email.serialize();
			
			$.post('/data/postemail/',$str,function(data){
				
				if(data === 'success'){
					$form_email.slideUp(function(){
						$(this).html('<div class="msg"><div class="pad"><strong>Success! </strong>Your email has been sent.</div></div>').slideDown();});
				}else{
					clearTimeout($errorTimer);$errorTimer = setTimeout ('clear_err()', 5000 );
					$form_email.append('<div class="errmsg"><div class="pad">' + data + '</div></div>');
					btnSendmail.show();
				}
			});
		}
		return false;
	});
	
	/* ===================================== */
	
	$('div .btn_subscribe').css({cursor:'pointer'}).live('click',function(){
			$(this).blur();
			$('div .btn_subscribe').hide();		  
			$form_subscribe = $('form#form_subscribe');
			
			$form_subscribe.find('.errmsg').slideUp(function(){
				$(this).remove();													
			});
			
			if($form_subscribe.size()==1){ /* there can be only 1. yes, just like highlander. */
			
				$str = $form_subscribe.serialize();
				$.post('/data/list/join/',$str,function(data){
					if(data === 'success'){
						$form_subscribe.slideUp(function(){
							$(this).html('<div class="message"><strong>Success!</strong> A confirmation email will be sent to you shortly.</div>').slideDown();								 						 
						});
					}else{
						clearTimeout($errorTimer);$errorTimer = setTimeout ('clear_err()', 5000 );
						$form_subscribe.prepend('<div class="errmsg">' + data + '</div>');
						$('div .btn_subscribe').show();
					}
					
					
				});
				
			}
			return false;
			
	});
	
	/* ===================================== */
	
	$('div .btn_unsubscribe').live('click',function(){
			$(this).blur();
			$('div .btn_unsubscribe').hide();
			
			$form_unsubscribe = $('form#form_unsubscribe');
			$form_unsubscribe.find('.errmsg').slideUp(function(){
				$(this).remove();													
			});
			
			if($form_unsubscribe.size()==1){ /* there can be only 1. yes, just like highlander. */
				$str = $form_unsubscribe.serialize();
				$.post('/data/list/leave/',$str,function(data){
					if(data === 'success'){
						$form_unsubscribe.slideUp(function(){
							$(this).html('<div class="message"><strong>Success!</strong> A confirmation email will be sent to you shortly.</div>').slideDown();							 						 
						});
					}else{
						clearTimeout($errorTimer);$errorTimer = setTimeout ('clear_err()', 5000 );
						$form_unsubscribe.prepend('<div class="errmsg">' + data + '</div>');
						$('div .btn_unsubscribe').show();
						
					//alert(data);
					}
					
				});
				
			}
			return false;
			
	});
	
});
