// JavaScript Document
$(document).ready(function(){
	
	var range = $("input[name="+$('.plus').attr('rel')+"]").val();
	var url	= BASEURL+"user/";	
	var perpage = 2;
    var responseId = '';

	pagelimit(url);
	navi(url);
	//showCategoriesLeft();
	myallauctiontabs();
	productSlider();
	productDesc();
	learnmorebib();
	purchaseProcess();
	WonPaymentProcess();
	paymentoptions();
	//CheckLoginLandingPage();
	//text_hideshow();
	
});
 
function UrlExists(url)
{
	var http = new XMLHttpRequest();
	http.open('HEAD', url, false);
	http.send();
	return http.status!=404;
}

function productSlider()
{
	
	$('.prod-mainImg .mainImg').hover(function(){
 
		var pos = $(this).position();

		$('.responseImg').css('left',pos.left+200);
		$('.responseImg').css('top',pos.top+50);
		$('.responseImg').css('z-index','200');
		$('.responseImg').html('<img src="'+$(this).attr('myattr')+'">');
		$('.responseImg').show('slow');
	
	},function(){
	
		$('.responseImg').hide('slow');
	
	});
	$('.showthumbs img').click(function(){
			if(UrlExists($(this).attr('rel')))
			{
				$('.prod-mainImg .mainImg').hide();
				$('.prod-mainImg .mainImg').attr('src',$(this).attr('rel'));
				$('.prod-mainImg .mainImg').attr('myattr',$(this).attr('myattr'));
				$('.prod-mainImg .mainImg').fadeIn();
			}
			else
			{
				$('.prod-mainImg .mainImg').attr('src','<?=BASECSS?>images/no_image_thumb.gif');
			}
	});
	
}


function productDesc()
{
	
 	
	$('.item-details .itemFullDesc').click(function(){
 
 	jQuery.fn.center = function () {
			var pos = this.position();
			this.css("position","absolute");
			this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
			this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
			$(this).fadeIn();
			return this;
		}
		$('#itemFullDesc').center();
	
	});
	$('.item-details .closeDesc').click(function(){
 	
		$('#itemFullDesc').hide();
	
	});
	 
	
}

function learnmorebib()
{
	
	$('.threebid .itemFullDescforbib').click(function(){
 
 	jQuery.fn.center = function () {
			var pos = this.position();
			this.css("position","absolute");
			this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
			this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
			$(this).fadeIn();
			return this;
		}
		$('#itemFullDescforbib').center();
	
	});
	$('.threebid .closeDesc').click(function(){
 	
		$('#itemFullDescforbib').hide();
	
	});
 
	
}

function paymentoptions()
{
	// FRO PAYMENT OPTION 1
	$('.myaccount .payoptions1').click(function(){
 	
 	jQuery.fn.center = function () {
			var pos = this.position();
			this.css("position","absolute");
			this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
			this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
			$(this).fadeIn();
			return this;
		}
		$('#payoptions1').center();
	
	});
	$('.myaccount .closeDesc').click(function(){
 	
		$('#payoptions1').hide();
	
	});
	
	// FRO PAYMENT OPTION 2
	$('.myaccount .payoptions2').click(function(){
 	
 	jQuery.fn.center = function () {
			var pos = this.position();
			this.css("position","absolute");
			this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
			this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
			$(this).fadeIn();
			return this;
		}
		$('#payoptions2').center();
	
	});
	$('.myaccount .closeDesc').click(function(){
 	
		$('#payoptions2').hide();
	
	});
	
	// FRO PAYMENT OPTION 3
	$('.myaccount .payoptions3').click(function(){
 
 	jQuery.fn.center = function () {
			var pos = this.position();
			this.css("position","absolute");
			this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
			this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
			$(this).fadeIn();
			return this;
		}
		$('#payoptions3').center();
	
	});
	$('.myaccount .closeDesc').click(function(){
 	
		$('#payoptions3').hide();
	
	});
		
}


