$(document).ready(function(){
        $('.rollover').rollover();
	
	$('#headerTop').click(function(){
		document.location='index.php';
	})
        
        /*$("a.zoomImage").fancybox({
		'transitionIn'	:'elastic',
		'transitionOut'	:'elastic',
		'speedIn'	:600, 
		'speedOut'	:200
	});*/
	
	$('.loginBtn').click(function(){
		$('#loginPopup').toggle('slow');
		$('#username_login').focus();
		return false;
	});
	
	$('#close_btn').click(function(){
		$('#loginPopup').fadeOut();	
	});

	$("#report").click(function() {
		$.ajax({
			type: 'GET',
			url: 'report.php?listing_id=' + $('#report').attr('listing_id'),
			success:function(msg){
				$('#message_box').html(msg);
			}
		})
		
		$("#message_box").fadeIn();
		
	});
	
	$(".popup_link").click(function() {
		var content = $(this).attr("href");
		$.ajax({
			type: 'GET',
			url: 'popup.php?content=' + content + '&' + new Date().getTime(),
			success:function(msg){
				$('#darkBack').fadeTo('slow', 0.5);
				$('#popup_box').html(msg);
			}
		})
		
		$("#popup_box").fadeIn();
		return false;
	});
	
	$(".edit_button").click(function() {
		var listing_id = $(this).attr("listing_id");
		window.location  = "my_listing.php?pa=edit&listing_id=" + listing_id;
	})
	$(".grab_button").click(function() {
		var listing_id = $(this).attr("listing_id");
		
		$.ajax({
			type: 'GET',
			url: 'grab_popup.php?listing_id=' + listing_id,
			success:function(msg){
				if (msg == "login required") {
					document.location = "login.php?pa=required";
				}
				$('#message_box').fadeIn();
				$('#confirmGettingIt').html(msg);
			}
		})
		
	})
	$(".naw").click(function() {
		$('#message_box').fadeOut();
	})
	$(".close_btn").click(function() {
		$('#message_box').fadeOut();
	})
	

	$('#agree_btn').click(function(){
		var listing_id = $(this).attr("listing_id");
                $('#message_box').fadeOut();
                
                $.ajax({
                    type: 'GET',
                    url: 'grab_popup.php?pa=confirm_grab&listing_id=' + listing_id,
                    success: function(msg){
			var obj = jQuery.parseJSON(msg);
			
                        if (obj.got_it == 'yes') {
				
                            //hide grab button on list page and turn row green
			    $('.listing_line[listing_id$="' + listing_id + '"]').css("background-color","#C6D7F6");
			    
			    //hide grab button on view page and show email and mobile details
			    $('#contact_details').html(obj.contact_details);
			    $('.grab_button[listing_id$="' + listing_id + '"]').fadeOut();
                        } else {
                            
                        }
			$('#confirmGettingIt').html(obj.msg);
                        $('#message_box').fadeIn();
			
                        
                    }
                })
                return false;
            });
	
	
	
	$('#ask_a_question').click(function(){
		
		$(this).popupBox({
			chainedEvent:true,
			href:"view.php?pa=ask_a_question_ajax&listing_id=" + $(this).attr('listing_id'),
			height:300,
			width:400
		})
	})
	

	
	
});


function doTheFlashCheck(){
	var MM_contentVersion = 6;
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
	if ( plugin ) {
			var words = navigator.plugins["Shockwave Flash"].description.split(" ");
		    for (var i = 0; i < words.length; ++i)
		    {
			if (isNaN(parseInt(words[i])))
			continue;
			var MM_PluginVersion = words[i]; 
		    }
		var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
	}
	else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {
		if(!FlashDetect.installed){
			//no flash 	
		}else{
			MM_FlashCanPlay = new Object();
		}
	}
	
	if ( MM_FlashCanPlay ) {
		//flash installed
	} else{
		$('.noFlash').show();
		$('.noFlashHide').hide();
		var temp_id = $('#non_flash_upload').attr('temp_id');
		$('#non_flash_upload').attr('src','iframe_image_form.php?temp_id=' + temp_id);
	}
}

function siteCheck(){
	msg = "<div class='browserCont'><a href='./close_nasty_ie_popup.php' style='float: right'>x Close</a><div style='font-size: 19px; font-weight: bold; color: red;'>THIS SITE REQUIRES AT LEAST INTERNET EXPLORER 7<br />Please update your browser, It's free!<br />For best the best experience get <a href='http://www.mozilla-europe.org/en/firefox/' target='_blank'>Firefox</a> or update <a href='http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx'>Internet Explorer</a></div></div>";
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	 if (ieversion>=7){
	        
	 }
	 else if (ieversion>=6)
	  document.write(msg)
	 else if (ieversion>=5)
	  document.write(msg)
	}
	else{
	
	}
}

function limitChars(textid, limit, infodiv){
	
	var text = $('#'+textid).val();
	var textlength = text.length;
	
	if(textlength > limit){
		$('#' + infodiv).html('<span class="errorMsg">You cannot write more than '+limit+' characters!</span>');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else{
		$('#' + infodiv).html((limit - textlength) +' characters remaining.');
		return true;
	}
}
