$(document).ready(function(){
	$.ajaxSetup({ cache: false });
	

	$('#gallery').nivoSlider({
		effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        animSpeed:500, // Slide transition speed
        pauseTime:7000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        directionNavHide:false, // Only show on hover
        controlNav:false, // 1,2,3... navigation
        controlNavThumbs:false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, // Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', // Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
        keyboardNav:true, // Use left & right arrows
        pauseOnHover:true, // Stop animation while hovering
        manualAdvance:false, // Force manual transitions
        captionOpacity:0.8, // Universal caption opacity
        prevText: 'Prev', // Prev directionNav text
        nextText: 'Next', // Next directionNav text
        beforeChange: function(){}, // Triggers before a slide transition
        afterChange: function(){}, // Triggers after a slide transition
        slideshowEnd: function(){}, // Triggers after all slides have been shown
        lastSlide: function(){}, // Triggers when last slide is shown
        afterLoad: function(){} // Triggers when slider has loaded	
	});
	
	/*view property gallery*/
	$('.view-img').cycle({delay: 6000});
	
	$('.view-img img').hover(function(){
		$('.view-img').cycle('pause')
	},function(){
		$('.view-img').cycle('resume')
	});
	
	$('.prev').click(function(){$('.view-img').cycle('prev')});
	$('.next').click(function(){$('.view-img').cycle('next')});
	
	$('#pause-play').toggle(function(){
		$('#pause-play span').text('play');
		$(this).attr('title', 'Play Gallery');
		$(this).removeClass('pause');
		$(this).addClass('play');
		$('.view-img').cycle('pause')
	},function(){
		$('#pause-play span').text('pause');
		$(this).attr('title', 'Pause Gallery');
		$(this).removeClass('play');
		$(this).addClass('pause');
		$('.view-img').cycle('resume') 
	});
	
	$('ul.nav').superfish({
		animation:   {opacity:'show',height:'show'}, 
		autoArrows	: false,
		dropShadows : false	
	});
	
	//$('select').customStyle();
	
	/*------begin contact page jquery stuff------*/
	$('div.maps-container a.hide').click(function(event){
		event.preventDefault();
		$('div.google-map').show();
		$('div.maps-container div.custom-map').fadeOut(500,function(){
			$('div.maps-container div.google-map').animate({opacity:1},500);
		});
	});
	
	$('div.maps-container a.show').click(function(event){
		event.preventDefault();
		$('div.maps-container div.google-map').animate({opacity: 0},500,function(){
			$('div.maps-container div.custom-map').fadeIn(500);
			$('div.google-map').hide();
		});
	});
	
	/*------end contact page jquery stuff------*/

	/* view best buy and featured */
	$('.view-list').click(function(e){
		e.preventDefault();
		var id = this.id;
		if ( $(this).hasClass('no-log')) 
			$(this).colorbox({iframe:true,scrolling: false,title: false,innerWidth:800, innerHeight:500});
		else 
			location.href='/search.html?'+(this.id)+'=1&page_number=1&clear_cache=1';
	});
	
	/* view newsletters */
	$('.snapshot_link').click(function(e){
		if ($(this).hasClass('no-log')){
			e.preventDefault();
			$(this).colorbox({close:'X', iframe:true,scrolling: false,title: false,innerWidth:800, innerHeight:500,
href:'http://gordonhillock.com/scripts/loginColorbox.php'});
		} 
	});
	/* save favorite */
	$('.add').click(function(e){
		e.preventDefault();
//log('adding to faves');
		var id = this.id;
		if ( $(this).hasClass('no-log')) {
			$(this).colorbox({iframe:true, innerWidth:800, innerHeight:500});
		} else {
			var queryString = id.replace(/_/g, '=').replace(/-/g, '&');
			$('#save_alert').html('One moment, saving your favorite.');
			$('#save_alert').show();
			
			$.get('/scripts/ajaxSaveFavorite.php?'+queryString, function(data){
				if (data.indexOf('success') === false )
					$('#save_alert').html('There was a problems saving your favorite.');
				else  {
//log(data);
					var fave_id = data.replace(/success_/, ''); 
					$('#save_alert').html('Your favorite was saved.');
					reloadUser();
					var span_id = $('#'+id).closest("span").attr("id");
//					log ('data is ' + data);
//					log ('span is ' + span_id);
//					log ('fave_id is ' + fave_id);
					$('#'+id).hide();
					$("#"+span_id+' .delete-favorite').show();
					$("#"+span_id+' .delete-favorite').attr('id', 'favorite_'+fave_id);
				}
				$('#save_alert').delay(4000).fadeOut(4000);
			});
		}
	});
	/* save search */
/*
	$('.save_search').click(function(e){
		e.preventDefault();
log('search from site.js');
		if ( $(this).hasClass('no-log')) {
			$(this).colorbox({iframe:true});
		} else {
			$('#save_alert').html('One moment, saving your search.');
			$('#save_alert').show();
			$.get('/scripts/saveSearchForm.php', function(data){
				if (data.indexOf('success') === false )
					$('#save_alert').html('There was a problems saving your search.');
				else  {
					var fave_id = data.replace(/success_/, ''); 
					$('#save_alert').html('Your search was saved.');
					reloadUser();
				}
				$('#save_alert').delay(4000).fadeOut(4000);
			});
		}
//	$(this).colorbox({iframe:true, innerWidth:700, innerHeight:400});
    });
*/
});
/* reloades the member sidebar and binds click events*/
function reloadUser(){
    if ($.colorbox != 'undefined')
        $.colorbox.close();
		var d = new Date();
		var nuk=d.getTime()
	$.get('/scripts/login_form.php', function(data){
		$('.left-column').html(data);
		$.get('/templates/include/quick_search.php?nul='+nuk, function(data){
			$('.left-column').html($('.left-column').html() + data);
			bindUser();
		});
	});
}
function bindUser(){
	$('.gk_link').click(function(e){
		e.preventDefault();
		$('.gk_link').removeClass('active');
		$(this).addClass('active');
		$('.cntr-column').html('One moment while I fetch your page...');
		$.get('/templates/'+(this.id).replace(/my_/, 'gold-')+'.phtml', function(data){
			$('.cntr-column').html(data);
		});
	});
	$('.gold_email').change(function(e){
		e.preventDefault();
		id = (this.id).replace(/gold_email_/, '');
		val = $(this).val();
		$.get('/scripts/ajaxChangeEmail.php?search_id='+id+'&email_schedule='+val, function(data){
			if (data != 'success'){
				$('#favorite_alert').html('There was a problem saving your schedule.');
				$('#favorite_alert').delay(4000).fadeOut(4000);
			}
		});
	})
	$('.delete-search').click(function(e){
		e.preventDefault();
		current_num = $('#my_searches > span > span').html().replace('(', '').replace(')', '');
		id = (this.id).replace(/search_/, '');
		$.get('/templates/include/ajaxDeleteSearch.php?search_id='+id, function(data){
			if (data == 'success') {
				if ($('#search-div_'+id) != 'undefined')
					$('#search-div_'+id).hide();
				if ($('#search_list_item_'+id) != 'undefined')
					$('#search_list_item_'+id).hide();
				$('#my_searches > span > span').html('(' + (current_num - 1) + ')');
			}
		});
	});
	$('.sidebar-delete-search').click(function(e){
		e.preventDefault();
		current_num = $('#my_searches > span > span').html().replace('(', '').replace(')', '');
		id = (this.id).replace(/sidebar-search_/, '');
		$.get('/templates/include/ajaxDeleteSearch.php?search_id='+id, function(data){
			if (data == 'success') {
				if ($('#search-div_'+id) != 'undefined')
					$('#search-div_'+id).hide();
				if ($('#search_list_item_'+id) != 'undefined')
					$('#search_list_item_'+id).hide();
				$('#my_searches > span > span').html('(' + (current_num - 1) + ')');
			}
		});
	});	
	$('#notify_hint_link').click(function(e){
		e.preventDefault();
log('clicked');
		$(this).colorbox({iframe:true,scrolling: false,title: false,innerWidth:400, innerHeight:200, href:'/scripts/cbox/cboxNotifyHint.php'});
	});
}
/* replace console.log so that it doesn't break windows */
function log(value) {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("msie") == -1)
		console.log(value);
}
function display_paused(st){
	if(st==1){
		$('#view-img-overlay').css( 'display', 'none' ) 
	}else{
		$('#view-img-overlay').css( 'display', 'block' ) 
	}

}