function purchaseProcess()
{
	
	var credit_selection = '';
	
	$('input[name=buy]').click(function(){
											  
		
		credit_selection = $(this).val();
	});

	$('input.purchaseLightBox').click(function(){
 
 		 if(credit_selection == '')
		 {
			 alert("Please Select Any Option");
			 return false;
		 }
		 else
		 {
			$('input[name=overridePrice]').attr('value',credit_selection); 
			if(confirm('Do You Want To Purchase Credit?'))
			{
				var pos = $('input#purchaseLightBox').position();
				$('#purchaseLightBox').css("position","absolute");
				$('#purchaseLightBox').css("top", ( $(window).scrollTop()  + "px"));
				$('#purchaseLightBox').css("left", ( $(window).width() - $('#purchaseLightBox').width() ) / 2+$(window).scrollLeft() + "px");
				$('#purchaseLightBox').fadeIn();	
				document.getElementById("purchaseform").submit();
			}
			else
			{
				return false;	
			}
		 }
	});
	
}

/* TO CHECK THAT PAYMENT IS ALREADY DONE OR NOT*/
 
function WonPaymentProcess()
{
	
	var credit_selection = $('input[name=Pay_type]').val();
	$('input[name=Pay_type]').click(function(){
											  
		//alert($(this).val()); 
		credit_selection = $(this).val();
	});
	
	$('input.WonPayLightBox').click(function(){
 
 		 
		$.post("pay_options.php", {action:"checkOption",aid:$('#aucid').val()}, function(data){ 
			
			 
			if(data=="0")
			{
				alert("Payment is already done on auction#"+$('#aucid').val());
				var url	=	BASEURL+'auction/myaccount.php';
				window.location= url;
				return false;
			}
			else
			{
				if(credit_selection == '2')
				{
					if(confirm('This will direct to a payment page for the item.Click ok to confirm, or cancel for a different option selection.'))
					{
						var pos = $('input#WonPayLightBox').position();
						$('#WonPayLightBox').css("position","absolute");
						$('#WonPayLightBox').css("top", ( $(window).scrollTop()  + "px"));
						$('#WonPayLightBox').css("left", ( $(window).width() - $('#WonPayLightBox').width() ) / 2+$(window).scrollLeft() + "px");
						$('#WonPayLightBox').fadeIn();	
						document.getElementById("paymentform").submit();
					}
					else
					{
						return false;	
					}
				}
				else
				{
					document.getElementById("payForWonAuction").submit();
				}
			}
		});	 
		
		//$('input[name=overridePrice]').attr('value',credit_selection);
		
	});
	
}



function myallauctiontabs()
{
	
    $('.myaccountshowhide').hide();
	var firstshow = $('.myauctionTabs h4.active').attr('title');
	$(firstshow).show();
	if(firstshow =='#auctions')
	{
		$('.myallauction a.first').addClass('active');
	}
	$('.myauctionTabs h4').click(function(){
									   
		//alert($(this).attr('title'));
		var showDiv = $(this).attr('title');
		$('.myauctionTabs h4').removeClass('active');
		$(this).addClass('active');
		$('.myaccountshowhide').hide();
		//alert('"'+showDiv+'"');
		$(showDiv).show();
		if(showDiv !='#auctions')
		{
			//alert('none');
			$('#auctions-inner').hide();
			$('.myallauction a').removeClass('active');
		}
		else
		{
			//alert('hi');
			$('#auctions-inner').show();
			$('.myauctionTabs .first,.myallauction a.first').addClass('active');
			//$('.myallauction a.first').addClass('active');
		}
	});
	
	$('.myallauction a').click(function(){
		
		$('#auctions-inner').show(); 
		$('.myaccountshowhide').hide();
		//alert($(this).attr('rel'));
		var showDiv = $(this).attr('rel');
		var getClass = $(this).attr('class');
		var getClass_arr = getClass.split(" ");
		var sel_class = getClass_arr[0]; 

		$('.myallauction a').removeClass('active');
		$('.myauctionTabs h4').removeClass('active');
		$('.myauctionTabs .first').addClass('active');
		$("."+sel_class+"").addClass('active');
		$('#auctions-inner').addClass('active');
		$('#auctions-inner').addClass('active');
		$(showDiv).show();
		
	});	
}

function showCategoriesLeft()
{

    $('a.level0 li ul').hide();
	$('a.level0').click(function(){

		var parentli = $(this).parent('li').find('ul');
		var showhide = $(parentli).css('display');
		
		$('li ul').hide();

		if(showhide == 'block') {  $(parentli).hide();	} else { $(parentli).show(); }
		
		$('li.currentParent ul').show();
		return false;	
	});
	
}

function navi(url)
{
	$('a.navigator').click(function(){

		var passdata = $(this).attr('rel');
		var responseId = $(this).attr('myattr');
		 
		var loderimage = BASEURL+'css/images/ajax-loader.gif';
		
		$('#'+responseId).html('<div style="text-align:center;"><img src="'+loderimage+'"></div>');
		 
		$.post(url+responseId+".php", {data:passdata}, function(data)
			{
				
				$('#'+responseId).html(data);
				navi(url);
			}, "text");
	});
}

function pagelimit(url)
{
	
     $('a.live-link').click(function(){
			
			var passdata = $(this).attr('rel');
			var responseId = $(this).attr('myattr');
			
			//responseId = $(this).parent('div').attr('class');
			var loderimage = BASEURL+'css/images/ajax-loader.gif';
		 
			$('#'+responseId).html('<div style="text-align:center;"><img src="'+loderimage+'"></div>');
			$.post(url+responseId+".php", {data:passdata}, function(data)
				{
					$('#'+responseId).html(data);
					navi(url);
					
				}, "text");									
				
	});	
}

/* TO CHANGE THE ACTIVE STATE OF THE NOTIFICATION */
function change_notification()
{
	$(".myauctionTabs .messages").click(function(){
		var infopath	=  window.location.href;
		//alert(infopath);
		$.post(infopath,{notify:'activate'}, function(data)
		{
			return data;
		},"text");	
	});	
}



function item_refresh(auctionId)
{
	 
	
	var infopath	=  BASEURL+'user/ajax-item.php';
	//alert(BASEURL);
	//alert(infopath);
	//var loderimage = BASEURL+'css/default/images/loding_animation.gif'; //[<img src="'+loderimage+'">]
	$('body').append('<div class="removeAutoRefresh" style="text-align:center;min-height:135px;position:absolute;top:-20px;right:0;height:100%;width:50%;text-align:right;"></div>');
	//$('body').append('<div class="removeAutoRefresh" style="text-align:center;min-height:135px;position:absolute;top:0;left:0;height:100%;width:95%;"><img src="'+loderimage+'" style="text-align:center;margin-top:200px;"></div>');
	//$('.responseLeftSide,.leftColResponse,.rightColResponse,.middleColResponse,.liveBiddersColResponse').html('<div style="text-align:center;min-height:135px;position:absolute;top:0;left:50%;"><img src="'+loderimage+'"></div>');
	
	$.post(infopath,{aid:auctionId}, function(data)
	{
		//alert(data);	
		var data_array = data.split('|||||');
		//alert(data_array[1]);
		$('.responseLeftSide').html(data_array[0]);
		$('.leftColResponse').html(data_array[1]);
		$('.middlelColResponse').html(data_array[2]);		
		$('.middlecColResponse').html(data_array[3]);		
		$('.middlerColResponse').html(data_array[4]);		
		$('.rightColResponse').html(data_array[5]);		
		$('.liveBiddersColResponse').html(data_array[6]);		
		
		setTimeout(function(){ $('div.removeAutoRefresh').fadeOut(); },1000);
		
		
	},"text");	
	
	setTimeout(function(){ item_refresh(auctionId); },10000);
		
}
function serverTime1() { 
    var time = null; 
    $.ajax({url: 'http://www.paylessbid.com/serverTime.php', 
        async: false, dataType: 'text', 
        success: function(text) { 
           //	alert(text);
			time = new Date(text); 
		//	alert(time);
        }, error: function(http, message, exc) { 
            time = new Date(); 
    }}); 
    return time; 
}


/* TO CHECK LOGIN ON LANDING PAGE */
 
function CheckLoginLandingPage()
{
	var username = $('#usernamel').val();
	var password = $('#passwordl').val();
	var returl = $('#landingRet').val();
	var path	  = BASEURL+'user/land_signup.php';
	$.post(path, {action:"checkLogin",uname:username,pwd:password,ret:returl}, function(data){ 
		
		
		if(data=="0")
		{
			alert("User does not Exist Or you might have entered wrong password");
		}
		else
		{
			window.location=data;	
		}
	});	
	
}
